mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-26 22:04:32 +02:00
errors: Misc improvements
* Redo the server error template * Always add the content file context if relevant * Remove some now superflous error string matching * Move the server error template to _server/error.html * Add file context (with position) to codeblock render blocks * Improve JS build errors Fixes #9892 Fixes #9891 Fixes #9893
This commit is contained in:
@@ -137,6 +137,12 @@ func (t *buildTransformation) Transform(ctx *resources.ResourceTransformationCtx
|
||||
return errors.New(msg.Text)
|
||||
}
|
||||
path := loc.File
|
||||
if path == stdinImporter {
|
||||
path = ctx.SourcePath
|
||||
}
|
||||
|
||||
errorMessage := msg.Text
|
||||
errorMessage = strings.ReplaceAll(errorMessage, nsImportHugo+":", "")
|
||||
|
||||
var (
|
||||
f afero.File
|
||||
@@ -158,15 +164,16 @@ func (t *buildTransformation) Transform(ctx *resources.ResourceTransformationCtx
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
fe := herrors.NewFileError(path, errors.New(msg.Text)).
|
||||
fe := herrors.
|
||||
NewFileError(path, errors.New(errorMessage)).
|
||||
UpdatePosition(text.Position{Offset: -1, LineNumber: loc.Line, ColumnNumber: loc.Column}).
|
||||
UpdateContent(f, herrors.SimpleLineMatcher)
|
||||
UpdateContent(f, nil)
|
||||
|
||||
f.Close()
|
||||
return fe
|
||||
}
|
||||
|
||||
return fmt.Errorf("%s", msg.Text)
|
||||
return fmt.Errorf("%s", errorMessage)
|
||||
}
|
||||
|
||||
var errors []error
|
||||
|
@@ -209,3 +209,53 @@ TS2: {{ template "print" $ts2 }}
|
||||
function greeter(person) {
|
||||
`)
|
||||
}
|
||||
|
||||
func TestBuildError(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
|
||||
filesTemplate := `
|
||||
-- config.toml --
|
||||
disableKinds=["page", "section", "taxonomy", "term", "sitemap", "robotsTXT"]
|
||||
-- assets/js/main.js --
|
||||
// A comment.
|
||||
import { hello1, hello2 } from './util1';
|
||||
hello1();
|
||||
hello2();
|
||||
-- assets/js/util1.js --
|
||||
/* Some
|
||||
comments.
|
||||
*/
|
||||
import { hello3 } from './util2';
|
||||
export function hello1() {
|
||||
return 'abcd';
|
||||
}
|
||||
export function hello2() {
|
||||
return hello3();
|
||||
}
|
||||
-- assets/js/util2.js --
|
||||
export function hello3() {
|
||||
return 'efgh';
|
||||
}
|
||||
-- layouts/index.html --
|
||||
{{ $js := resources.Get "js/main.js" | js.Build }}
|
||||
JS Content:{{ $js.Content }}:End:
|
||||
|
||||
`
|
||||
|
||||
c.Run("Import from main not found", func(c *qt.C) {
|
||||
c.Parallel()
|
||||
files := strings.Replace(filesTemplate, "import { hello1, hello2 }", "import { hello1, hello2, FOOBAR }", 1)
|
||||
b, err := hugolib.NewIntegrationTestBuilder(hugolib.IntegrationTestConfig{T: c, NeedsOsFS: true, TxtarString: files}).BuildE()
|
||||
b.Assert(err, qt.IsNotNil)
|
||||
b.Assert(err.Error(), qt.Contains, `main.js:2:25": No matching export`)
|
||||
})
|
||||
|
||||
c.Run("Import from import not found", func(c *qt.C) {
|
||||
c.Parallel()
|
||||
files := strings.Replace(filesTemplate, "import { hello3 } from './util2';", "import { hello3, FOOBAR } from './util2';", 1)
|
||||
b, err := hugolib.NewIntegrationTestBuilder(hugolib.IntegrationTestConfig{T: c, NeedsOsFS: true, TxtarString: files}).BuildE()
|
||||
b.Assert(err, qt.IsNotNil)
|
||||
b.Assert(err.Error(), qt.Contains, `util1.js:4:17": No matching export in`)
|
||||
})
|
||||
|
||||
}
|
||||
|
@@ -22,7 +22,6 @@ import (
|
||||
jww "github.com/spf13/jwalterweatherman"
|
||||
|
||||
qt "github.com/frankban/quicktest"
|
||||
"github.com/gohugoio/hugo/common/herrors"
|
||||
"github.com/gohugoio/hugo/htesting"
|
||||
"github.com/gohugoio/hugo/hugolib"
|
||||
)
|
||||
@@ -116,7 +115,7 @@ func TestTransformPostCSS(t *testing.T) {
|
||||
|
||||
b.AssertFileContent("public/index.html", `
|
||||
Styles RelPermalink: /css/styles.css
|
||||
Styles Content: Len: 770875|
|
||||
Styles Content: Len: 770917|
|
||||
`)
|
||||
|
||||
}
|
||||
@@ -138,13 +137,6 @@ func TestTransformPostCSSError(t *testing.T) {
|
||||
}).BuildE()
|
||||
|
||||
s.AssertIsFileError(err)
|
||||
fe := herrors.UnwrapFileError(err)
|
||||
pos := fe.Position()
|
||||
c.Assert(strings.TrimPrefix(pos.Filename, s.H.WorkingDir), qt.Equals, filepath.FromSlash("/assets/css/components/a.css"))
|
||||
c.Assert(pos.LineNumber, qt.Equals, 4)
|
||||
errctx := fe.ErrorContext()
|
||||
c.Assert(errctx, qt.IsNotNil)
|
||||
c.Assert(errctx.Lines, qt.DeepEquals, []string{"/* Another comment. */", "class-in-a {", "\t@apply foo;", "}", ""})
|
||||
c.Assert(errctx.ChromaLexer, qt.Equals, "css")
|
||||
c.Assert(err.Error(), qt.Contains, "a.css:4:2")
|
||||
|
||||
}
|
||||
|
@@ -368,7 +368,8 @@ func (imp *importResolver) shouldImport(s string) bool {
|
||||
}
|
||||
|
||||
func (imp *importResolver) toFileError(output string) error {
|
||||
inErr := errors.New(strings.TrimSpace(output))
|
||||
output = strings.TrimSpace(loggers.RemoveANSIColours(output))
|
||||
inErr := errors.New(output)
|
||||
|
||||
match := cssSyntaxErrorRe.FindStringSubmatch(output)
|
||||
if match == nil {
|
||||
|
@@ -116,8 +116,13 @@ func (t *transform) Transform(ctx *resources.ResourceTransformationCtx) error {
|
||||
filename = filename[len(stdinPrefix):]
|
||||
}
|
||||
|
||||
offsetMatcher := func(m herrors.LineMatcher) bool {
|
||||
return m.Offset+len(m.Line) >= start.Offset && strings.Contains(m.Line, context)
|
||||
offsetMatcher := func(m herrors.LineMatcher) int {
|
||||
if m.Offset+len(m.Line) >= start.Offset && strings.Contains(m.Line, context) {
|
||||
// We found the line, but return 0 to signal that we want to determine
|
||||
// the column from the error.
|
||||
return 0
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
return herrors.NewFileErrorFromFile(sassErr, filename, filename, hugofs.Os, offsetMatcher)
|
||||
|
Reference in New Issue
Block a user