Create an error with Error Code

NewCode/NewCodef

  • Description: It functions the same as the New/Newf method, used to create an error object with custom error information, includes stack information, and adds an error code object as input.

  • Format:

    1. // NewCode creates and returns an error that has error code and given text.
    2. NewCode(code gcode.Code, text ...string) error
    3. // NewCodef returns an error that has error code and formats as the given format and args.
    4. NewCodef(code gcode.Code, format string, args ...interface{}) error
  • Examples:

    1. func ExampleNewCode() {
    2. err := gerror.NewCode(gcode.New(10000, "", nil), "My Error")
    3. fmt.Println(err.Error())
    4. fmt.Println(gerror.Code(err))
    5. // Output:
    6. // My Error
    7. // 10000
    8. }
    9. func ExampleNewCodef() {
    10. err := gerror.NewCodef(gcode.New(10000, "", nil), "It's %s", "My Error")
    11. fmt.Println(err.Error())
    12. fmt.Println(gerror.Code(err).Code())
    13. // Output:
    14. // It's My Error
    15. // 10000
    16. }

WrapCode/WrapCodef

  • Description: It functions the same as the Wrap/Wrapf methods, used to wrap other error objects, constructing multi-level error information that includes stack information, and adds an input parameter for error codes.

  • Format:

    1. // WrapCode wraps error with code and text.
    2. // It returns nil if given err is nil.
    3. WrapCode(code gcode.Code, err error, text ...string) error
    4. // WrapCodef wraps error with code and format specifier.
    5. // It returns nil if given `err` is nil.
    6. WrapCodef(code gcode.Code, err error, format string, args ...interface{}) error
  • Examples:

    1. func ExampleWrapCode() {
    2. err1 := errors.New("permission denied")
    3. err2 := gerror.WrapCode(gcode.New(10000, "", nil), err1, "Custom Error")
    4. fmt.Println(err2.Error())
    5. fmt.Println(gerror.Code(err2).Code())
    6. // Output:
    7. // Custom Error: permission denied
    8. // 10000
    9. }
    10. func ExampleWrapCodef() {
    11. err1 := errors.New("permission denied")
    12. err2 := gerror.WrapCodef(gcode.New(10000, "", nil), err1, "It's %s", "Custom Error")
    13. fmt.Println(err2.Error())
    14. fmt.Println(gerror.Code(err2).Code())
    15. // Output:
    16. // It's Custom Error: permission denied
    17. // 10000
    18. }

NewCodeSkip/NewCodeSkipf

  • Description: It functions the same as NewCode/NewCodef, used to create an error object with an error code but ignores part of the stack information (ignoring upwards from the current method call position).

  • Format:

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

WrapCodeSkip/WrapCodeSkipf

  • Description: It functions the same as WrapCodeSkip/WrapCodeSkipf, used to wrap an error object with an error code but ignores part of the stack information (ignoring upwards from the current method call position).

  • Format:

    1. // WrapCodeSkip wraps error with code and text.
    2. // It returns nil if given err is nil.
    3. // The parameter `skip` specifies the stack callers skipped amount.
    4. func WrapCodeSkip(code gcode.Code, skip int, err error, text ...string) error
    5. // WrapCodeSkipf wraps error with code and text that is formatted with given format and args.
    6. // It returns nil if given err is nil.
    7. // The parameter `skip` specifies the stack callers skipped amount.
    8. 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 code gcode.CodeNil.

  • Format:

    1. // Code returns the error code of `current error`.
    2. // It returns `CodeNil` if it has no error code neither it does not implement interface Code.
    3. func Code(err error) gcode.Code
  • Examples:

    1. func ExampleCode() {
    2. err1 := gerror.NewCode(gcode.CodeInternalError, "permission denied")
    3. err2 := gerror.Wrap(err1, "operation failed")
    4. fmt.Println(gerror.Code(err1))
    5. fmt.Println(gerror.Code(err2))
    6. // Output:
    7. // 50:Internal Error
    8. // 50:Internal Error
    9. }

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:

    1. // HasCode checks and reports whether `err` has `code` in its chaining errors.
    2. func HasCode(err error, code gcode.Code) bool
  • Examples:

    1. func ExampleHasCode() {
    2. err1 := gerror.NewCode(gcode.CodeInternalError, "permission denied")
    3. err2 := gerror.Wrap(err1, "operation failed")
    4. fmt.Println(gerror.HasCode(err1, gcode.CodeOK))
    5. fmt.Println(gerror.HasCode(err2, gcode.CodeInternalError))
    6. // Output:
    7. // false
    8. // true
    9. }