Merge pull request #706 from trheyi/main
Optimize error handling in SUI core build and compile functions
This commit is contained in:
commit
13a98b8ad7
6 changed files with 22 additions and 15 deletions
|
|
@ -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))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue