task/errors/error_taskfile_decode.go
Valentin Maerten 4571284f62
fix(taskfile): find decode errors wrapped by yaml v4
yaml v4 collects errors returned by UnmarshalYAML hooks into
*yaml.LoadErrors, which errors.As cannot unwrap. A Taskfile with a
wrongly typed field lost its snippet and file location, and exited
with code 109 instead of 102.
2026-11-09 11:42:26 +01:00

143 lines
3.6 KiB
Go

package errors
import (
"bytes"
"cmp"
"errors"
"fmt"
"strings"
"github.com/fatih/color"
"go.yaml.in/yaml/v4"
)
type (
TaskfileDecodeError struct {
Message string
Location string
Line int
Column int
Tag string
Snippet string
Err error
}
)
func NewTaskfileDecodeError(err error, node *yaml.Node) *TaskfileDecodeError {
// If the error is already a DecodeError, return it
if taskfileInvalidErr, ok := AsTaskfileDecodeError(err); ok {
return taskfileInvalidErr
}
return &TaskfileDecodeError{
Line: node.Line,
Column: node.Column,
Tag: node.ShortTag(),
Err: err,
}
}
// AsTaskfileDecodeError finds a TaskfileDecodeError in err's chain. yaml v4
// collects errors returned by UnmarshalYAML hooks into *yaml.LoadErrors,
// which errors.As cannot see through.
func AsTaskfileDecodeError(err error) (*TaskfileDecodeError, bool) {
if decodeErr, ok := errors.AsType[*TaskfileDecodeError](err); ok {
return decodeErr, true
}
if loadErrs, ok := errors.AsType[*yaml.LoadErrors](err); ok {
for _, loadErr := range loadErrs.Errors {
if decodeErr, ok := errors.AsType[*TaskfileDecodeError](loadErr); ok {
return decodeErr, true
}
}
}
return nil, false
}
func (err *TaskfileDecodeError) Error() string {
buf := &bytes.Buffer{}
// Print the error message
if err.Message != "" {
fmt.Fprintln(buf, color.RedString("err: %s", err.Message))
} else {
// Extract the errors from the TypeError. LoadErrors synthesizes it with
// the compact "line N: message" entries, which LoadError.Error() lacks.
if te, ok := errors.AsType[*yaml.TypeError](err.Err); ok { //nolint:staticcheck // see above
if len(te.Errors) > 1 {
fmt.Fprintln(buf, color.RedString("errs:"))
for _, message := range te.Errors {
fmt.Fprintln(buf, color.RedString("- %s", message))
}
} else {
fmt.Fprintln(buf, color.RedString("err: %s", te.Errors[0]))
}
} else {
// Otherwise print the error message normally
fmt.Fprintln(buf, color.RedString("err: %s", err.Err))
}
}
fmt.Fprintln(buf, color.RedString("file: %s:%d:%d", err.Location, err.Line, err.Column))
fmt.Fprint(buf, err.Snippet)
return buf.String()
}
func (err *TaskfileDecodeError) Debug() string {
const indentWidth = 2
buf := &bytes.Buffer{}
fmt.Fprintln(buf, "TaskfileDecodeError:")
// Recursively loop through the error chain and print any details
var debug func(error, int)
debug = func(err error, indent int) {
indentStr := strings.Repeat(" ", indent*indentWidth)
// Nothing left to unwrap
if err == nil {
fmt.Fprintf(buf, "%sEnd of chain\n", indentStr)
return
}
// Taskfile decode error
decodeErr := &TaskfileDecodeError{}
if errors.As(err, &decodeErr) {
fmt.Fprintf(buf, "%s%s (%s:%d:%d)\n",
indentStr,
cmp.Or(decodeErr.Message, "<no_message>"),
decodeErr.Location,
decodeErr.Line,
decodeErr.Column,
)
debug(errors.Unwrap(err), indent+1)
return
}
fmt.Fprintf(buf, "%s%s\n", indentStr, err)
debug(errors.Unwrap(err), indent+1)
}
debug(err, 0)
return buf.String()
}
func (err *TaskfileDecodeError) Unwrap() error {
return err.Err
}
func (err *TaskfileDecodeError) Code() int {
return CodeTaskfileDecode
}
func (err *TaskfileDecodeError) WithMessage(format string, a ...any) *TaskfileDecodeError {
err.Message = fmt.Sprintf(format, a...)
return err
}
func (err *TaskfileDecodeError) WithTypeMessage(t string) *TaskfileDecodeError {
err.Message = fmt.Sprintf("cannot unmarshal %s into %s", err.Tag, t)
return err
}
func (err *TaskfileDecodeError) WithFileInfo(location string, snippet string) *TaskfileDecodeError {
err.Location = location
err.Snippet = snippet
return err
}