Create an error
with Error Code
NewCode/NewCodef
Description: It functions the same as the
New/Newf
method, used to create anerror
object with custom error information, includes stack information, and adds an error code object as input.Format:
// NewCode creates and returns an error that has error code and given text.
NewCode(code gcode.Code, text ...string) error
// NewCodef returns an error that has error code and formats as the given format and args.
NewCodef(code gcode.Code, format string, args ...interface{}) error
Examples:
func ExampleNewCode() {
err := gerror.NewCode(gcode.New(10000, "", nil), "My Error")
fmt.Println(err.Error())
fmt.Println(gerror.Code(err))
// Output:
// My Error
// 10000
}
func ExampleNewCodef() {
err := gerror.NewCodef(gcode.New(10000, "", nil), "It's %s", "My Error")
fmt.Println(err.Error())
fmt.Println(gerror.Code(err).Code())
// Output:
// It's My Error
// 10000
}
WrapCode/WrapCodef
Description: It functions the same as the
Wrap/Wrapf
methods, used to wrap othererror
objects, constructing multi-level error information that includes stack information, and adds an input parameter for error codes.Format:
// WrapCode wraps error with code and text.
// It returns nil if given err is nil.
WrapCode(code gcode.Code, err error, text ...string) error
// WrapCodef wraps error with code and format specifier.
// It returns nil if given `err` is nil.
WrapCodef(code gcode.Code, err error, format string, args ...interface{}) error
Examples:
func ExampleWrapCode() {
err1 := errors.New("permission denied")
err2 := gerror.WrapCode(gcode.New(10000, "", nil), err1, "Custom Error")
fmt.Println(err2.Error())
fmt.Println(gerror.Code(err2).Code())
// Output:
// Custom Error: permission denied
// 10000
}
func ExampleWrapCodef() {
err1 := errors.New("permission denied")
err2 := gerror.WrapCodef(gcode.New(10000, "", nil), err1, "It's %s", "Custom Error")
fmt.Println(err2.Error())
fmt.Println(gerror.Code(err2).Code())
// Output:
// It's Custom Error: permission denied
// 10000
}
NewCodeSkip/NewCodeSkipf
Description: It functions the same as
NewCode/NewCodef
, used to create anerror
object with an error code but ignores part of the stack information (ignoring upwards from the current method call position).Format:
// 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 NewCodeSkip(code, skip int, text string) error
// 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 NewCodeSkipf(code, skip int, format string, args ...interface{}) error
WrapCodeSkip/WrapCodeSkipf
Description: It functions the same as
WrapCodeSkip/WrapCodeSkipf
, used to wrap anerror
object with an error code but ignores part of the stack information (ignoring upwards from the current method call position).Format:
// 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 WrapCodeSkip(code gcode.Code, skip int, err error, text ...string) error
// 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 WrapCodeSkipf(code gcode.Code, skip int, err error, format string, args ...interface{}) error
Code
Description: Retrieve the error code from the error object. This method will recursively retrieve it. If the current error object does not have an error code, it will trace back to find the error code of its preceding error object, and so on. When the given
error
does not carry error code information, this method returns the predefined error codegcode.CodeNil
.Format:
// Code returns the error code of `current error`.
// It returns `CodeNil` if it has no error code neither it does not implement interface Code.
func Code(err error) gcode.Code
Examples:
func ExampleCode() {
err1 := gerror.NewCode(gcode.CodeInternalError, "permission denied")
err2 := gerror.Wrap(err1, "operation failed")
fmt.Println(gerror.Code(err1))
fmt.Println(gerror.Code(err2))
// Output:
// 50:Internal Error
// 50:Internal Error
}
HasCode
Description: This method checks whether the given error object contains the specified error code. Similar to the
Code
method, it will also trace back to find the error code of the preceding errors.Format:
// HasCode checks and reports whether `err` has `code` in its chaining errors.
func HasCode(err error, code gcode.Code) bool
Examples:
func ExampleHasCode() {
err1 := gerror.NewCode(gcode.CodeInternalError, "permission denied")
err2 := gerror.Wrap(err1, "operation failed")
fmt.Println(gerror.HasCode(err1, gcode.CodeOK))
fmt.Println(gerror.HasCode(err2, gcode.CodeInternalError))
// Output:
// false
// true
}