Skip to content

Commit

Permalink
Merge pull request #706 from trheyi/main
Browse files Browse the repository at this point in the history
Optimize error handling in SUI core build and compile functions
  • Loading branch information
trheyi authored Jul 24, 2024
2 parents 4823402 + 8269dcf commit 13a98b8
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 15 deletions.
1 change: 1 addition & 0 deletions cmd/sui/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ var WatchCmd = &cobra.Command{
}

if len(warnings) > 0 {
fmt.Fprintln(os.Stderr, color.YellowString("\nWarnings:"))
for _, warning := range warnings {
fmt.Fprintln(os.Stderr, color.YellowString(warning))
}
Expand Down
9 changes: 6 additions & 3 deletions sui/core/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -488,15 +488,15 @@ 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
}

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
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions sui/core/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions sui/storages/local/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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

}

Expand Down
6 changes: 3 additions & 3 deletions sui/storages/local/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
4 changes: 2 additions & 2 deletions sui/storages/local/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestTemplatePageTS(t *testing.T) {
assert.NotEmpty(t, page.Codes.DATA.Code)

_, err = tmpl.Page("/the/page/could/not/be/found")
assert.Contains(t, err.Error(), "Page /the/page/could/not/be/found not found")
assert.Contains(t, err.Error(), "/the/page/could/not/be/found not found")
}

func TestTemplatePageJS(t *testing.T) {
Expand Down Expand Up @@ -144,7 +144,7 @@ func TestTemplatePageJS(t *testing.T) {
assert.NotEmpty(t, page.Codes.DATA.Code)

_, err = tmpl.Page("/the/page/could/not/be/found")
assert.Contains(t, err.Error(), "Page /the/page/could/not/be/found not found")
assert.Contains(t, err.Error(), "/the/page/could/not/be/found not found")
}

func TestPageSaveTempBoard(t *testing.T) {
Expand Down

0 comments on commit 13a98b8

Please sign in to comment.