Skip to content

Commit 8d5b521

Browse files
Sisyphus-JuniorSisyphus-Junior
authored andcommitted
fix: 使用 errors.New 替代 fmt.Errorf 以改进错误处理
1 parent 29c618b commit 8d5b521

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

internal/app/app.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (a *App) Run(args []string) {
6363

6464
func (a *App) Execute(args []string) error {
6565
if err := a.validateConfig(); err != nil {
66-
return fmt.Errorf(a.tf("err.invalid_config", err))
66+
return errors.New(a.tf("err.invalid_config", err))
6767
}
6868
cmd := a.newRootCommand()
6969
cmd.SetArgs(args)
@@ -97,7 +97,7 @@ func (a *App) newRootCommand() *cobra.Command {
9797
if st, err := os.Stat(abs); err != nil {
9898
return err
9999
} else if !st.IsDir() {
100-
return fmt.Errorf(a.tf("err.chdir_not_directory", abs))
100+
return errors.New(a.tf("err.chdir_not_directory", abs))
101101
}
102102
return os.Chdir(abs)
103103
}
@@ -182,7 +182,7 @@ func (a *App) newRootCommand() *cobra.Command {
182182
cleanup := &cobra.Command{Use: "cleanup", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error {
183183
testArtifacts, _ := cmd.Flags().GetBool("test-artifacts")
184184
if !testArtifacts {
185-
return fmt.Errorf(a.t("err.cleanup_usage"))
185+
return errors.New(a.t("err.cleanup_usage"))
186186
}
187187
if err := a.cleanup(); err != nil {
188188
return err

internal/app/service.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package app
22

33
import (
4+
"errors"
45
"fmt"
56
"os"
67
"os/exec"
@@ -98,9 +99,9 @@ func (a *App) serviceAction(action, _ string) error {
9899
if err != nil {
99100
return err
100101
}
101-
fmt.Printf(a.tf("service.status_line", serviceName, status))
102+
fmt.Print(a.tf("service.status_line", serviceName, status))
102103
default:
103-
return fmt.Errorf(a.tf("err.service_unsupported_action", action))
104+
return errors.New(a.tf("err.service_unsupported_action", action))
104105
}
105106
a.instanceLog.Infof(a.tf("log.service_action_completed", action))
106107
return nil

internal/app/workspace.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (a *App) workspaceDir(name string) (string, error) {
4949
return "", err
5050
}
5151
if !found {
52-
return "", fmt.Errorf(a.t("err.workspace_not_initialized_run_init"))
52+
return "", errors.New(a.t("err.workspace_not_initialized_run_init"))
5353
}
5454
return dir, nil
5555
}
@@ -189,7 +189,7 @@ func (a *App) startInstance(name string) error {
189189
cfg, err := a.readWorkspaceConfig(selected)
190190
if err != nil {
191191
if errors.Is(err, os.ErrNotExist) {
192-
return fmt.Errorf(a.t("err.workspace_not_initialized_run_init"))
192+
return errors.New(a.t("err.workspace_not_initialized_run_init"))
193193
}
194194
return err
195195
}
@@ -256,7 +256,7 @@ func (a *App) stopInstance(name string) error {
256256
cfg, err := a.readWorkspaceConfig(selected)
257257
if err != nil {
258258
if errors.Is(err, os.ErrNotExist) {
259-
return fmt.Errorf(a.t("err.workspace_not_initialized_run_init"))
259+
return errors.New(a.t("err.workspace_not_initialized_run_init"))
260260
}
261261
return err
262262
}
@@ -288,7 +288,7 @@ func (a *App) statusInstance(name string) error {
288288
cfg, err := a.readWorkspaceConfig(name)
289289
if err != nil {
290290
if errors.Is(err, os.ErrNotExist) {
291-
return fmt.Errorf(a.t("err.workspace_not_initialized"))
291+
return errors.New(a.t("err.workspace_not_initialized"))
292292
}
293293
return err
294294
}
@@ -316,7 +316,7 @@ func (a *App) logsInstance(name string, tail int) error {
316316
data, err := os.ReadFile(logPath)
317317
if err != nil {
318318
if errors.Is(err, os.ErrNotExist) {
319-
return fmt.Errorf(a.t("err.workspace_log_not_found"))
319+
return errors.New(a.t("err.workspace_log_not_found"))
320320
}
321321
return err
322322
}
@@ -356,7 +356,7 @@ func (a *App) updateInstance(name string) error {
356356
cfg, err := a.readWorkspaceConfig(name)
357357
if err != nil {
358358
if errors.Is(err, os.ErrNotExist) {
359-
return fmt.Errorf(a.t("err.workspace_not_initialized_run_init"))
359+
return errors.New(a.t("err.workspace_not_initialized_run_init"))
360360
}
361361
return err
362362
}

0 commit comments

Comments
 (0)