diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-05-12 12:43:20 +0300 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-05-14 14:40:56 +0300 |
commit | 5c96bda70a7afb2ce97cbb3cd70c64fc8cb94446 (patch) | |
tree | 394a557b0dc7db1f6753cf2a09e8cb0577f18442 /resources | |
parent | 4a96df96d958a8ce122f103c4b417eaba52e6cb1 (diff) |
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
Diffstat (limited to 'resources')
5 files changed, 71 insertions, 16 deletions
diff --git a/resources/resource_transformers/js/build.go b/resources/resource_transformers/js/build.go index d2fbf5065..00012b4e8 100644 --- a/resources/resource_transformers/js/build.go +++ b/resources/resource_transformers/js/build.go @@ -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 diff --git a/resources/resource_transformers/js/integration_test.go b/resources/resource_transformers/js/integration_test.go index 0c5b04a51..b9f466873 100644 --- a/resources/resource_transformers/js/integration_test.go +++ b/resources/resource_transformers/js/integration_test.go @@ -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`) + }) + +} diff --git a/resources/resource_transformers/postcss/integration_test.go b/resources/resource_transformers/postcss/integration_test.go index c748f036b..4101818be 100644 --- a/resources/resource_transformers/postcss/integration_test.go +++ b/resources/resource_transformers/postcss/integration_test.go @@ -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") } diff --git a/resources/resource_transformers/postcss/postcss.go b/resources/resource_transformers/postcss/postcss.go index dd7ad1418..a5c86df6f 100644 --- a/resources/resource_transformers/postcss/postcss.go +++ b/resources/resource_transformers/postcss/postcss.go @@ -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 { diff --git a/resources/resource_transformers/tocss/dartsass/transform.go b/resources/resource_transformers/tocss/dartsass/transform.go index 082e30710..be4367b2f 100644 --- a/resources/resource_transformers/tocss/dartsass/transform.go +++ b/resources/resource_transformers/tocss/dartsass/transform.go @@ -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) |