Skip to content

Commit 530be91

Browse files
authored
Simplify error handling (#741)
## Summary Remove unnecessary `if` check and `err` variable declaration. ## How was it tested? tests. Signed-off-by: Zhizhen He <[email protected]>
1 parent 01dc9b9 commit 530be91

File tree

21 files changed

+39
-143
lines changed

21 files changed

+39
-143
lines changed

devbox.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ type Devbox interface {
3939
RemoveGlobal(pkgs ...string) error
4040
RunScript(scriptName string, scriptArgs []string) error
4141
Services() (plugin.Services, error)
42-
// Shell generates the devbox environment and launches nix-shell as a child
43-
// process.
42+
// Shell generates the devbox environment and launches nix-shell as a child process.
4443
Shell() error
4544
// ShellPlan creates a plan of the actions that devbox will take to generate its
4645
// shell environment.
@@ -55,8 +54,7 @@ func Open(dir string, writer io.Writer) (Devbox, error) {
5554
return impl.Open(dir, writer)
5655
}
5756

58-
// InitConfig creates a default devbox config file if one doesn't already
59-
// exist.
57+
// InitConfig creates a default devbox config file if one doesn't already exist.
6058
func InitConfig(dir string, writer io.Writer) (bool, error) {
6159
return impl.InitConfig(dir, writer)
6260
}

internal/boxcli/gen-docs.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ func genDocsCmd() *cobra.Command {
5151
// ###### Auto generated by spf13/cobra on 18-Jul-2022
5252
rootCmd.DisableAutoGenTag = true
5353

54-
err = doc.GenMarkdownTree(rootCmd, docsPath)
55-
return errors.WithStack(err)
54+
return errors.WithStack(doc.GenMarkdownTree(rootCmd, docsPath))
5655
},
5756
}
5857

internal/boxcli/generate/devcontainer_util.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,7 @@ func CreateDockerfile(tmplFS embed.FS, path string) error {
4545
tmplName := "devcontainerDockerfile.tmpl"
4646
t := template.Must(template.ParseFS(tmplFS, "tmpl/"+tmplName))
4747
// write content into file
48-
err = t.Execute(file, nil)
49-
if err != nil {
50-
return errors.WithStack(err)
51-
}
52-
return nil
48+
return errors.WithStack(t.Execute(file, nil))
5349
}
5450

5551
// CreateDevcontainer creates a devcontainer.json in path and writes getDevcontainerContent's output into it
@@ -67,10 +63,7 @@ func CreateDevcontainer(path string, pkgs []string) error {
6763
}
6864
// writing devcontainer's content into json file
6965
_, err = file.Write(devcontainerFileBytes)
70-
if err != nil {
71-
return errors.WithStack(err)
72-
}
73-
return nil
66+
return errors.WithStack(err)
7467
}
7568

7669
func CreateEnvrc(tmplFS embed.FS, path string) error {
@@ -83,11 +76,7 @@ func CreateEnvrc(tmplFS embed.FS, path string) error {
8376
tmplName := "envrc.tmpl"
8477
t := template.Must(template.ParseFS(tmplFS, "tmpl/"+tmplName))
8578
// write content into file
86-
err = t.Execute(file, nil)
87-
if err != nil {
88-
return errors.WithStack(err)
89-
}
90-
return nil
79+
return errors.WithStack(t.Execute(file, nil))
9180
}
9281

9382
func getDevcontainerContent(pkgs []string) *devcontainerObject {

internal/boxcli/init.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,5 @@ func runInitCmd(cmd *cobra.Command, args []string) error {
3434
if err != nil {
3535
return errors.WithStack(err)
3636
}
37-
err = box.GenerateEnvrc(false, "init")
38-
if err != nil {
39-
return errors.WithStack(err)
40-
}
41-
return nil
37+
return errors.WithStack(box.GenerateEnvrc(false, "init"))
4238
}

internal/cloud/mutagen/forward.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,5 @@ func ForwardList(env map[string]string, labels map[string]string) ([]Forward, er
3838
}
3939

4040
list := []Forward{}
41-
42-
if err := json.Unmarshal(out, &list); err != nil {
43-
return nil, errors.WithStack(err)
44-
}
45-
4641
return list, errors.WithStack(json.Unmarshal(out, &list))
4742
}

internal/cloud/mutagenbox/mutagenbox.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,5 @@ func createAndGetDataDir() (string, error) {
6262
}
6363

6464
path := filepath.Join(home, dataDirPath)
65-
if err := os.MkdirAll(path, 0700); err != nil {
66-
return "", errors.WithStack(err)
67-
}
68-
return path, nil
65+
return path, errors.WithStack(os.MkdirAll(path, 0700))
6966
}

