Documentation ¶
Overview ¶
Package errors provides simple functions to manipulate errors.
Very note that, this package is quite a base package, which should not import extra packages except standard packages, to avoid cycle imports.
Index ¶
- func Cause(err error) error
- func Code(err error) int
- func Current(err error) error
- func New(text string) error
- func NewCode(code int, text string) error
- func NewCodeSkip(code, skip int, text string) error
- func NewCodeSkipf(code, skip int, format string, args ...interface{}) error
- func NewCodef(code int, format string, args ...interface{}) error
- func NewSkip(skip int, text string) error
- func NewSkipf(skip int, format string, args ...interface{}) error
- func Newf(format string, args ...interface{}) error
- func Next(err error) error
- func Stack(err error) string
- func Wrap(err error, text string) error
- func WrapCode(code int, err error, text string) error
- func WrapCodeSkip(code, skip int, err error, text string) error
- func WrapCodeSkipf(code, skip int, err error, format string, args ...interface{}) error
- func WrapCodef(code int, err error, format string, args ...interface{}) error
- func WrapSkip(skip int, err error, text string) error
- func WrapSkipf(skip int, err error, format string, args ...interface{}) error
- func Wrapf(err error, format string, args ...interface{}) error
- type Error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Code ¶
Cause returns the error code of current error. It returns -1 if it has no error code or it does not implements interface Code.
func Current ¶
Current creates and returns the current level error. It returns nil if current level error is nil.
func NewCode ¶
NewCode creates and returns an error that has error code and given text.
Example ¶
package main import ( "fmt" "gitee.com/micro-tools/wf/errors/gerror" ) func main() { err := gerror.NewCode(10000, "My Error") fmt.Println(err.Error()) fmt.Println(gerror.Code(err)) }
Output: My Error 10000
func NewCodeSkip ¶
NewCodeSkip creates and returns an error which has error code and is formatted from given text. The parameter <skip> specifies the stack callers skipped amount.
func NewCodeSkipf ¶
NewCodeSkipf returns an error that has error code and formats as the given format and args. The parameter <skip> specifies the stack callers skipped amount.
func NewCodef ¶
NewCodef returns an error that has error code and formats as the given format and args.
Example ¶
package main import ( "fmt" "gitee.com/micro-tools/wf/errors/gerror" ) func main() { err := gerror.NewCodef(10000, "It's %s", "My Error") fmt.Println(err.Error()) fmt.Println(gerror.Code(err)) }
Output: It's My Error 10000
func NewSkip ¶
NewSkip creates and returns an error which is formatted from given text. The parameter <skip> specifies the stack callers skipped amount.
func NewSkipf ¶
NewSkipf returns an error that formats as the given format and args. The parameter <skip> specifies the stack callers skipped amount.
func Next ¶
Next returns the next level error. It returns nil if current level error or the next level error is nil.
func Stack ¶
Stack returns the stack callers as string. It returns the error string directly if the <err> does not support stacks.
func WrapCode ¶
WrapCode wraps error with code and text. It returns nil if given err is nil.
Example ¶
package main import ( "errors" "fmt" "gitee.com/micro-tools/wf/errors/gerror" ) func main() { err1 := errors.New("permission denied") err2 := gerror.WrapCode(10000, err1, "Custom Error") fmt.Println(err2.Error()) fmt.Println(gerror.Code(err2)) }
Output: Custom Error: permission denied 10000
func WrapCodeSkip ¶
WrapCodeSkip wraps error with code and text. It returns nil if given err is nil. The parameter <skip> specifies the stack callers skipped amount.
func WrapCodeSkipf ¶
WrapCodeSkipf wraps error with code and text that is formatted with given format and args. It returns nil if given err is nil. The parameter <skip> specifies the stack callers skipped amount.
func WrapCodef ¶
WrapCodef wraps error with code and format specifier. It returns nil if given <err> is nil.
Example ¶
package main import ( "errors" "fmt" "gitee.com/micro-tools/wf/errors/gerror" ) func main() { err1 := errors.New("permission denied") err2 := gerror.WrapCodef(10000, err1, "It's %s", "Custom Error") fmt.Println(err2.Error()) fmt.Println(gerror.Code(err2)) }
Output: It's Custom Error: permission denied 10000
func WrapSkip ¶
WrapSkip wraps error with text. It returns nil if given err is nil. The parameter <skip> specifies the stack callers skipped amount.
Types ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error is custom error for additional features.
func (*Error) Current ¶
Current creates and returns the current level error. It returns nil if current level error is nil.
func (*Error) Format ¶
Format formats the frame according to the fmt.Formatter interface.
%v, %s : Print all the error string; %-v, %-s : Print current level error string; %+s : Print full stack error list; %+v : Print the error string and full stack error list;
func (*Error) MarshalJSON ¶
MarshalJSON implements the interface MarshalJSON for json.Marshal. Note that do not use pointer as its receiver here.