La referencia de API proviene de los comentarios de documentación de Go en el código fuente y está redactada en inglés.
package loaderr
import "github.com/nsgo-dev/game/pkg/loaderr"Resumen
Package loaderr is the error that every loader and validator in this module returns for a file it cannot use: which file, which line and column, which field, what was wrong, what was expected and how to fix it, so a person or an agent reading the message can act on it without opening the loader's source.
A validator that walks a decoded document does not know lines, so it names the field instead, with Errorf and a path built by Path ("layers[2].objects[0].id"). The loader's entry point then passes every error through Locate with the file name and the bytes it decoded: Locate finds the field's line and column in the YAML or JSON text, turns encoding/json and YAML decode errors (syntax, unknown fields, wrong types) into an Error with their line and a fix, and fills in the file name. A loader that is handed bytes rather than a path does not know the file's name; the caller that read the file names it with WithFile.
See docs/en/errors.md for the guideline loaders follow.
Índice
- Constantes
- func FindField(data []byte, field string) (line, column int)
- func Join(base string, parts ...any) string
- func Locate(err error, file string, data []byte, fix string) error
- func LocateJSON(err error, file string, data []byte, fix string) error
- func Path(parts ...any) string
- func WithFile(err error, file string) error
- type Error
Constantes
const (
FixSyntax = "correct the syntax at this position; the rest of the file is not read until it parses"
FixUnknownField = "remove the field or correct its spelling; this format refuses fields it does not define"
FixType = "write a value of the expected type"
FixDuplicateKey = "remove one of the two keys; a key may appear once in a mapping"
FixTruncated = "complete the document; it ends in the middle of a value, as a file cut short does"
)Fixes Locate gives the decode errors it recognises.
Funciones
func FindField(data []byte, field string) (line, column int)FindField returns the 1-based line and column of the value at field (a path as Path writes it) in data, a YAML or JSON document. For a mapping key it is the key's position. When field is not in data it returns the position of the nearest enclosing value that is, the document's start for an empty field, and 0, 0 when data does not parse.
func Join(base string, parts ...any) stringJoin returns the path base, as Path writes it, followed by parts, so a validator can extend the path of the value it was handed: Join("layers[2]", "objects", 0) is "layers[2].objects[0]".
func Locate(err error, file string, data []byte, fix string) errorLocate returns err as an *Error that names file and, when data holds the text the loader decoded, the line and column of the problem. Loaders call it once on every error their entry point returns.
- An *Error in err's chain is copied with File set to file when it has none, Line and Column set from where its Field is in data, and Fix set to fix when it has none. When the field itself is missing from data, the line is that of the nearest enclosing value that is there. The copy keeps that Error's Problem and Err, not the text of errors wrapping it.
- A decode error from encoding/json (syntax, unknown field, wrong type) or from YAML (syntax, unknown field, wrong type, duplicate key, an unexpected end) becomes an Error at its line and column, with the field it was in, when known, and a fix for its kind.
- Any other error becomes an Error that names file, with fix as its Fix and err as its Err, at line 0.
Locate returns nil for a nil err.
func LocateJSON(err error, file string, data []byte, fix string) errorLocateJSON is like Locate for loaders that only decode JSON, XML, or CSV documents and never YAML. Using LocateJSON lets dead-code elimination omit the YAML parser from WebAssembly builds that do not load YAML files.
func Path(parts ...any) stringPath builds a field path from keys and indexes: a string is a key and an int an index, so Path("layers", 2, "objects", 0, "id") is "layers[2].objects[0].id". A key that is empty or holds '.', '[', ']' or '"' is written quoted in brackets, as in `tilemaps["a.b"]`.
func WithFile(err error, file string) errorWithFile returns a copy of err with File set to file, when err is itself an *Error whose File is empty. Any other error, including one that only wraps an *Error, is returned unchanged, so the context and sentinels around it still match with errors.Is; so is nil.
A caller that read a file and handed its bytes to a loader, whose entry point returns an *Error, uses it to name the file in the loader's errors.
Tipos
type Error struct {
File string // the file's path or name, empty until a loader or its caller knows it
Line int // 1-based line in File, 0 when unknown
Column int // 1-based column in Line, 0 when unknown
Field string // where in the document, as Path writes it ("layers[2].space"), empty for the document as a whole
Problem string // what is wrong, as the loader says it
Expected string // what the loader expected there, empty when Problem already says it
Fix string // what to change so the file loads
Err error // the underlying error, if any
}Error describes one problem in a file a loader read: where it is, what is wrong and how to fix it.
Its message reads "file:line:column: field: problem; expected E; fix: F", leaving out the parts it does not know. Line and Column are 1-based and 0 when the problem is not at one place in the text, such as a file that cannot be read.
func Errorf(field, fix, format string, args ...any) *ErrorErrorf returns an Error for the value at field whose Problem is format formatted with args, as fmt.Errorf formats it, and whose fix is fix. A %w verb keeps the wrapped error as Err; with two or more, Err is the formatted error, which wraps them all, so errors.Is and errors.As find each one.
func (e *Error) Error() stringError returns the message described on Error.
func (e *Error) Unwrap() errorUnwrap returns the underlying error, so errors.Is and errors.As see through an Error.