Skip to content

Commit

Permalink
add error wraps
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnewhall committed Apr 15, 2021
1 parent cab8cf3 commit 9954178
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 11 deletions.
36 changes: 30 additions & 6 deletions cameras.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,29 +209,45 @@ func (c *Camera) ToggleContinuous(arm CameraArmMode) error {
params := make(url.Values)
params.Set("arm", string(arm))

return c.server.SimpleReq("++ssControlContinuous", params, c.Number)
if err := c.server.SimpleReq("++ssControlContinuous", params, c.Number); err != nil {
return fmt.Errorf("request failed: %w", err)
}

return nil
}

// ToggleMotion arms (true) or disarms (false) a camera's motion capture mode.
func (c *Camera) ToggleMotion(arm CameraArmMode) error {
params := make(url.Values)
params.Set("arm", string(arm))

return c.server.SimpleReq("++ssControlMotionCapture", params, c.Number)
if err := c.server.SimpleReq("++ssControlMotionCapture", params, c.Number); err != nil {
return fmt.Errorf("request failed: %w", err)
}

return nil
}

// ToggleActions arms (true) or disarms (false) a camera's actions.
func (c *Camera) ToggleActions(arm CameraArmMode) error {
params := make(url.Values)
params.Set("arm", string(arm))

return c.server.SimpleReq("++ssControlActions", params, c.Number)
if err := c.server.SimpleReq("++ssControlActions", params, c.Number); err != nil {
return fmt.Errorf("request failed: %w", err)
}

return nil
}

// TriggerMotion sets a camera as currently seeing motion.
// Other actions likely occur because of this!
func (c *Camera) TriggerMotion() error {
return c.server.SimpleReq("++triggermd", make(url.Values), c.Number)
if err := c.server.SimpleReq("++triggermd", make(url.Values), c.Number); err != nil {
return fmt.Errorf("request failed: %w", err)
}

return nil
}

// SetSchedule configures a camera mode's primary schedule.
Expand All @@ -242,7 +258,11 @@ func (c *Camera) SetSchedule(mode CameraMode, scheduleID int) error {
params.Set("mode", string(mode))
params.Set("id", strconv.Itoa(scheduleID))

return c.server.SimpleReq("++ssSetSchedule", params, c.Number)
if err := c.server.SimpleReq("++ssSetSchedule", params, c.Number); err != nil {
return fmt.Errorf("request failed: %w", err)
}

return nil
}

// SetScheduleOverride temporarily overrides a camera mode's current schedule.
Expand All @@ -253,7 +273,11 @@ func (c *Camera) SetScheduleOverride(mode CameraMode, overrideID int) error {
params.Set("mode", string(mode))
params.Set("id", strconv.Itoa(overrideID))

return c.server.SimpleReq("++ssSetOverride", params, c.Number)
if err := c.server.SimpleReq("++ssSetOverride", params, c.Number); err != nil {
return fmt.Errorf("request failed: %w", err)
}

return nil
}

/* INTERFACE HELPER METHODS FOLLOW */
Expand Down
7 changes: 6 additions & 1 deletion cameras_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package securityspy

import (
"encoding/xml"
"fmt"
)

// Encoder is the path to ffmpeg.
Expand Down Expand Up @@ -44,7 +45,11 @@ type CameraSchedule struct {
// UnmarshalXML stores a schedule ID into a CameraSchedule type.
// This isn't a method you should ever call directly; it is only used during data initialization.
func (bit *CameraSchedule) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
return d.DecodeElement(&bit.ID, &start)
if err := d.DecodeElement(&bit.ID, &start); err != nil {
return fmt.Errorf("decoding xml: %w", err)
}

return nil
}

// Camera defines the data returned from the SecuritySpy API. This data is directly
Expand Down
6 changes: 5 additions & 1 deletion ptz.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,11 @@ func (z *PTZ) ptzReq(command ptzCommand) error {
params := make(url.Values)
params.Set("command", strconv.Itoa(int(command)))

return z.camera.server.SimpleReq("++ptz/command", params, z.camera.Number)
if err := z.camera.server.SimpleReq("++ptz/command", params, z.camera.Number); err != nil {
return fmt.Errorf("ptz failed: %w", err)
}

return nil
}

// UnmarshalXML method converts ptzCapbilities bitmask from an XML payload into true/false abilities.
Expand Down
6 changes: 5 additions & 1 deletion schedules.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,9 @@ func (s *Server) SetSchedulePreset(presetID int) error {
params := make(url.Values)
params.Set("id", strconv.Itoa(presetID))

return s.SimpleReq("++ssSetPreset", params, -1)
if err := s.SimpleReq("++ssSetPreset", params, -1); err != nil {
return fmt.Errorf("http request: %w", err)
}

return nil
}
7 changes: 6 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ func (s *Config) GetContextClient(ctx context.Context, api string, params url.Va

req.URL.RawQuery = params.Encode()

return client.Do(req)
resp, err := client.Do(req)
if err != nil {
return resp, fmt.Errorf("http request: %w", err)
}

return resp, nil
}

// GetContext is the same as Get except you can pass in your own context.
Expand Down
2 changes: 1 addition & 1 deletion server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func testingHTTPClient(handler http.Handler) (*http.Client, *httptest.Server) {
client := &http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, network, _ string) (net.Conn, error) {
return net.Dial(network, fakeServer.Listener.Addr().String())
return net.Dial(network, fakeServer.Listener.Addr().String()) //nolint:wrapcheck
},
},
}
Expand Down

0 comments on commit 9954178

Please sign in to comment.