diff options
author | Bjørn Erik Pedersen <[email protected]> | 2022-05-02 16:07:52 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2022-05-06 19:43:22 +0200 |
commit | f2946da9e806c2bafbdd26707fe339db79bd980b (patch) | |
tree | b5609317a861ea5f399e094e1b9287ca71dc22d1 /parser | |
parent | 6eea32bd6bc8e7a7dd07a8cb6a8343ae2c74aba0 (diff) | |
download | hugo-f2946da9e806c2bafbdd26707fe339db79bd980b.tar.gz hugo-f2946da9e806c2bafbdd26707fe339db79bd980b.zip |
Improve error messages, esp. when the server is running
* Add file context to minifier errors when publishing
* Misc fixes (see issues)
* Allow custom server error template in layouts/server/error.html
To get to this, this commit also cleans up and simplifies the code surrounding errors and files. This also removes the usage of `github.com/pkg/errors`, mostly because of https://github.com/pkg/errors/issues/223 -- but also because most of this is now built-in to Go.
Fixes #9852
Fixes #9857
Fixes #9863
Diffstat (limited to 'parser')
-rw-r--r-- | parser/metadecoders/decoder.go | 19 | ||||
-rw-r--r-- | parser/pageparser/pageparser.go | 4 |
2 files changed, 11 insertions, 12 deletions
diff --git a/parser/metadecoders/decoder.go b/parser/metadecoders/decoder.go index 5bf96b2d8..fe0581734 100644 --- a/parser/metadecoders/decoder.go +++ b/parser/metadecoders/decoder.go @@ -26,7 +26,6 @@ import ( xml "github.com/clbanning/mxj/v2" toml "github.com/pelletier/go-toml/v2" - "github.com/pkg/errors" "github.com/spf13/afero" "github.com/spf13/cast" jww "github.com/spf13/jwalterweatherman" @@ -74,7 +73,7 @@ func (d Decoder) UnmarshalToMap(data []byte, f Format) (map[string]any, error) { func (d Decoder) UnmarshalFileToMap(fs afero.Fs, filename string) (map[string]any, error) { format := FormatFromString(filename) if format == "" { - return nil, errors.Errorf("%q is not a valid configuration format", filename) + return nil, fmt.Errorf("%q is not a valid configuration format", filename) } data, err := afero.ReadFile(fs, filename) @@ -106,7 +105,7 @@ func (d Decoder) UnmarshalStringTo(data string, typ any) (any, error) { case float64: return cast.ToFloat64E(data) default: - return nil, errors.Errorf("unmarshal: %T not supported", typ) + return nil, fmt.Errorf("unmarshal: %T not supported", typ) } } @@ -144,7 +143,7 @@ func (d Decoder) UnmarshalTo(data []byte, f Format, v any) error { if err == nil { xmlRootName, err := xmlRoot.Root() if err != nil { - return toFileError(f, errors.Wrap(err, "failed to unmarshal XML")) + return toFileError(f, data, fmt.Errorf("failed to unmarshal XML: %w", err)) } xmlValue = xmlRoot[xmlRootName].(map[string]any) } @@ -160,7 +159,7 @@ func (d Decoder) UnmarshalTo(data []byte, f Format, v any) error { case YAML: err = yaml.Unmarshal(data, v) if err != nil { - return toFileError(f, errors.Wrap(err, "failed to unmarshal YAML")) + return toFileError(f, data, fmt.Errorf("failed to unmarshal YAML: %w", err)) } // To support boolean keys, the YAML package unmarshals maps to @@ -191,14 +190,14 @@ func (d Decoder) UnmarshalTo(data []byte, f Format, v any) error { return d.unmarshalCSV(data, v) default: - return errors.Errorf("unmarshal of format %q is not supported", f) + return fmt.Errorf("unmarshal of format %q is not supported", f) } if err == nil { return nil } - return toFileError(f, errors.Wrap(err, "unmarshal failed")) + return toFileError(f, data, fmt.Errorf("unmarshal failed: %w", err)) } func (d Decoder) unmarshalCSV(data []byte, v any) error { @@ -215,7 +214,7 @@ func (d Decoder) unmarshalCSV(data []byte, v any) error { case *any: *v.(*any) = records default: - return errors.Errorf("CSV cannot be unmarshaled into %T", v) + return fmt.Errorf("CSV cannot be unmarshaled into %T", v) } @@ -260,8 +259,8 @@ func (d Decoder) unmarshalORG(data []byte, v any) error { return nil } -func toFileError(f Format, err error) error { - return herrors.ToFileError(string(f), err) +func toFileError(f Format, data []byte, err error) error { + return herrors.NewFileError(fmt.Sprintf("_stream.%s", f), err).UpdateContent(bytes.NewReader(data), nil) } // stringifyMapKeys recurses into in and changes all instances of diff --git a/parser/pageparser/pageparser.go b/parser/pageparser/pageparser.go index 3338e063e..bfbedcf26 100644 --- a/parser/pageparser/pageparser.go +++ b/parser/pageparser/pageparser.go @@ -15,11 +15,11 @@ package pageparser import ( "bytes" + "fmt" "io" "io/ioutil" "github.com/gohugoio/hugo/parser/metadecoders" - "github.com/pkg/errors" ) // Result holds the parse result. @@ -102,7 +102,7 @@ func ParseMain(r io.Reader, cfg Config) (Result, error) { func parseSection(r io.Reader, cfg Config, start stateFunc) (Result, error) { b, err := ioutil.ReadAll(r) if err != nil { - return nil, errors.Wrap(err, "failed to read page content") + return nil, fmt.Errorf("failed to read page content: %w", err) } return parseBytes(b, cfg, start) } |