This chapter only introduces some commonly used methods. For a complete list of error methods, please refer to the interface documentation: https://pkg.go.dev/github.com/gogf/gf/v2/errors/gerror

New/Newf

  • Description: Used to create a custom error message error object, including stack information.

  • Format:

    1. // New creates and returns an error which is formatted from given text.
    2. New(text string) error
    3. // Newf returns an error that formats as the given format and args.
    4. Newf(format string, args ...interface{}) error

Wrap/Wrapf

  • Description: Used to wrap other error objects, constructing multi-level error messages, including stack information.

  • Format:

    1. // Wrap wraps error with text. It returns nil if given err is nil.
    2. // Note that it does not lose the error code of wrapped error, as it inherits the error code from it.
    3. func Wrap(err error, text string) error
    4. // Wrapf returns an error annotating err with a stack trace at the point Wrapf is called, and the format specifier.
    5. // It returns nil if given `err` is nil.
    6. // Note that it does not lose the error code of wrapped error, as it inherits the error code from it.
    7. func Wrapf(err error, format string, args ...interface{}) error

NewSkip/NewSkipf

  • Description: Used to create a custom error message error object, and ignore part of the stack information (ignoring upwards from the current method call location). Advanced functionality, rarely used by general developers.

  • Format:

    1. // NewSkip creates and returns an error which is formatted from given text.
    2. // The parameter `skip` specifies the stack callers skipped amount.
    3. func NewSkip(skip int, text string) error
    4. // NewSkipf returns an error that formats as the given format and args.
    5. // The parameter `skip` specifies the stack callers skipped amount.
    6. func NewSkipf(skip int, format string, args ...interface{}) error

WrapSkip/WrapSkipf

  • Description: Similar to the Wrap/Wrapf methods, but ignores part of the stack information.

  • Format:

    1. // WrapSkip wraps error with text. It returns nil if given err is nil.
    2. // The parameter `skip` specifies the stack callers skipped amount.
    3. // Note that it does not lose the error code of wrapped error, as it inherits the error code from it.
    4. func WrapSkip(skip int, err error, text string) error
    5. // WrapSkipf wraps error with text that is formatted with given format and args. It returns nil if given err is nil.
    6. // The parameter `skip` specifies the stack callers skipped amount.
    7. // Note that it does not lose the error code of wrapped error, as it inherits the error code from it.
    8. func WrapSkipf(skip int, err error, format string, args ...interface{}) error