errors

package
v0.0.0-...-4f9d474 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 29, 2023 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const UNKNOWN = "unknown"

Variables

View Source
var ErrPreconditionViolated = errors.New("precondition is violated")

ErrPreconditionViolated is returned when the precondition is violated

Functions

func As

func As(err error, target interface{}) bool

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 Cause

func Cause(err error) error

Cause 返回错误的根本原因(如果 err 实现了 Cause() 方法).

func Errorf

func Errorf(format string, args ...interface{}) error

Errorf 基于给定的参数格式化字符串返回一个 base 错误.

func FilterOut

func FilterOut(err error, fns ...Matcher) error

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

func Is(err, target error) bool

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 IsCode

func IsCode(err error, code int) bool

IsCode 判断 err 链中是否有错误码为 code 的错误.

func MustRegister

func MustRegister(coder Coder)

MustRegister 注册一个用户定义的错误码(与已存在错误码冲突时会 panic).

func New

func New(message string) error

New 基于给定的 message 返回一个 base 错误.

func Reduce

func Reduce(err error) error

Reduce will return err or, if err is an Aggregate and only has one item, the first item in the aggregate.

func Register

func Register(coder Coder)

Register 注册一个用户定义的错误码(会覆盖已存在的错误码).

func Unwrap

func Unwrap(err error) error

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.

func WithCode

func WithCode(code int, format string, args ...interface{}) error

WithCode 创建一个 withCode 错误.

func Wrap

func Wrap(err error, message string) error

Wrap 基于给定的 message 包装错误.

func WrapC

func WrapC(err error, code int, format string, args ...interface{}) error

WrapC 将 err 封装成一个 withCode 错误.

func Wrapf

func Wrapf(err error, format string, args ...interface{}) error

Wrapf 基于给定的参数格式化字符串包装错误.

Types

type Aggregate

type Aggregate interface {
	error
	Errors() []error
	Is(error) bool
}

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

func AggregateGoroutines(funcs ...func() error) Aggregate

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

func Flatten(agg Aggregate) Aggregate

Flatten takes an Aggregate, which may hold other Aggregates in arbitrary nesting, and flattens them all into a single Aggregate, recursively.

func NewAggregate

func NewAggregate(errlist []error) Aggregate

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

func ParseCoder(err error) Coder

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

func (f Frame) Format(s fmt.State, verb rune)

Format 根据 fmt.Formatter 接口对帧进行格式化.

%s    源文件.
%d    源代码行号.
%n    函数名.
%v    相当于 %s:%d.
%+s   源文件相对于编译时 GOPATH 的函数名和路径,由 \n\t 分隔(<funcname>\n\t<path>).
%+v   相当于 %+s:%d.

func (Frame) MarshalText

func (f Frame) MarshalText() ([]byte, error)

MarshalText 将堆栈格式化为文本字符串. 输出与 fmt.Sprintf("%+v", f) 相同,但没有换行符或制表符.

type Matcher

type Matcher func(error) bool

Matcher is used to match errors. Returns true if the error matches.

type MessageCountMap

type MessageCountMap map[string]int

MessageCountMap contains occurrence for each error message.

type StackTrace

type StackTrace []Frame

StackTrace 是一个从内层到外层的栈帧列表.

func (StackTrace) Format

func (st StackTrace) Format(s fmt.State, verb rune)

Format 根据 fmt.Formatter 接口对栈帧进行格式化.

%s	列出堆栈中每个帧的源文件.
%v	列出堆栈中每个帧的源文件和行号.
%+v   打印堆栈中每个帧的文件名、函数和行号.

type String

type String map[string]Empty

String is a set of strings, implemented via map[string]struct{} for minimal memory consumption.

func NewString

func NewString(items ...string) String

NewString creates a String from a list of values.

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) Delete

func (s String) Delete(items ...string) String

Delete removes all items from the set.

func (String) Difference

func (s String) Difference(s2 String) String

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

func (s String) Equal(s2 String) bool

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) Has

func (s String) Has(item string) bool

Has returns true if and only if item is contained in the set.

func (String) HasAll

func (s String) HasAll(items ...string) bool

HasAll returns true if and only if all items are contained in the set.

func (String) HasAny

func (s String) HasAny(items ...string) bool

HasAny returns true if any items are contained in the set.

func (String) Insert

func (s String) Insert(items ...string) String

Insert adds items to the set.

func (String) Intersection

func (s String) Intersection(s2 String) String

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

func (s String) IsSuperset(s2 String) bool

IsSuperset returns true if and only if s is a superset of s2.

func (String) Len

func (s String) Len() int

Len returns the size of the set.

func (String) List

func (s String) List() []string

List returns the contents as a sorted string slice.

func (String) PopAny

func (s String) PopAny() (string, bool)

PopAny returns a single element from the set.

func (String) Union

func (s String) Union(s2 String) String

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

func (s String) UnsortedList() []string

UnsortedList returns the slice with contents in random order.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL