Documentation ¶
Index ¶
- Constants
- func ErrorType(err error) string
- func OriginalError(err error) error
- type Error
- func (e *Error) Clone() *Error
- func (e *Error) Error() string
- func (e *Error) Message() string
- func (e *Error) WithField(k string, v interface{}) *Error
- func (e *Error) WithFields(kv map[string]interface{}) *Error
- func (e *Error) WithLabel(l string) *Error
- func (e *Error) WithMessage(msg string) *Error
- func (e *Error) WithStack() *Error
- func (e *Error) WithType(t string) *Error
Constants ¶
const ( // ErrTypeUnknown is the default value for Error's Type field, // set a custom Type By NewErrorWithType or NewErrorType,WithType is recommended ErrTypeUnknown = "unknown" // ErrTypeConfig is a common error type using when the error is happened by config paring or checking. ErrTypeConfig = "config" )
Variables ¶
This section is empty.
Functions ¶
func ErrorType ¶
ErrorType return Type field of the Error object, it will return "unknown" when the input is not type of *base.Error
func OriginalError ¶
OriginalError return Err field of the Error object, it will return the input object self when it's not type of *base.Error
Types ¶
type Error ¶
type Error struct { // Type defines the error's type as a self-define string, it will be set to "unknown" as default. // Using NewErrorWithType or NewError,WithType to create *base.Error is recommended. // This field could split errors by categories, it's a required field. // When do querying or present errors with UI, Type field is also the important tag or condition. Type string // Err is the error object return by functions, it's not typed base.Error as usual. Err error // Labels defines many labels of the error object, when used in searching scenes. // It will be added when call WithLabel method. // When do formatting, it will be split with ",". Labels map[string]struct{} // Msg defines additional comment for the error. // It will be added when call WithMessage method. // When do formatting, it will be split with "\n". Msg []string // Stack is the debug.stack information when the error is happened. // It will be set when call WithStack method Stack string // Fields is the additional params information of then error. // It will be added when call WithField or WithFields. // When do formatting to text, it will be joined with "=" and split with ",", such as k1=v1,k2=v2. // When do formatting to backend database, it will be marshals to json format. Fields map[string]interface{} }
Error struct is the basic error wrapper struct, which is widely used in projects. This error object is used as standard error return value of functions in projects. Functions in projects should return this error object instead of others. Use base.NewError or base.NewErrorWithType wraps the third module's error return which is not a base.Error typed object. Use base.WrapError add more information to the Error object when the function return is a base.Error typed object. The base.Error could format in struct object(k-v format) to backend database which could present by UI.
func NewConfigError ¶
NewConfigError create an error object with "config" Type
func NewError ¶
NewError create an error object wrapped input err, default Type is "unknown", you can set the Type by WithType method later. Using NewErrorWithType is more recommend than NewError, because a clear Type value is very important.
func NewErrorWithType ¶
NewErrorWithType create an error object wrapped input err with the given Type field. This function is used to create error in projects typically. A clear type for the error is very important, see the Type field comments.
func WrapError ¶
WrapError uses to create a new error object or wrapped exist *base.Error. It's used for wrapping the exists *base.Error object typically. When the input err object is not type of *base.Error, it will create a new error object with "unknown" Type, it should be set the custom Type with WithType method.