Documentation ¶
Overview ¶
Package meh adds typed errors, codes and reusable details to Go's native errors.
Index ¶
- Constants
- func ApplyCode(err error, code Code) error
- func ApplyDetails(err error, details Details) error
- func ApplyStackTrace(err error) error
- func ClearPassThrough(err error) error
- func Finalize(err error) error
- func NewBadInputErr(message string, details Details) error
- func NewBadInputErrFromErr(err error, message string, details Details) error
- func NewErr(code Code, message string, details Details) error
- func NewErrFromErr(err error, code Code, message string, details Details) error
- func NewForbiddenErr(message string, details Details) error
- func NewForbiddenErrFromErr(err error, message string, details Details) error
- func NewInternalErr(message string, details Details) error
- func NewInternalErrFromErr(err error, message string, details Details) error
- func NewNotFoundErr(message string, details Details) error
- func NewNotFoundErrFromErr(err error, message string, details Details) error
- func NewPassThroughErr(err error, code Code, message string, details Details) error
- func NewUnauthorizedErr(message string, details Details) error
- func NewUnauthorizedErrFromErr(err error, message string, details Details) error
- func NilOrWrap(err error, message string, details Details) error
- func ToMap(err error) map[string]interface{}
- func Wrap(toWrap error, message string, details Details) error
- type Code
- type Details
- type Error
- type ErrorUnwrapper
- type StackTrace
Constants ¶
const ( MapFieldErrorCode = "x_code" MapFieldErrorMessage = "x_err_message" )
Field names for usage in ToMap.
Variables ¶
This section is empty.
Functions ¶
func ApplyDetails ¶
ApplyDetails wraps the given error with an ErrNeutral and the given Details.
func ApplyStackTrace ¶
ApplyStackTrace applies the current stack trace to the given error.
func ClearPassThrough ¶ added in v1.7.0
ClearPassThrough returns the given error with all pass-through-fields (from wrapped errors as well).
func Finalize ¶ added in v1.7.0
Finalize alters the given error for handing it off to other libraries. If the given error is nil, nil is returned. If it contains a pass-through, indicated by Error.WrappedErrPassThrough being true, the wrapped error is returned from the first one, where pass-through is set. Otherwise, the error is returned as with Cast.
func NewBadInputErr ¶
NewBadInputErr creates a new ErrBadInput with the given message and details.
func NewBadInputErrFromErr ¶
NewBadInputErrFromErr creates a new ErrBadInput with the given error to be wrapped, message and details.
func NewErrFromErr ¶ added in v1.6.0
NewErrFromErr creates a new Error with the given wrapped one, Code, message and details.
func NewForbiddenErr ¶ added in v1.5.0
NewForbiddenErr creates a new ErrForbidden with the given message and details.
func NewForbiddenErrFromErr ¶ added in v1.5.0
NewForbiddenErrFromErr creates a new ErrForbidden with the given error to be wrapped, message and details.
func NewInternalErr ¶
NewInternalErr creates a new ErrInternal with the given message and details.
func NewInternalErrFromErr ¶
NewInternalErrFromErr creates a new ErrInternal with the given error to be wrapped, message and details.
func NewNotFoundErr ¶
NewNotFoundErr creates a new ErrNotFound with the given message and details.
func NewNotFoundErrFromErr ¶
NewNotFoundErrFromErr creates a new ErrNotFound with the given error to be wrapped, message and details.
func NewPassThroughErr ¶ added in v1.7.0
NewPassThroughErr is similar to NewErrFromErr with the only difference that Error.WrappedErrPassThrough is set to true. This makes Finalize return the given error, even if wrapped inside *Error.
func NewUnauthorizedErr ¶ added in v1.3.0
NewUnauthorizedErr creates a new ErrUnauthorized with the given message and details.
func NewUnauthorizedErrFromErr ¶ added in v1.5.0
NewUnauthorizedErrFromErr creates a new ErrUnauthorized with the given error to be wrapped, message and details.
func NilOrWrap ¶ added in v1.4.0
NilOrWrap returns nil if the given error is nil or calls meh.Wrap on it otherwise.
func ToMap ¶
ToMap returns the details of the given error as a key-value map with appended enhanced information regarding the error itself (Error.Code to MapFieldErrorCode and the Error.Error-message to MapFieldErrorMessage).
func Wrap ¶
Wrap wraps the given error with an ErrNeutral, the message and details. If no details should be added, set them nil. However, the parameter is mandatory in order to enforce providing as much detail as possible.
Warning: Wrap will NOT add a stack trace unlike errors.Wrap as this seems inconvenient when calling Wrap while bubbling!
Types ¶
type Code ¶
type Code string
Code is the type of error in Error.
const ( // ErrUnexpected is the default error that is used when no other Code is // specified. ErrUnexpected Code = "" // ErrInternal is used for basic internal errors. ErrInternal Code = "internal" // ErrBadInput is used when submitted data was invalid. This should be used, // when handling external input, e.g. client data or database requests failing // because of constraint violation, etc. ErrBadInput Code = "bad-input" // ErrNotFound is used when requested resources where not found. ErrNotFound Code = "not-found" // ErrNeutral is used mainly for wrapping in order to not change the Code. ErrNeutral Code = "neutral" // required authorized access. ErrUnauthorized Code = "unauthorized" // ErrForbidden is used for unauthorized access to resources. ErrForbidden Code = "forbidden" )
type Details ¶
type Details map[string]interface{}
Details are optionally provided error details in Error.Details that are used for easier debugging and error locating.
type Error ¶
type Error struct { // Code is the type of Error. // // Warning: The Code only applies to the current level. For checking the actual // level (ignoring ErrNeutral like being added by ApplyCode or Wrap), you need to // use ErrorCode! Code Code // WrappedErr is an optionally wrapped error for example added when wrapping // low-level errors or using Wrap. WrappedErr error // WrappedErrPassThrough describes whether the WrappedErr should be returned on // Finalize. This is useful if the wrapped error is relevant for other libraries, // etc. WrappedErrPassThrough bool // Message is an internal error message that is used when generating the error // message if not an empty string. Message string // Details is any optionally added information. Details Details // Trace is the stack trace to use (set it via ApplyStackTrace). Trace StackTrace }
Error is the container for any relevant error information that needs to be kept when bubbling. For wrapping errors use Wrap. You can create an Error manually or by using generators like NewInternalErrFromErr.
func Cast ¶
Cast tries to Cast the given error to *Error. In case of failure, a new ErrUnexpected is created, wrapping the original error.
func (*Error) Error ¶
Error is used for implementing the error interface and printing the error string by unwrapping errors. The error string will not contain the error code or any further details but only messages.
func (*Error) MarshalJSON ¶ added in v1.9.0
MarshalJSON marshals the Error into a JSON representation.
func (*Error) StackTrace ¶
func (e *Error) StackTrace() errors.StackTrace
StackTrace returns the lowest-level errors.StackTrace for the Error if one was set.
func (*Error) UnmarshalJSON ¶ added in v1.9.0
UnmarshalJSON unmarshals an Error from JSON representation.
type ErrorUnwrapper ¶
type ErrorUnwrapper struct {
// contains filtered or unexported fields
}
ErrorUnwrapper is used for iterating over the recursive structure of wrapped errors in Error.WrappedErr.
func NewErrorUnwrapper ¶
func NewErrorUnwrapper(err error) *ErrorUnwrapper
NewErrorUnwrapper allows iterating the given error from top to bottom level using ErrorUnwrapper.Next in a loop and getting the current level's error using ErrorUnwrapper.Current.
func (*ErrorUnwrapper) Current ¶
func (it *ErrorUnwrapper) Current() error
Current returns the current error. Remember to call Next before the first Current call.
func (*ErrorUnwrapper) Level ¶
func (it *ErrorUnwrapper) Level() int
Level returns the current level. Starting at -1, it will increment from 0 for each Next-call.
func (*ErrorUnwrapper) Next ¶
func (it *ErrorUnwrapper) Next() bool
Next unwraps the current error.
Warning: You always need to make a Next call before being able to retrieve the first error via Current. The reason for this is that it allows simple iterating in a for-loop using Next as condition like when scanning rows using the sql-package.
type StackTrace ¶
type StackTrace struct { // StackTrace is the actual errors.StackTrace. StackTrace errors.StackTrace // StackTraceStr is a formatted stack trace from debug.Stack. StackTraceStr string }
StackTrace holds an errors.StackTrace as well as a formatted stack trace for usage in logging. You can add one using ApplyStackTrace.
Directories ¶
Path | Synopsis |
---|---|
Package mehgin provides some wrappers and utils for bridging mehhttp and gin.
|
Package mehgin provides some wrappers and utils for bridging mehhttp and gin. |
Package mehhttp provides functionality for handling and responding errors.
|
Package mehhttp provides functionality for handling and responding errors. |
Package mehlog allows logging of meh.Error to zap.Logger.
|
Package mehlog allows logging of meh.Error to zap.Logger. |
Package mehpg provides error functionality regarding postgres-errors.
|
Package mehpg provides error functionality regarding postgres-errors. |