package errors
import "cuelang.org/go/cue/errors"
Package errors defines shared types for handling CUE errors.
The pivotal error type in CUE packages is the interface type Error.
The information available in such errors can be most easily retrieved using
the Path, Positions, and Print functions.
Code:play
Output:Example¶
package main
import (
"fmt"
"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/errors"
)
func main() {
v := cuecontext.New().CompileString(`
a: string & 123
b: int & "foo"
`, cue.Filename("input.cue"))
err := v.Validate()
// [error.Error] only shows the first error encountered.
fmt.Printf("string via the Error method:\n %q\n\n", err.Error())
// [errors.Errors] allows listing all the errors encountered.
fmt.Printf("list via errors.Errors:\n")
for _, e := range errors.Errors(err) {
fmt.Printf(" * %s\n", e)
}
fmt.Printf("\n")
// [errors.Positions] lists the positions of all errors encountered.
fmt.Printf("positions via errors.Positions:\n")
for _, pos := range errors.Positions(err) {
fmt.Printf(" * %s\n", pos)
}
fmt.Printf("\n")
// [errors.Details] renders a human-friendly description of all errors like cmd/cue does.
fmt.Printf("human-friendly string via errors.Details:\n")
fmt.Println(errors.Details(err, nil))
}
string via the Error method:
"a: conflicting values string and 123 (mismatched types string and int) (and 1 more errors)"
list via errors.Errors:
* a: conflicting values string and 123 (mismatched types string and int)
* b: conflicting values int and "foo" (mismatched types int and string)
positions via errors.Positions:
* input.cue:2:6
* input.cue:2:15
human-friendly string via errors.Details:
a: conflicting values string and 123 (mismatched types string and int):
input.cue:2:6
input.cue:2:15
b: conflicting values int and "foo" (mismatched types int and string):
input.cue:3:6
input.cue:3:12
Index ¶
- func As(err error, target interface{}) bool
- func Details(err error, cfg *Config) string
- func Is(err, target error) bool
- func New(msg string) error
- func Path(err error) []string
- func Positions(err error) []token.Pos
- func Print(w io.Writer, err error, cfg *Config)
- func String(err Error) string
- func Unwrap(err error) error
- type Config
- type Error
- func Append(a, b Error) Error
- func Errors(err error) []Error
- func Newf(p token.Pos, format string, args ...interface{}) Error
- func Promote(err error, msg string) Error
- func Sanitize(err Error) Error
- func Wrap(parent Error, child error) Error
- func Wrapf(err error, p token.Pos, format string, args ...interface{}) Error
- type Message
Examples ¶
Functions ¶
func As ¶
As finds the first error in err's chain that matches the type to which target points, and if so, sets the target to its value and returns true. An error matches a type if it is assignable to the target type, or if it has a method As(interface{}) bool such that As(target) returns true. As will panic if target is not a non-nil pointer to a type which implements error or is of interface type.
The As method should set the target to its value and return true if err matches the type to which target points.
func Details ¶
Details is a convenience wrapper for Print to return the error text as a string.
func Is ¶
Is reports whether any error in err's chain matches target.
An error is considered to match a target if it is equal to that target or if it implements a method Is(error) bool such that Is(target) returns true.
func New ¶
New is a convenience wrapper for errors.New in the core library. It does not return a CUE error.
func Path ¶
Path returns the path of an Error if err is of that type.
func Positions ¶
Positions returns all positions returned by an error, sorted by relevance when possible and with duplicates removed.
func Print ¶
Print is a utility function that prints a list of errors to w, one error per line, if the err parameter is an List. Otherwise it prints the err string.
func String ¶
String generates a short message from a given Error.
func Unwrap ¶
Unwrap returns the result of calling the Unwrap method on err, if err implements Unwrap. Otherwise, Unwrap returns nil.
Types ¶
type Config ¶
type Config struct { // Format formats the given string and arguments and writes it to w. // It is used for all printing. Format func(w io.Writer, format string, args ...interface{}) // Cwd is the current working directory. Filename positions are taken // relative to this path. Cwd string // ToSlash sets whether to use Unix paths. Mostly used for testing. ToSlash bool }
A Config defines parameters for printing.
type Error ¶
type Error interface { // Position returns the primary position of an error. If multiple positions // contribute equally, this reflects one of them. Position() token.Pos // InputPositions reports positions that contributed to an error, including // the expressions resulting in the conflict, as well as values that were // the input to this expression. InputPositions() []token.Pos // Error reports the error message without position information. Error() string // Path returns the path into the data tree where the error occurred. // This path may be nil if the error is not associated with such a location. Path() []string // Msg returns the unformatted error message and its arguments for human // consumption. Msg() (format string, args []interface{}) }
Error is the common error message.
func Append ¶
Append combines two errors, flattening Lists as necessary.
func Errors ¶
Errors reports the individual errors associated with an error, which is the error itself if there is only one or, if the underlying type is List, its individual elements. If the given error is not an Error, it will be promoted to one.
func Newf ¶
Newf creates an Error with the associated position and message.
func Promote ¶
Promote converts a regular Go error to an Error if it isn't already one.
func Sanitize ¶
Sanitize sorts multiple errors and removes duplicates on a best effort basis. If err represents a single or no error, it returns the error as is.
func Wrap ¶
Wrap creates a new error where child is a subordinate error of parent. If child is list of Errors, the result will itself be a list of errors where child is a subordinate error of each parent.
func Wrapf ¶
Wrapf creates an Error with the associated position and message. The provided error is added for inspection context.
type Message ¶
type Message struct {
// contains filtered or unexported fields
}
A Message implements the error interface as well as Message to allow internationalized messages. A Message is typically used as an embedding in a CUE message.
func NewMessage ¶
NewMessage creates an error message for human consumption.
Deprecated: Use NewMessagef instead.
func NewMessagef ¶
NewMessagef creates an error message for human consumption. The arguments are for later consumption, allowing the message to be localized at a later time. The passed argument list should not be modified.
func (*Message) Error ¶
func (*Message) Msg ¶
Msg returns a printf-style format string and its arguments for human consumption.
Source Files ¶
errors.go
- Version
- v0.12.0 (latest)
- Published
- Jan 30, 2025
- Platform
- linux/amd64
- Imports
- 8 packages
- Last checked
- 5 hours ago –
Tools for package owners.