Documentation ¶
Index ¶
- Constants
- Variables
- func As(err error, target interface{}) bool
- func Cause(err error) error
- func Errorf(format string, args ...interface{}) error
- func FilterOut(err error, fns ...Matcher) error
- func Is(err, target error) bool
- func IsCode(err error, code int) bool
- func MustRegister(coder Coder)
- func New(message string) error
- func Reduce(err error) error
- func Register(coder Coder)
- func Unwrap(err error) error
- func WithCode(code int, format string, args ...interface{}) error
- func Wrap(err error, message string) error
- func WrapC(err error, code int, format string, args ...interface{}) error
- func Wrapf(err error, format string, args ...interface{}) error
- type Aggregate
- type Coder
- type Empty
- type Frame
- type Matcher
- type MessageCountMap
- type StackTrace
- type String
- func (s String) Delete(items ...string) String
- func (s String) Difference(s2 String) String
- func (s String) Equal(s2 String) bool
- func (s String) Has(item string) bool
- func (s String) HasAll(items ...string) bool
- func (s String) HasAny(items ...string) bool
- func (s String) Insert(items ...string) String
- func (s String) Intersection(s2 String) String
- func (s String) IsSuperset(s2 String) bool
- func (s String) Len() int
- func (s String) List() []string
- func (s String) PopAny() (string, bool)
- func (s String) Union(s2 String) String
- func (s String) UnsortedList() []string
Constants ¶
const UNKNOWN = "unknown"
Variables ¶
var ErrPreconditionViolated = errors.New("precondition is violated")
ErrPreconditionViolated is returned when the precondition is violated
Functions ¶
func As ¶
As finds the first error in err's chain that matches target, and if so, sets target to that error value and returns true.
The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.
An error matches target if the error's concrete value is assignable to the value pointed to by target, or if the error has a method As(interface{}) bool such that As(target) returns true. In the latter case, the As method is responsible for setting target.
As will panic if target is not a non-nil pointer to either a type that implements error, or to any interface type. As returns false if err is nil.
func FilterOut ¶
FilterOut removes all errors that match any of the matchers from the input error. If the input is a singular error, only that error is tested. If the input implements the Aggregate interface, the list of errors will be processed recursively.
This can be used, for example, to remove known-OK errors (such as io.EOF or os.PathNotFound) from a list of errors.
func Is ¶
Is reports whether any error in err's chain matches target.
The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.
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 Reduce ¶
Reduce will return err or, if err is an Aggregate and only has one item, the first item in the aggregate.
func Unwrap ¶
Unwrap returns the result of calling the Unwrap method on err, if err's type contains an Unwrap method returning error. Otherwise, Unwrap returns nil.
Types ¶
type Aggregate ¶
Aggregate represents an object that contains multiple errors, but does not necessarily have singular semantic meaning. The aggregate can be used with `errors.Is()` to check for the occurrence of a specific error type. Errors.As() is not supported, because the caller presumably cares about a specific error of potentially multiple that match the given type.
func AggregateGoroutines ¶
AggregateGoroutines runs the provided functions in parallel, stuffing all non-nil errors into the returned Aggregate. Returns nil if all the functions complete successfully.
func CreateAggregateFromMessageCountMap ¶
func CreateAggregateFromMessageCountMap(m MessageCountMap) Aggregate
CreateAggregateFromMessageCountMap converts MessageCountMap Aggregate
func Flatten ¶
Flatten takes an Aggregate, which may hold other Aggregates in arbitrary nesting, and flattens them all into a single Aggregate, recursively.
func NewAggregate ¶
NewAggregate converts a slice of errors into an Aggregate interface, which is itself an implementation of the error interface. If the slice is empty, this returns nil. It will check if any of the element of input error list is nil, to avoid nil pointer panic when call Error().
type Coder ¶
type Coder interface { Code() int // 返回错误码 HTTPStatus() int // 返回与错误码相对应的 HTTP 状态码 String() string // 返回展示给外部用户的错误文本 Reference() string // 返回给用户的详细文档 }
Coder 定义了错误码详细信息的接口.
func ParseCoder ¶
ParseCoder 将 err 映射为 withCode 错误并解析其中错误码. 解析失败会返回 unknown Error.
type Empty ¶
type Empty struct{}
Empty is public since it is used by some internal API objects for conversions between external string arrays and internal sets, and conversion logic requires public types today.
type Frame ¶
type Frame uintptr
Frame 表示一个栈帧中的程序计数器.
func (Frame) Format ¶
Format 根据 fmt.Formatter 接口对帧进行格式化.
%s 源文件. %d 源代码行号. %n 函数名. %v 相当于 %s:%d. %+s 源文件相对于编译时 GOPATH 的函数名和路径,由 \n\t 分隔(<funcname>\n\t<path>). %+v 相当于 %+s:%d.
func (Frame) MarshalText ¶
MarshalText 将堆栈格式化为文本字符串. 输出与 fmt.Sprintf("%+v", f) 相同,但没有换行符或制表符.
type MessageCountMap ¶
MessageCountMap contains occurrence for each error message.
type String ¶
String is a set of strings, implemented via map[string]struct{} for minimal memory consumption.
func StringKeySet ¶
func StringKeySet(theMap interface{}) String
StringKeySet creates a String from a keys of a map[string](? extends interface{}). If the value passed in is not actually a map, this will panic.
func (String) Difference ¶
Difference returns a set of objects that are not in s2 For example: s = {a1, a2, a3} s2 = {a1, a2, a4, a5} s.Difference(s2) = {a3} s2.Difference(s) = {a4, a5}
func (String) Equal ¶
Equal returns true if and only if s is equal (as a set) to s2. Two sets are equal if their membership is identical. (In practice, this means same elements, order doesn't matter)
func (String) Intersection ¶
Intersection returns a new set which includes the item in BOTH s and s2 For example: s = {a1, a2} s2 = {a2, a3} s.Intersection(s2) = {a2}
func (String) IsSuperset ¶
IsSuperset returns true if and only if s is a superset of s2.
func (String) Union ¶
Union returns a new set which includes items in either s or s2. For example: s = {a1, a2} s2 = {a3, a4} s.Union(s2) = {a1, a2, a3, a4} s2.Union(s) = {a1, a2, a3, a4}
func (String) UnsortedList ¶
UnsortedList returns the slice with contents in random order.