Optimize error handling in SUI core build and compile functions

This commit is contained in:
Max 2024-07-24 08:15:50 +08:00
parent 80239284de
commit 47064422a9
4 changed files with 19 additions and 13 deletions

View file

@ -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 := `<div style="color:red; margin:10px 0px; font-size: 12px; font-family: monospace; padding: 10px; border: 1px solid red; background-color: #f8d7da;">%s</div>`
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 {

View file

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

View file

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

View file

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