Skip to content

Commit

Permalink
Merge pull request #17 from hammingweight/change-err-check-idiom
Browse files Browse the repository at this point in the history
Rewrite some error checks more idiomatically.
  • Loading branch information
hammingweight authored Jan 18, 2025
2 parents c688925 + c016d26 commit 93366d5
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 16 deletions.
3 changes: 1 addition & 2 deletions cmd/configuration_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ func generate() error {
Password: password,
DefaultInverterSN: inverterSN,
}
err := configuration.WriteConfigurationToFile(viper.GetString("config"), config)
if err != nil {
if err := configuration.WriteConfigurationToFile(viper.GetString("config"), config); err != nil {
return fmt.Errorf("%w: %w", ErrCantCreateConfigFile, err)
}
fmt.Printf("Wrote configuration to '%s'.\n", configFile)
Expand Down
3 changes: 1 addition & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ func Execute() {
// Ensure that commands timeout after 30 seconds
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
err := rootCmd.ExecuteContext(ctx)
if err != nil {
if err := rootCmd.ExecuteContext(ctx); err != nil {
os.Exit(1)
}
}
Expand Down
3 changes: 1 addition & 2 deletions configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ type Configuration struct {

func createAndOpenFile(filename string) (*os.File, error) {
dir := filepath.Dir(filename)
err := os.MkdirAll(dir, 0700)
if err != nil {
if err := os.MkdirAll(dir, 0700); err != nil {
return nil, err
}
return os.OpenFile(filename, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0600)
Expand Down
9 changes: 3 additions & 6 deletions configuration/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,11 @@ func TestWriteConfiguration(t *testing.T) {
Password: password,
}
w := &bytes.Buffer{}
err := writeConfiguration(w, configuration)
if err != nil {
if err := writeConfiguration(w, configuration); err != nil {
t.Fatal("error:", err)
}
configMap := map[string]string{}
err = yaml.Unmarshal(w.Bytes(), configMap)
if err != nil {
if err := yaml.Unmarshal(w.Bytes(), configMap); err != nil {
t.Fatal("error:", err)
}
if len(configMap) != 3 {
Expand All @@ -132,8 +130,7 @@ func TestWriteConfigurationToFile(t *testing.T) {
Password: password,
}
filename := filepath.Join(t.TempDir(), "config")
err := WriteConfigurationToFile(filename, configuration)
if err != nil {
if err := WriteConfigurationToFile(filename, configuration); err != nil {
t.Fatal("error: ", err)
}
f, err := os.Open(filename)
Expand Down
3 changes: 1 addition & 2 deletions rest/inverter.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ func (synkClient *SynkClient) inverterSerialNumbers(ctx context.Context, page in
queryParams["type"] = "-2"
queryParams["status"] = "-1"
resp := &map[string]any{}
err := synkClient.readAPIV1(ctx, resp, queryParams, path...)
if err != nil {
if err := synkClient.readAPIV1(ctx, resp, queryParams, path...); err != nil {
return nil, err
}
allInverters, ok := (*resp)["infos"]
Expand Down
3 changes: 1 addition & 2 deletions rest/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ func TestString(t *testing.T) {
// Test that the String method returns valid JSON
s := SynkObject{"foo": 3, "bar": "baz"}.String()
data := map[string]any{}
err := json.Unmarshal([]byte(s), &data)
if err != nil {
if err := json.Unmarshal([]byte(s), &data); err != nil {
t.Fatal(err)
}
if len(data) != 2 {
Expand Down

0 comments on commit 93366d5

Please sign in to comment.