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.
This commit is contained in:
Max 2025-09-28 10:24:17 +08:00
parent ffc666c4de
commit c425b74383
30 changed files with 46 additions and 46 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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()))

View file

@ -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
}

View file

@ -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"),

View file

@ -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)

View file

@ -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)

View file

@ -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 {

View file

@ -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
}
}

View file

@ -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

View file

@ -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)

View file

@ -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)

View file

@ -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

View file

@ -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
}

View file

@ -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

View file

@ -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
}

View file

@ -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

View file

@ -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) {

View file

@ -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
}

View file

@ -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

View file

@ -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
}

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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
}