From c425b74383b3455fa81cf1db3b1ca38144cb00d9 Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 28 Sep 2025 10:24:17 +0800 Subject: [PATCH] Refactor error handling in various components to improve logging and message formatting - Updated error messages to use fmt.Errorf with %s for better clarity and consistency. - Enhanced logging statements to utilize formatted strings for improved readability. - Refactored multiple Load functions across different modules to standardize error handling practices. --- api/api.go | 2 +- attachment/load.go | 2 +- cmd/migrate.go | 16 ++++++++-------- cmd/restore.go | 4 ++-- cmd/run.go | 2 +- cmd/start.go | 2 +- cmd/sui/build.go | 2 +- cmd/sui/trans.go | 2 +- cmd/sui/watch.go | 4 ++-- config/config.go | 2 +- importer/importer.go | 2 +- mcp/mcp.go | 2 +- model/model.go | 2 +- pipe/context.go | 2 +- pipe/node.go | 8 ++++---- plugin/plugin.go | 2 +- schedule/schedule.go | 2 +- share/db.go | 4 ++-- share/utils.go | 2 +- store/store.go | 2 +- sui/api/request.go | 2 +- task/task.go | 2 +- widgets/chart/chart.go | 2 +- widgets/dashboard/dashboard.go | 2 +- widgets/form/fields.go | 2 +- widgets/form/form.go | 4 ++-- widgets/list/fields.go | 2 +- widgets/list/list.go | 4 ++-- widgets/table/table.go | 4 ++-- widgets/widgets.go | 2 +- 30 files changed, 46 insertions(+), 46 deletions(-) diff --git a/api/api.go b/api/api.go index 16d2b78f..13945841 100644 --- a/api/api.go +++ b/api/api.go @@ -36,7 +36,7 @@ func Load(cfg config.Config) error { }, exts...) if len(messages) > 0 { - return fmt.Errorf(strings.Join(messages, ";\n")) + return fmt.Errorf("%s", strings.Join(messages, ";\n")) } return err diff --git a/attachment/load.go b/attachment/load.go index 1105256e..e749ebef 100644 --- a/attachment/load.go +++ b/attachment/load.go @@ -49,7 +49,7 @@ func Load(cfg config.Config) error { for _, message := range messages { log.Error("Load filesystem uploaders error: %s", message) } - return fmt.Errorf(strings.Join(messages, ";\n")) + return fmt.Errorf("%s", strings.Join(messages, ";\n")) } return nil diff --git a/cmd/migrate.go b/cmd/migrate.go index 1138ccda..7c90f56a 100644 --- a/cmd/migrate.go +++ b/cmd/migrate.go @@ -56,43 +56,43 @@ var migrateCmd = &cobra.Command{ return } - fmt.Printf(color.WhiteString(L("Update schema model: %s (%s) "), mod.Name, mod.MetaData.Table.Name) + "\t") + fmt.Print(color.WhiteString(fmt.Sprintf(L("Update schema model: %s (%s) "), mod.Name, mod.MetaData.Table.Name)) + "\t") if resetModel { err := mod.DropTable() if err != nil { - fmt.Printf(color.RedString(L("FAILURE\n%s"), err.Error()) + "\n") + fmt.Print(color.RedString(fmt.Sprintf(L("FAILURE\n%s"), err.Error())) + "\n") return } } err := mod.Migrate(false) if err != nil { - fmt.Printf(color.RedString(L("FAILURE\n%s"), err.Error()) + "\n") + fmt.Print(color.RedString(fmt.Sprintf(L("FAILURE\n%s"), err.Error())) + "\n") return } - fmt.Printf(color.GreenString(L("SUCCESS")) + "\n") + fmt.Print(color.GreenString(L("SUCCESS")) + "\n") return } // Do Stuff Here for _, mod := range model.Models { - fmt.Printf(color.WhiteString(L("Update schema model: %s (%s) "), mod.Name, mod.MetaData.Table.Name) + "\t") + fmt.Print(color.WhiteString(fmt.Sprintf(L("Update schema model: %s (%s) "), mod.Name, mod.MetaData.Table.Name)) + "\t") if resetModel { err := mod.DropTable() if err != nil { - fmt.Printf(color.RedString(L("FAILURE\n%s"), err.Error()) + "\n") + fmt.Print(color.RedString(fmt.Sprintf(L("FAILURE\n%s"), err.Error())) + "\n") continue } } err := mod.Migrate(false) if err != nil { - fmt.Printf(color.RedString(L("FAILURE\n%s"), err.Error()) + "\n") + fmt.Print(color.RedString(fmt.Sprintf(L("FAILURE\n%s"), err.Error())) + "\n") continue } - fmt.Printf(color.GreenString(L("SUCCESS")) + "\n") + fmt.Print(color.GreenString(L("SUCCESS")) + "\n") } // After Migrate Hook diff --git a/cmd/restore.go b/cmd/restore.go index eac6c37c..3454f52e 100644 --- a/cmd/restore.go +++ b/cmd/restore.go @@ -125,7 +125,7 @@ func restoreModels(basePath string, migOpts []model.MigrateOption) { // Migrate models for _, mod := range model.Models { fmt.Printf("\r%s", strings.Repeat(" ", 80)) - fmt.Printf(color.GreenString(L("\rUpdate schema model: %s (%s) "), mod.Name, mod.MetaData.Table.Name)) + fmt.Print(color.GreenString(fmt.Sprintf(L("\rUpdate schema model: %s (%s) "), mod.Name, mod.MetaData.Table.Name))) err := mod.Migrate(true, migOpts...) if err != nil { fmt.Println(color.RedString(L("Fatal: %s"), err.Error())) @@ -140,7 +140,7 @@ func restoreModels(basePath string, migOpts []model.MigrateOption) { name := strings.Join(namer[:len(namer)-2], ".") if mod, has := model.Models[name]; has { fmt.Printf("\r%s", strings.Repeat(" ", 80)) - fmt.Printf(color.GreenString(L("\rRestore model: %s (%s) "), mod.Name, mod.MetaData.Table.Name)) + fmt.Print(color.GreenString(fmt.Sprintf(L("\rRestore model: %s (%s) "), mod.Name, mod.MetaData.Table.Name))) err := mod.Import(filepath.Join(basePath, file.Name())) if err != nil { fmt.Println(color.RedString(L("Fatal: %s"), err.Error())) diff --git a/cmd/run.go b/cmd/run.go index 12f56e61..d1ce8c30 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -53,7 +53,7 @@ var runCmd = &cobra.Command{ color.White(share.BUILDNAME + " help\n") return } - fmt.Printf(L("Not enough arguments\n")) + fmt.Print(L("Not enough arguments\n")) return } diff --git a/cmd/start.go b/cmd/start.go index 34bb24cd..20ada54d 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -488,7 +488,7 @@ func printApis(silent bool) { } if len(websocket.Upgraders) > 0 { - fmt.Printf(color.CyanString("\n%s(%d)\n", "WebSocket", len(websocket.Upgraders))) + fmt.Print(color.CyanString(fmt.Sprintf("\n%s(%d)\n", "WebSocket", len(websocket.Upgraders)))) for name, upgrader := range websocket.Upgraders { // WebSocket fmt.Println( colorMehtod("GET"), diff --git a/cmd/sui/build.go b/cmd/sui/build.go index d19c38c1..a3fd069b 100644 --- a/cmd/sui/build.go +++ b/cmd/sui/build.go @@ -54,7 +54,7 @@ var BuildCmd = &cobra.Command{ sui, has := core.SUIs[id] if !has { - fmt.Fprintf(os.Stderr, color.RedString(("the sui " + id + " does not exist"))) + fmt.Fprint(os.Stderr, color.RedString("the sui "+id+" does not exist")) return } sui.WithSid(sid) diff --git a/cmd/sui/trans.go b/cmd/sui/trans.go index 3c753d32..81dfdd56 100644 --- a/cmd/sui/trans.go +++ b/cmd/sui/trans.go @@ -55,7 +55,7 @@ var TransCmd = &cobra.Command{ sui, has := core.SUIs[id] if !has { - fmt.Fprintf(os.Stderr, color.RedString(("the sui " + id + " does not exist"))) + fmt.Fprint(os.Stderr, color.RedString("the sui "+id+" does not exist")) return } sui.WithSid(sid) diff --git a/cmd/sui/watch.go b/cmd/sui/watch.go index 74c77aab..d67bd9fa 100644 --- a/cmd/sui/watch.go +++ b/cmd/sui/watch.go @@ -68,7 +68,7 @@ var WatchCmd = &cobra.Command{ sui, has := core.SUIs[id] if !has { - fmt.Fprintf(os.Stderr, color.RedString(("the sui " + id + " does not exist"))) + fmt.Fprint(os.Stderr, color.RedString("the sui "+id+" does not exist")) return } sui.WithSid(sid) @@ -94,7 +94,7 @@ var WatchCmd = &cobra.Command{ go watch(root, func(event, name string) { if event == "WRITE" || event == "CREATE" || event == "RENAME" { // @Todo build single page and sync single asset file to public - fmt.Printf(color.WhiteString("Building... ")) + fmt.Print(color.WhiteString("Building... ")) tmpl, err := sui.GetTemplate(template) if err != nil { diff --git a/config/config.go b/config/config.go index 105784b8..4998cf9b 100644 --- a/config/config.go +++ b/config/config.go @@ -180,7 +180,7 @@ func CloseLog() { if LogOutput != nil { err := LogOutput.Close() if err != nil { - log.Error(err.Error()) + log.Error("Failed to close log output: %v", err) return } } diff --git a/importer/importer.go b/importer/importer.go index 5b720c31..9e8619a6 100644 --- a/importer/importer.go +++ b/importer/importer.go @@ -461,7 +461,7 @@ func (imp *Importer) Run(src from.Source, mapping *Mapping) interface{} { if imp.Output != "" { res, err := process.New(imp.Output, output).WithSID(imp.Sid).Exec() if err != nil { - log.With(log.F{"output": imp.Output}).Error(err.Error()) + log.With(log.F{"output": imp.Output}).Error("%v", err) return output } return res diff --git a/mcp/mcp.go b/mcp/mcp.go index 527aefc7..6d83c570 100644 --- a/mcp/mcp.go +++ b/mcp/mcp.go @@ -36,7 +36,7 @@ func Load(cfg config.Config) error { for _, message := range messages { log.Error("Load filesystem MCP clients error: %s", message) } - return fmt.Errorf(strings.Join(messages, ";\n")) + return fmt.Errorf("%s", strings.Join(messages, ";\n")) } // Load database MCP clients (ignore error) diff --git a/model/model.go b/model/model.go index 54e0106f..485b087b 100644 --- a/model/model.go +++ b/model/model.go @@ -72,7 +72,7 @@ func Load(cfg config.Config) error { for _, message := range messages { log.Error("Load filesystem models error: %s", message) } - return fmt.Errorf(strings.Join(messages, ";\n")) + return fmt.Errorf("%s", strings.Join(messages, ";\n")) } // Load database models ( ignore error) diff --git a/pipe/context.go b/pipe/context.go index 569d8bc1..a7c9710b 100644 --- a/pipe/context.go +++ b/pipe/context.go @@ -67,7 +67,7 @@ func (ctx *Context) resume(args ...any) (any, error) { node := ctx.current output, err := ctx.parseNodeOutput(node, args) if err != nil { - return nil, node.Errorf(ctx, err.Error()) + return nil, node.Errorf(ctx, "%v", err) } // Next node diff --git a/pipe/node.go b/pipe/node.go index 57cbf5a0..35e2792d 100644 --- a/pipe/node.go +++ b/pipe/node.go @@ -83,12 +83,12 @@ func (node *Node) YaoProcess(ctx *Context, input Input) (any, error) { // Execute the process process, err := process.Of(node.Process.Name, args...) if err != nil { - return nil, node.Errorf(ctx, err.Error()) + return nil, node.Errorf(ctx, "%v", err) } res, err := process.WithGlobal(ctx.global).WithSID(ctx.sid).Exec() if err != nil { - return nil, node.Errorf(ctx, err.Error()) + return nil, node.Errorf(ctx, "%v", err) } output, err := ctx.parseNodeOutput(node, res) @@ -270,12 +270,12 @@ func (node *Node) renderCli(ctx *Context, input Input) (any, error) { lines, err := cli.New(option).Render(input) if err != nil { - return nil, node.Errorf(ctx, err.Error()) + return nil, node.Errorf(ctx, "%v", err) } output, err := ctx.parseNodeOutput(node, lines) if err != nil { - return nil, node.Errorf(ctx, err.Error()) + return nil, node.Errorf(ctx, "%v", err) } return output, nil } diff --git a/plugin/plugin.go b/plugin/plugin.go index 07e830d2..088b8c58 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -43,7 +43,7 @@ func Load(cfg config.Config) error { }) if len(messages) > 0 { - return fmt.Errorf(strings.Join(messages, ";\n")) + return fmt.Errorf("%s", strings.Join(messages, ";\n")) } return err diff --git a/schedule/schedule.go b/schedule/schedule.go index 311708f1..8268c577 100644 --- a/schedule/schedule.go +++ b/schedule/schedule.go @@ -37,7 +37,7 @@ func Load(cfg config.Config) error { }, exts...) if len(messages) > 0 { - return fmt.Errorf(strings.Join(messages, ";\n")) + return fmt.Errorf("%s", strings.Join(messages, ";\n")) } return err } diff --git a/share/db.go b/share/db.go index f5750cd7..af151b60 100644 --- a/share/db.go +++ b/share/db.go @@ -63,8 +63,8 @@ func DBClose() error { if len(messages) > 0 { msg := fmt.Sprintf("[DBClose] %s ", strings.Join(messages, ";")) - log.Error(msg) - return fmt.Errorf(msg) + log.Error("%s", msg) + return fmt.Errorf("%s", msg) } return nil diff --git a/share/utils.go b/share/utils.go index b753d7fc..1b521f41 100644 --- a/share/utils.go +++ b/share/utils.go @@ -20,7 +20,7 @@ func Walk(root string, typeName string, cb func(root, filename string)) error { root = path.Join(root, "/") err := filepath.Walk(root, func(filename string, info os.FileInfo, err error) error { if err != nil { - log.With(log.F{"root": root, "type": typeName, "filename": filename}).Error(err.Error()) + log.With(log.F{"root": root, "type": typeName, "filename": filename}).Error("Walk error: %v", err) return err } if strings.HasSuffix(filename, typeName) { diff --git a/store/store.go b/store/store.go index fa90fc41..c8f7d098 100644 --- a/store/store.go +++ b/store/store.go @@ -74,7 +74,7 @@ func Load(cfg config.Config) error { }, exts...) if len(messages) > 0 { - return fmt.Errorf(strings.Join(messages, ";\n")) + return fmt.Errorf("%s", strings.Join(messages, ";\n")) } return err } diff --git a/sui/api/request.go b/sui/api/request.go index 6b45d5ef..a8f1e9cf 100644 --- a/sui/api/request.go +++ b/sui/api/request.go @@ -96,7 +96,7 @@ func (r *Request) Render() (string, int, error) { message := fmt.Sprintf("[SUI] The page %s is not cached. file=%s DisableCache=%v", r.Request.URL.Path, r.File, r.Request.DisableCache()) go fmt.Println(color.YellowString(message)) - go log.Warn(message) + go log.Warn("%s", message) var status int var err error diff --git a/task/task.go b/task/task.go index d411af56..8b6dcc41 100644 --- a/task/task.go +++ b/task/task.go @@ -37,7 +37,7 @@ func Load(cfg config.Config) error { }, exts...) if len(messages) > 0 { - return fmt.Errorf(strings.Join(messages, ";\n")) + return fmt.Errorf("%s", strings.Join(messages, ";\n")) } return err } diff --git a/widgets/chart/chart.go b/widgets/chart/chart.go index 8dae5de1..e0cd432b 100644 --- a/widgets/chart/chart.go +++ b/widgets/chart/chart.go @@ -79,7 +79,7 @@ func Load(cfg config.Config) error { }, exts...) if len(messages) > 0 { - return fmt.Errorf(strings.Join(messages, ";\n")) + return fmt.Errorf("%s", strings.Join(messages, ";\n")) } return err diff --git a/widgets/dashboard/dashboard.go b/widgets/dashboard/dashboard.go index 23f99d70..c8181c2f 100644 --- a/widgets/dashboard/dashboard.go +++ b/widgets/dashboard/dashboard.go @@ -79,7 +79,7 @@ func Load(cfg config.Config) error { }, exts...) if len(messages) > 0 { - return fmt.Errorf(strings.Join(messages, ";\n")) + return fmt.Errorf("%s", strings.Join(messages, ";\n")) } return err diff --git a/widgets/form/fields.go b/widgets/form/fields.go index 2f5d4fc4..eca3caf1 100644 --- a/widgets/form/fields.go +++ b/widgets/form/fields.go @@ -142,7 +142,7 @@ func (fields *FieldsDSL) Xgen(layout *LayoutDSL) (map[string]interface{}, error) } if len(messages) > 0 { - return nil, fmt.Errorf(strings.Join(messages, ";\n")) + return nil, fmt.Errorf("%s", strings.Join(messages, ";\n")) } res["form"] = forms return res, nil diff --git a/widgets/form/form.go b/widgets/form/form.go index 66c3e2d4..1545f635 100644 --- a/widgets/form/form.go +++ b/widgets/form/form.go @@ -71,7 +71,7 @@ func New(id string, file string, source []byte) *DSL { func LoadAndExport(cfg config.Config) error { err := Load(cfg) if err != nil { - log.Error(err.Error()) + log.Error("%v", err) } return Export() } @@ -102,7 +102,7 @@ func Load(cfg config.Config) error { }, exts...) if len(messages) > 0 { - return fmt.Errorf(strings.Join(messages, ";\n")) + return fmt.Errorf("%s", strings.Join(messages, ";\n")) } return err diff --git a/widgets/list/fields.go b/widgets/list/fields.go index b93a493a..0d04536a 100644 --- a/widgets/list/fields.go +++ b/widgets/list/fields.go @@ -139,7 +139,7 @@ func (fields *FieldsDSL) Xgen(layout *LayoutDSL, query map[string]interface{}) ( } if len(messages) > 0 { - return nil, fmt.Errorf(strings.Join(messages, ";\n")) + return nil, fmt.Errorf("%s", strings.Join(messages, ";\n")) } res["list"] = lists return res, nil diff --git a/widgets/list/list.go b/widgets/list/list.go index 5a3fcf11..cc71657d 100644 --- a/widgets/list/list.go +++ b/widgets/list/list.go @@ -61,7 +61,7 @@ func New(id string) *DSL { func LoadAndExport(cfg config.Config) error { err := Load(cfg) if err != nil { - log.Error(err.Error()) + log.Error("%v", err) } return Export() } @@ -91,7 +91,7 @@ func Load(cfg config.Config) error { }, exts...) if len(messages) > 0 { - return fmt.Errorf(strings.Join(messages, ";\n")) + return fmt.Errorf("%s", strings.Join(messages, ";\n")) } return err diff --git a/widgets/table/table.go b/widgets/table/table.go index 3507f94a..d870413a 100644 --- a/widgets/table/table.go +++ b/widgets/table/table.go @@ -100,7 +100,7 @@ func New(id string, file string, source []byte) *DSL { func LoadAndExport(cfg config.Config) error { err := Export() if err != nil { - log.Error(err.Error()) + log.Error("%v", err) } return Load(cfg) } @@ -131,7 +131,7 @@ func Load(cfg config.Config) error { }, exts...) if len(messages) > 0 { - return fmt.Errorf(strings.Join(messages, ";\n")) + return fmt.Errorf("%s", strings.Join(messages, ";\n")) } return err diff --git a/widgets/widgets.go b/widgets/widgets.go index 3986b5bd..e03c28d7 100644 --- a/widgets/widgets.go +++ b/widgets/widgets.go @@ -83,7 +83,7 @@ func Load(cfg config.Config) error { } if len(messages) > 0 { - err = fmt.Errorf(strings.Join(messages, ";\n")) + err = fmt.Errorf("%s", strings.Join(messages, ";\n")) return err }