Documentation ¶
Index ¶
- Constants
- func Cause(err error) error
- func Errorf(format string, args ...interface{}) error
- func IsCode(err error, code int) bool
- func MustRegister(coder Coder)
- func New(message string) error
- func NewWithCode(code int, message string) error
- func NewWithCodef(code int, format string, args ...interface{}) error
- func Register(coder Coder)
- func WithCode(err error, code int, message string) error
- func WithCodef(err error, code int, format string, args ...interface{}) error
- func WithMessage(err error, message string) error
- func WithMessagef(err error, format string, args ...interface{}) error
- func WithStack(err error) error
- type Coder
- type DefaultCoder
- type Frame
- type StackTrace
Constants ¶
const ( // ErrUnknown - 500: Internal server error. ErrUnknown = iota + 100000 // ErrBind - 400: Error occurred while binding the request body to the struct. ErrBind // ErrValidation - 400: Validation failed. ErrValidation // ErrTokenInvalid - 401: Token invalid. ErrTokenInvalid // ErrPageNotFound - 404: Page not found. ErrPageNotFound )
const ( // ErrEncrypt - 401: Error occurred while encrypting the user password. ErrEncrypt int = iota + 100201 // ErrSignatureInvalid - 401: Signature is invalid. ErrSignatureInvalid // ErrExpired - 401: Token expired. ErrExpired // ErrInvalidAuthHeader - 401: Invalid authorization header. ErrInvalidAuthHeader // ErrMissingHeader - 401: The `Authorization` header was empty. ErrMissingHeader // ErrPasswordIncorrect - 401: Password was incorrect. ErrPasswordIncorrect // ErrPermissionDenied - 403: Permission denied. ErrPermissionDenied )
common: authorization and authentication errors.
const ( // ErrEncodingFailed - 500: Encoding failed due to an error with the data. ErrEncodingFailed int = iota + 100301 // ErrDecodingFailed - 500: Decoding failed due to an error with the data. ErrDecodingFailed // ErrInvalidJSON - 500: Data is not valid JSON. ErrInvalidJSON // ErrEncodingJSON - 500: JSON data could not be encoded. ErrEncodingJSON // ErrDecodingJSON - 500: JSON data could not be decoded. ErrDecodingJSON // ErrInvalidYaml - 500: Data is not valid Yaml. ErrInvalidYaml // ErrEncodingYaml - 500: Yaml data could not be encoded. ErrEncodingYaml // ErrDecodingYaml - 500: Yaml data could not be decoded. ErrDecodingYaml )
common: encode/decode errors.
const ( // ErrDatabase - 500: Database error. ErrDatabase int = iota + 100101 )
common: database errors.
Variables ¶
This section is empty.
Functions ¶
func Cause ¶
Cause returns the underlying cause of the error, if possible. An error value has a cause if it implements the following interface:
type causer interface { Cause() error }
If the error does not implement Cause, the original error will be returned. If the error is nil, nil will be returned without further investigation.
func Errorf ¶
Errorf formats according to a format specifier and returns the string as a value that satisfies error. Errorf also records the stack trace at the point it was called.
func MustRegister ¶
func MustRegister(coder Coder)
MustRegister register a user define error code. It will panic when the same Code already exist.
func New ¶
New returns an error with the supplied message. New also records the stack trace at the point it was called.
func NewWithCode ¶
func NewWithCodef ¶
func Register ¶
func Register(coder Coder)
Register register a user define error code. It will overrid the exist code.
func WithMessage ¶
Wrap returns an error annotating err with a stack trace at the point Wrap is called, and the supplied message. If err is nil, Wrap returns nil.
func WithMessagef ¶
WithMessagef annotates err with the format specifier. If err is nil, WithMessagef returns nil.
Types ¶
type Coder ¶
type Coder interface { // HTTP status that should be used for the associated error code. HTTPStatus() int // External (user) facing error text. String() string // Reference returns the detail documents for user. Reference() string // Code returns the code of the coder Code() int }
Coder defines an interface for an error code detail information.
func ParseCoder ¶
ParseCoder parse any error into *withCode. nil error will return nil direct. None withStack error will be parsed as ErrUnknown.
type DefaultCoder ¶ added in v1.3.1
type DefaultCoder struct { // C refers to the integer code of the ErrCode. C int // HTTP status that should be used for the associated error code. HTTP int // External (user) facing error text. Ext string // Ref specify the reference document. Ref string }
func (DefaultCoder) Code ¶ added in v1.3.1
func (coder DefaultCoder) Code() int
Code returns the integer code of the coder.
func (DefaultCoder) HTTPStatus ¶ added in v1.3.1
func (coder DefaultCoder) HTTPStatus() int
HTTPStatus returns the associated HTTP status code, if any. Otherwise, returns 200.
func (DefaultCoder) Reference ¶ added in v1.3.1
func (coder DefaultCoder) Reference() string
Reference returns the reference document.
func (DefaultCoder) String ¶ added in v1.3.1
func (coder DefaultCoder) String() string
String implements stringer. String returns the external error message, if any.
type Frame ¶
type Frame uintptr
Frame represents a program counter inside a stack frame. For historical reasons if Frame is interpreted as a uintptr its value represents the program counter + 1.
func (Frame) Format ¶
Format formats the frame according to the fmt.Formatter interface.
%s source file %d source line %n function name %v equivalent to %s:%d
Format accepts flags that alter the printing of some verbs, as follows:
%+s function name and path of source file relative to the compile time GOPATH separated by \n\t (<funcname>\n\t<path>) %+v equivalent to %+s:%d
func (Frame) MarshalText ¶
MarshalText formats a stacktrace Frame as a text string. The output is the same as that of fmt.Sprintf("%+v", f), but without newlines or tabs.
type StackTrace ¶
type StackTrace []Frame
StackTrace is stack of Frames from innermost (newest) to outermost (oldest).
func (StackTrace) Format ¶
func (st StackTrace) Format(s fmt.State, verb rune)
Format formats the stack of Frames according to the fmt.Formatter interface.
%s lists source files for each Frame in the stack %v lists the source file and line number for each Frame in the stack
Format accepts flags that alter the printing of some verbs, as follows:
%+v Prints filename, function, and line number for each Frame in the stack.