internal/cloud/openssh/sshshim/generate.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,17 @@ func Setup() error {
3333

3434
// create scp symlink
3535
scpSymlink := filepath.Join(shimDir, "scp")
36-
if err := makeSymlink(scpSymlink, devboxExecutablePath); err != nil {
37-
return errors.WithStack(err)
38-
}
39-
40-
return nil
36+
return errors.WithStack(makeSymlink(scpSymlink, devboxExecutablePath))
4137
}
4238

4339
func makeSymlink(from string, target string) error {
44-
4540
if err := os.Remove(from); err != nil && !os.IsNotExist(err) {
4641
return errors.WithStack(err)
4742
}
4843

49-
if err := os.Symlink(target, from); err != nil {
50-
if !os.IsExist(err) {
51-
return errors.WithStack(err)
52-
}
44+
err := os.Symlink(target, from)
45+
if os.IsExist(err) {
46+
err = nil
5347
}
54-
return nil
48+
return errors.WithStack(err)
5549
}

internal/cloud/openssh/sshshim/mutagen.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ func checkActiveVMWithRetries(vmAddr string) (bool, error) {
7272
}
7373

7474
func checkActiveVM(vmAddr string) (bool, error) {
75-
7675
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*2)
7776
defer cancel()
7877
cmd := exec.CommandContext(ctx, "ssh", vmAddr, "echo 'alive'")

internal/cloud/openssh/username.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ func SaveGithubUsernameToLocalFile(username string) error {
3131
return errors.WithStack(err)
3232
}
3333

34-
err = os.WriteFile(filePath, []byte(username), 0600)
35-
return errors.WithStack(err)
34+
return errors.WithStack(os.WriteFile(filePath, []byte(username), 0600))
3635
}
3736

3837
func usernameFilePath() (string, error) {

internal/cuecfg/cuecfg.go

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,13 @@ func Marshal(valuePtr any, extension string) ([]byte, error) {
3535
func Unmarshal(data []byte, extension string, valuePtr any) error {
3636
switch extension {
3737
case ".json":
38-
err := unmarshalJSON(data, valuePtr)
39-
if err != nil {
40-
return errors.WithStack(err)
41-
}
42-
return nil
38+
return errors.WithStack(unmarshalJSON(data, valuePtr))
4339
case ".yml", ".yaml":
44-
err := unmarshalYaml(data, valuePtr)
45-
if err != nil {
46-
return errors.WithStack(err)
47-
}
48-
return nil
40+
return errors.WithStack(unmarshalYaml(data, valuePtr))
4941
case ".toml":
50-
err := unmarshalToml(data, valuePtr)
51-
if err != nil {
52-
return errors.WithStack(err)
53-
}
54-
return nil
42+
return errors.WithStack(unmarshalToml(data, valuePtr))
5543
case ".xml":
56-
err := unmarshalXML(data, valuePtr)
57-
if err != nil {
58-
return errors.WithStack(err)
59-
}
60-
return nil
44+
return errors.WithStack(unmarshalXML(data, valuePtr))
6145
}
6246
return errors.Errorf("Unsupported file format '%s' for config file", extension)
6347
}
@@ -98,11 +82,7 @@ func WriteFile(path string, value any) error {
9882
return errors.WithStack(err)
9983
}
10084

101-
err = os.WriteFile(path, data, 0644)
102-
if err != nil {
103-
return errors.WithStack(err)
104-
}
105-
return nil
85+
return errors.WithStack(os.WriteFile(path, data, 0644))
10686
}
10787

10888
func IsSupportedExtension(ext string) bool {

0 commit comments

Comments
 (0)