Documentation ¶
Overview ¶
Package errors helps in wrapping errors with custom type as well as a user friendly message. This is particularly useful when responding to APIs
Index ¶
- Constants
- func As(err error, target interface{}) bool
- func ErrWithoutTrace(err error) (string, bool)
- func HTTPStatusCode(err error) (int, bool)
- func HTTPStatusCodeMessage(err error) (int, string, bool)
- func HasType(err error, et errType) bool
- func Is(err, target error) bool
- func Join(errs ...error) error
- func Message(err error) (string, bool)
- func ProgramCounters(err error) []uintptr
- func RuntimeFrames(err error) *runtime.Frames
- func SetDefaultType(e errType)
- func Stacktrace(err error) string
- func StacktraceCustomFormat(msgformat string, traceFormat string, err error) string
- func StacktraceFromPcs(err error) string
- func StacktraceNoFormat(err error) []string
- func Type(err error) errType
- func TypeInt(err error) int
- func Unwrap(err error) error
- func WriteHTTP(err error, w http.ResponseWriter)
- type Error
- func DownstreamDependencyTimedout(message string) *Error
- func DownstreamDependencyTimedoutErr(original error, message string) *Error
- func DownstreamDependencyTimedoutErrf(original error, format string, args ...interface{}) *Error
- func DownstreamDependencyTimedoutf(format string, args ...interface{}) *Error
- func Duplicate(message string) *Error
- func DuplicateErr(original error, message string) *Error
- func DuplicateErrf(original error, format string, args ...interface{}) *Error
- func Duplicatef(format string, args ...interface{}) *Error
- func Empty(message string) *Error
- func EmptyErr(original error, message string) *Error
- func EmptyErrf(original error, format string, args ...interface{}) *Error
- func Emptyf(format string, args ...interface{}) *Error
- func Errorf(fromat string, args ...interface{}) *Error
- func InputBody(message string) *Error
- func InputBodyErr(original error, message string) *Error
- func InputBodyErrf(original error, format string, args ...interface{}) *Error
- func InputBodyf(format string, args ...interface{}) *Error
- func Internal(message string) *Error
- func InternalErr(original error, message string) *Error
- func InternalErrf(original error, format string, args ...interface{}) *Error
- func Internalf(format string, args ...interface{}) *Error
- func MaximumAttempts(message string) *Error
- func MaximumAttemptsErr(original error, message string) *Error
- func MaximumAttemptsErrf(original error, format string, args ...interface{}) *Error
- func MaximumAttemptsf(format string, args ...interface{}) *Error
- func New(msg string) *Error
- func NewWithErrMsgType(original error, message string, etype errType) *Error
- func NewWithErrMsgTypef(original error, etype errType, format string, args ...interface{}) *Error
- func NewWithType(msg string, etype errType) *Error
- func NewWithTypef(etype errType, format string, args ...interface{}) *Error
- func Newf(fromat string, args ...interface{}) *Error
- func NotFound(message string) *Error
- func NotFoundErr(original error, message string) *Error
- func NotFoundErrf(original error, format string, args ...interface{}) *Error
- func NotFoundf(format string, args ...interface{}) *Error
- func SubscriptionExpired(message string) *Error
- func SubscriptionExpiredErr(original error, message string) *Error
- func SubscriptionExpiredErrf(original error, format string, args ...interface{}) *Error
- func SubscriptionExpiredf(format string, args ...interface{}) *Error
- func Unauthenticated(message string) *Error
- func UnauthenticatedErr(original error, message string) *Error
- func UnauthenticatedErrf(original error, format string, args ...interface{}) *Error
- func Unauthenticatedf(format string, args ...interface{}) *Error
- func Unauthorized(message string) *Error
- func UnauthorizedErr(original error, message string) *Error
- func UnauthorizedErrf(original error, format string, args ...interface{}) *Error
- func Unauthorizedf(format string, args ...interface{}) *Error
- func Validation(message string) *Error
- func ValidationErr(original error, message string) *Error
- func ValidationErrf(original error, format string, args ...interface{}) *Error
- func Validationf(format string, args ...interface{}) *Error
- func Wrap(original error, msg ...string) *Error
- func WrapWithMsg(original error, msg string) *Errordeprecated
- func Wrapf(original error, format string, args ...interface{}) *Error
- func (e *Error) Error() string
- func (e *Error) ErrorWithoutFileLine() string
- func (e *Error) Format(s fmt.State, verb rune)
- func (e *Error) HTTPStatusCode() int
- func (e *Error) Is(err error) bool
- func (e *Error) Message() string
- func (e *Error) ProgramCounters() []uintptr
- func (e *Error) RuntimeFrames() *runtime.Frames
- func (e *Error) StackTrace() []string
- func (e *Error) StackTraceCustomFormat(msgformat string, traceFormat string) []string
- func (e *Error) StackTraceNoFormat() []string
- func (e *Error) Type() errType
- func (e *Error) Unwrap() error
Constants ¶
const ( // TypeInternal is error type for when there is an internal system error. e.g. Database errors TypeInternal errType = iota // TypeValidation is error type for when there is a validation error. e.g. invalid email address TypeValidation // TypeInputBody is error type for when an input data type error. e.g. invalid JSON TypeInputBody // TypeDuplicate is error type for when there's duplicate content TypeDuplicate // TypeUnauthenticated is error type when trying to access an authenticated API without authentication TypeUnauthenticated TypeUnauthorized // TypeEmpty is error type for when an expected non-empty resource, is empty TypeEmpty // TypeNotFound is error type for an expected resource is not found e.g. user ID not found TypeNotFound // TypeMaximumAttempts is error type for attempting the same action more than allowed TypeMaximumAttempts // TypeSubscriptionExpired is error type for when a user's 'paid' account has expired TypeSubscriptionExpired // TypeDownstreamDependencyTimedout is error type for when a request to a downstream dependent service times out TypeDownstreamDependencyTimedout // DefaultMessage is the default user friendly message DefaultMessage = "unknown error occurred" )
While adding a new Type, the respective helper functions should be added, also update the WriteHTTP method accordingly
Variables ¶
This section is empty.
Functions ¶
func ErrWithoutTrace ¶
ErrWithoutTrace is a duplicate of Message, but with clearer name. The boolean is 'true' if the provided err is of type *Error
func HTTPStatusCode ¶
HTTPStatusCode returns appropriate HTTP response status code based on type of the error. The boolean is 'true' if the provided error is of type *Err In case of joined errors, it'll return the status code of the last *Error
func HTTPStatusCodeMessage ¶
HTTPStatusCodeMessage returns the appropriate HTTP status code, message, boolean for the error the boolean value is true if the error was of type *Error, false otherwise.
func HasType ¶
HasType will check if the provided err type is available anywhere nested in the error
func Message ¶
Message recursively concatenates all the messages set while creating/wrapping the errors. The boolean is 'true' if the provided error is of type *Err
func ProgramCounters ¶
func RuntimeFrames ¶
func SetDefaultType ¶
func SetDefaultType(e errType)
SetDefaultType will set the default error type, which is used in the 'New' function
func Stacktrace ¶
Stacktrace returns a string representation of the stacktrace, where each trace is separated by a newline and tab '\t'
func StacktraceCustomFormat ¶
StacktraceCustomFormat lets you prepare a stacktrace in a custom format
msgformat - is used to format the line which prints message from Error.message traceFormat - is used to format the line which prints trace Supported directives: %m - message if err type is *Error, otherwise output of `.Error()` %p - file path, empty if type is not *Error %l - line, empty if type is not *Error %f - function, empty if type is not *Error
func StacktraceFromPcs ¶
func StacktraceNoFormat ¶
Stacktrace returns a string representation of the stacktrace, as a slice of string where each element represents the error message and traces.
func Type ¶
func Type(err error) errType
Type returns the errType if it's an instance of *Error, -1 otherwise In case of joined error, it'll return the type of the last *Error
func WriteHTTP ¶
func WriteHTTP(err error, w http.ResponseWriter)
WriteHTTP is a convenience method which will check if the error is of type *Error and respond appropriately
Types ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error is the struct which holds custom attributes
func DownstreamDependencyTimedout ¶
DownstreamDependencyTimedout is a helper function to create a new error of type TypeDownstreamDependencyTimedout
func DownstreamDependencyTimedoutErr ¶
DownstreamDependencyTimedoutErr is a helper function to create a new error of type TypeDownstreamDependencyTimedout which also accepts an original error
func DownstreamDependencyTimedoutErrf ¶
DownstreamDependencyTimedoutErrf is a helper function to create a new error of type TypeDownstreamDependencyTimedout which also accepts an original error, with formatted message
func DownstreamDependencyTimedoutf ¶
DownstreamDependencyTimedoutf is a helper function to create a new error of type TypeDownstreamDependencyTimedout, with formatted message
func DuplicateErr ¶
DuplicateErr is a helper function to create a new error of type TypeDuplicate which also accepts an original error
func DuplicateErrf ¶
DuplicateErrf is a helper function to create a new error of type TypeDuplicate which also accepts an original error, with formatted message
func Duplicatef ¶
Duplicatef is a helper function to create a new error of type TypeDuplicate, with formatted message
func EmptyErr ¶
EmptyErr is a helper function to create a new error of type TypeEmpty which also accepts an original error
func EmptyErrf ¶
EmptyErr is a helper function to create a new error of type TypeEmpty which also accepts an original error, with formatted message
func Emptyf ¶
Emptyf is a helper function to create a new error of type TypeEmpty, with formatted message
func Errorf ¶
Errorf is a convenience method to create a new instance of Error with formatted message Important: %w directive is not supported, use fmt.Errorf if you're using the %w directive or use Wrap/Wrapf to wrap an error.
func InputBodyErr ¶
InputBodyErr is a helper function to create a new error of type TypeInputBody which also accepts an original error
func InputBodyErrf ¶
InputBodyErrf is a helper function to create a new error of type TypeInputBody which also accepts an original error, with formatted message
func InputBodyf ¶
InputBodyf is a helper function to create a new error of type TypeInputBody, with formatted message
func InternalErr ¶
InternalErr helper method for creation internal errors which also accepts an original error
func InternalErrf ¶
InternalErr helper method for creation internal errors which also accepts an original error, with formatted message
func MaximumAttempts ¶
MaximumAttempts is a helper function to create a new error of type TypeMaximumAttempts
func MaximumAttemptsErr ¶
MaximumAttemptsErr is a helper function to create a new error of type TypeMaximumAttempts which also accepts an original error
func MaximumAttemptsErrf ¶
MaximumAttemptsErr is a helper function to create a new error of type TypeMaximumAttempts which also accepts an original error, with formatted message
func MaximumAttemptsf ¶
MaximumAttemptsf is a helper function to create a new error of type TypeMaximumAttempts, with formatted message
func NewWithErrMsgType ¶
NewWithErrMsgType returns an error instance with custom error type and message
func NewWithErrMsgTypef ¶
NewWithErrMsgTypef returns an error instance with custom error type and formatted message
func NewWithType ¶
NewWithType returns an error instance with custom error type
func NewWithTypef ¶
NewWithTypef returns an error instance with custom error type. And formatted message
func NotFoundErr ¶
NotFoundErr is a helper function to create a new error of type TypeNotFound which also accepts an original error
func NotFoundErrf ¶
NotFoundErrf is a helper function to create a new error of type TypeNotFound which also accepts an original error, with formatted message
func NotFoundf ¶
NotFoundf is a helper function to create a new error of type TypeNotFound, with formatted message
func SubscriptionExpired ¶
SubscriptionExpired is a helper function to create a new error of type TypeSubscriptionExpired
func SubscriptionExpiredErr ¶
SubscriptionExpiredErr is a helper function to create a new error of type TypeSubscriptionExpired which also accepts an original error
func SubscriptionExpiredErrf ¶
SubscriptionExpiredErrf is a helper function to create a new error of type TypeSubscriptionExpired which also accepts an original error, with formatted message
func SubscriptionExpiredf ¶
SubscriptionExpiredf is a helper function to create a new error of type TypeSubscriptionExpired, with formatted message
func Unauthenticated ¶
Unauthenticated is a helper function to create a new error of type TypeUnauthenticated
func UnauthenticatedErr ¶
UnauthenticatedErr is a helper function to create a new error of type TypeUnauthenticated which also accepts an original error
func UnauthenticatedErrf ¶
UnauthenticatedErrf is a helper function to create a new error of type TypeUnauthenticated which also accepts an original error, with formatted message
func Unauthenticatedf ¶
Unauthenticatedf is a helper function to create a new error of type TypeUnauthenticated, with formatted message
func Unauthorized ¶
Unauthorized is a helper function to create a new error of type TypeUnauthorized
func UnauthorizedErr ¶
UnauthorizedErr is a helper function to create a new error of type TypeUnauthorized which also accepts an original error
func UnauthorizedErrf ¶
UnauthorizedErrf is a helper function to create a new error of type TypeUnauthorized which also accepts an original error, with formatted message
func Unauthorizedf ¶
Unauthorizedf is a helper function to create a new error of type TypeUnauthorized, with formatted message
func Validation ¶
Validation is a helper function to create a new error of type TypeValidation
func ValidationErr ¶
ValidationErr helper method for creation validation errors which also accepts an original error
func ValidationErrf ¶
ValidationErr helper method for creation validation errors which also accepts an original error, with formatted message
func Validationf ¶
Validationf is a helper function to create a new error of type TypeValidation, with formatted message
func Wrap ¶
Wrap is used to simply wrap an error with optional message; error type would be the default error type set using SetDefaultType; TypeInternal otherwise If the error being wrapped is already of type Error, then its respective type is used
func WrapWithMsg
deprecated
func (*Error) ErrorWithoutFileLine ¶
ErrorWithoutFileLine prints the final string without the stack trace / file+line number
func (*Error) Format ¶
Format implements the verbs/directives supported by Error to be used in fmt annotated/formatted strings
%v - the same output as Message(). i.e. recursively get all the custom messages set by user
- if any of the wrapped error is not of type *Error, that will *not* be displayed
%+v - recursively prints all the messages along with the file & line number. Also includes output of `Error()` of non *Error types.
%s - identical to %v %+s - recursively prints all the messages without file & line number. Also includes output `Error()` of non *Error types.
func (*Error) HTTPStatusCode ¶
HTTPStatusCode is a convenience method used to get the appropriate HTTP response status code for the respective error type
func (*Error) Message ¶
Message returns the user friendly message stored in the error struct. It will ignore all errors which are not of type *Error
func (*Error) ProgramCounters ¶
func (*Error) RuntimeFrames ¶
func (*Error) StackTrace ¶
func (*Error) StackTraceCustomFormat ¶
StackTraceCustomFormat lets you prepare a stacktrace in a custom format
Supported directives: %m - message %p - file path %l - line %f - function