Skip to content

Commit

Permalink
Merge pull request #18 from hammingweight/some-more-error-check-refac…
Browse files Browse the repository at this point in the history
…toring

Rewrite error checking more idiomatically.
  • Loading branch information
hammingweight authored Jan 18, 2025
2 parents 93366d5 + e72e3b2 commit 6d37535
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 16 deletions.
3 changes: 1 addition & 2 deletions cmd/inverter_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ func applyInverterSettings(ctx context.Context, in *os.File) error {
return err
}
settings := &rest.Inverter{}
err = json.Unmarshal(data, settings)
if err != nil {
if err = json.Unmarshal(data, settings); err != nil {
return err
}
return client.UpdateInverter(ctx, settings)
Expand Down
6 changes: 2 additions & 4 deletions cmd/inverter_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ func updateInverterSettings(ctx context.Context) error {
if err != nil {
return fmt.Errorf("%w: essential-only must be \"true\" or \"false\", not \"%s\"", ErrCantUpdateInverterSettings, essentialOnly)
}
err = inverterSettings.SetLimitedToLoad(flag)
if err != nil {
if err = inverterSettings.SetLimitedToLoad(flag); err != nil {
return fmt.Errorf("%w: %w", ErrCantUpdateInverterSettings, err)
}
}
Expand All @@ -60,8 +59,7 @@ func updateInverterSettings(ctx context.Context) error {
if err != nil {
return fmt.Errorf("%w: battery-capacity must be an integer, not \"%s\"", ErrCantUpdateInverterSettings, batteryCap)
}
err = inverterSettings.SetBatteryCapacity(batteryCapInt)
if err != nil {
if err = inverterSettings.SetBatteryCapacity(batteryCapInt); err != nil {
return fmt.Errorf("%w: %w", ErrCantUpdateInverterSettings, err)
}
}
Expand Down
3 changes: 1 addition & 2 deletions configuration/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ func TestWriteConfigurationToFile(t *testing.T) {
t.Fatal("error: ", err)
}
configMap := map[string]string{}
err = yaml.Unmarshal(data, configMap)
if err != nil {
if err = yaml.Unmarshal(data, configMap); err != nil {
t.Fatal("error: ", err)
}
if len(configMap) != 3 {
Expand Down
9 changes: 3 additions & 6 deletions rest/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ func unmarshalResponseData(resp *http.Response, data any) error {
return err
}
synkResponse := SynkResponse{}
err = json.Unmarshal(body, &synkResponse)
if err != nil {
if err = json.Unmarshal(body, &synkResponse); err != nil {
return err
}
if !synkResponse.Success {
Expand All @@ -58,8 +57,7 @@ func unmarshalResponseData(resp *http.Response, data any) error {
if err != nil {
return nil
}
err = json.Unmarshal(dataBytes, data)
if err != nil {
if err = json.Unmarshal(dataBytes, data); err != nil {
return nil
}
}
Expand Down Expand Up @@ -91,8 +89,7 @@ func (synkClient *SynkClient) readAPIV1(ctx context.Context, synkObject any, que
if err != nil {
return err
}
err = unmarshalResponseData(resp, synkObject)
if err != nil {
if err = unmarshalResponseData(resp, synkObject); err != nil {
return err
}
return nil
Expand Down
3 changes: 1 addition & 2 deletions rest/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ func Authenticate(ctx context.Context, config *configuration.Configuration) (*Sy
return nil, err
}
tokens := &tokens{}
err = unmarshalResponseData(resp, tokens)
if err != nil {
if err = unmarshalResponseData(resp, tokens); err != nil {
return nil, err
}
return &SynkClient{endpoint: config.Endpoint, tokens: *tokens, SerialNumber: config.DefaultInverterSN}, err
Expand Down

0 comments on commit 6d37535

Please sign in to comment.