From 47064422a982dd08f87c3c747fb23591bdaa2680 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 24 Jul 2024 08:15:50 +0800 Subject: [PATCH] Optimize error handling in SUI core build and compile functions --- sui/core/build.go | 9 ++++++--- sui/core/compile.go | 4 ++-- sui/storages/local/build.go | 13 ++++++++----- sui/storages/local/page.go | 6 +++--- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/sui/core/build.go b/sui/core/build.go index c521713c..c8136361 100644 --- a/sui/core/build.go +++ b/sui/core/build.go @@ -140,7 +140,7 @@ func (page *Page) BuildAsComponent(sel *goquery.Selection, ctx *BuildContext, op name, exists := sel.Attr("is") if !exists { - return "", fmt.Errorf("The component tag must have an is attribute") + return "", fmt.Errorf("The component %s tag must have an is attribute", page.Route) } namespace := Namespace(name, ctx.sequence, option.ScriptMinify) @@ -488,7 +488,7 @@ func (page *Page) buildComponents(doc *goquery.Document, ctx *BuildContext, opti sel.SetAttr("parsed", "true") ipage, err := tmpl.Page(name) if err != nil { - message := err.Error() + message := fmt.Sprintf("%s on page %s", err.Error(), page.Route) warnings = append(warnings, message) setError(sel, err) return @@ -496,7 +496,7 @@ func (page *Page) buildComponents(doc *goquery.Document, ctx *BuildContext, opti err = ipage.Load() if err != nil { - message := err.Error() + message := fmt.Sprintf("%s on page %s", err.Error(), page.Route) warnings = append(warnings, message) setError(sel, err) return @@ -690,6 +690,9 @@ func (page *Page) BuildHTML(option *BuildOption) (string, error) { func setError(sel *goquery.Selection, err error) { html := `
%s
` sel.SetHtml(fmt.Sprintf(html, err.Error())) + if sel.Nodes != nil || len(sel.Nodes) > 0 { + sel.Nodes[0].Data = "Error" + } } func addTabToEachLine(input string, prefix ...string) string { diff --git a/sui/core/compile.go b/sui/core/compile.go index b13bbe34..83986c33 100644 --- a/sui/core/compile.go +++ b/sui/core/compile.go @@ -23,7 +23,7 @@ func (page *Page) Compile(ctx *BuildContext, option *BuildOption) (string, []str doc, warnings, err := page.Build(ctx, option) if err != nil { - return "", warnings, err + return "", warnings, fmt.Errorf("Page build error: %s", err.Error()) } if warnings != nil && len(warnings) > 0 { @@ -105,7 +105,7 @@ func (page *Page) Compile(ctx *BuildContext, option *BuildOption) (string, []str page.ReplaceDocument(doc) html, err := doc.Html() if err != nil { - return "", warnings, err + return "", warnings, fmt.Errorf("Generate html error: %s", err.Error()) } // @todo: Minify the html diff --git a/sui/storages/local/build.go b/sui/storages/local/build.go index 535dbf6f..c9909820 100644 --- a/sui/storages/local/build.go +++ b/sui/storages/local/build.go @@ -324,13 +324,13 @@ func (page *Page) Build(globalCtx *core.GlobalBuildContext, option *core.BuildOp html, warnings, err := page.Page.Compile(ctx, option) if err != nil { - return warnings, err + return warnings, fmt.Errorf("Compile the page %s error: %s", page.Route, err.Error()) } // Save the html err = page.writeHTML([]byte(html), option.Data) if err != nil { - return warnings, err + return warnings, fmt.Errorf("Write the page %s error: %s", page.Route, err.Error()) } // Save the locale files @@ -343,11 +343,10 @@ func (page *Page) Build(globalCtx *core.GlobalBuildContext, option *core.BuildOp if globalCtx == nil { jitComponents, err := page.tmpl.GlobRoutes(ctx.GetJitComponents(), true) if err != nil { - return warnings, err + return warnings, fmt.Errorf("Glob the jit components error: %s", err.Error()) } for _, route := range jitComponents { - var err error p := page.tmpl.loaded[route] if p == nil { p, err = page.tmpl.Page(route) @@ -365,9 +364,13 @@ func (page *Page) Build(globalCtx *core.GlobalBuildContext, option *core.BuildOp warnings = append(warnings, messages...) } } + + if err != nil { + return warnings, fmt.Errorf("Build the page %s error: %s", page.Route, err.Error()) + } } - return warnings, err + return warnings, nil } diff --git a/sui/storages/local/page.go b/sui/storages/local/page.go index 36b334b0..aa8caf7a 100644 --- a/sui/storages/local/page.go +++ b/sui/storages/local/page.go @@ -162,7 +162,7 @@ func (tmpl *Template) Page(route string) (core.IPage, error) { return page, nil } } - return nil, fmt.Errorf("Page %s not found", route) + return nil, fmt.Errorf("%s not found", route) } // PageExist check if the page exist @@ -661,7 +661,7 @@ func (page *Page) AssetScript() (*core.Asset, error) { }, nil } - return nil, fmt.Errorf("Page %s script not found", page.Route) + return nil, fmt.Errorf("%s script not found", page.Route) } // AssetStyle get the style @@ -683,5 +683,5 @@ func (page *Page) AssetStyle() (*core.Asset, error) { Content: cssCode, }, nil } - return nil, fmt.Errorf("Page %s style not found", page.Route) + return nil, fmt.Errorf("%s style not found", page.Route) }