errors package - github.com/naughtygopher/errors - Go Packages (original) (raw)
Package errors helps in wrapping errors with custom type as well as a user friendly message. This is particularly useful when responding to APIs
- Constants
- func As(err error, target interface{}) bool
- func ErrWithoutTrace(err error) (string, bool)
- func GRPCStatusCode(err error) (codes.Code, bool)
- func GRPCStatusCodeMessage(err error) (codes.Code, 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 ContextCancelled(message string) *Error
- func ContextCancelledErr(original error, message string) *Error
- func ContextCancelledErrf(original error, format string, args ...any) *Error
- func ContextCancelledf(format string, args ...any) *Error
- func ContextTimedout(message string) *Error
- func ContextTimedoutf(format string, args ...any) *Error
- func DownstreamDependencyTimedout(message string) *Error
- func DownstreamDependencyTimedoutErr(original error, message string) *Error
- func DownstreamDependencyTimedoutErrf(original error, format string, args ...any) *Error
- func DownstreamDependencyTimedoutf(format string, args ...any) *Error
- func Duplicate(message string) *Error
- func DuplicateErr(original error, message string) *Error
- func DuplicateErrf(original error, format string, args ...any) *Error
- func Duplicatef(format string, args ...any) *Error
- func Empty(message string) *Error
- func EmptyErr(original error, message string) *Error
- func EmptyErrf(original error, format string, args ...any) *Error
- func Emptyf(format string, args ...any) *Error
- func Errorf(fromat string, args ...any) *Error
- func InputBody(message string) *Error
- func InputBodyErr(original error, message string) *Error
- func InputBodyErrf(original error, format string, args ...any) *Error
- func InputBodyf(format string, args ...any) *Error
- func Internal(message string) *Error
- func InternalErr(original error, message string) *Error
- func InternalErrf(original error, format string, args ...any) *Error
- func Internalf(format string, args ...any) *Error
- func MaximumAttempts(message string) *Error
- func MaximumAttemptsErr(original error, message string) *Error
- func MaximumAttemptsErrf(original error, format string, args ...any) *Error
- func MaximumAttemptsf(format string, args ...any) *Error
- func New(msg string) *Error
- func NewWithErrMsgType(original error, message string, etype errType) *Error
- func NewWithErrMsgTypef(original error, etype errType, format string, args ...any) *Error
- func NewWithType(msg string, etype errType) *Error
- func NewWithTypef(etype errType, format string, args ...any) *Error
- func Newf(fromat string, args ...any) *Error
- func NotFound(message string) *Error
- func NotFoundErr(original error, message string) *Error
- func NotFoundErrf(original error, format string, args ...any) *Error
- func NotFoundf(format string, args ...any) *Error
- func NotImplemented(message string) *Error
- func NotImplementedErr(original error, message string) *Error
- func NotImplementedErrf(original error, format string, args ...any) *Error
- func NotImplementedf(format string, args ...any) *Error
- func SubscriptionExpired(message string) *Error
- func SubscriptionExpiredErr(original error, message string) *Error
- func SubscriptionExpiredErrf(original error, format string, args ...any) *Error
- func SubscriptionExpiredf(format string, args ...any) *Error
- func Unauthenticated(message string) *Error
- func UnauthenticatedErr(original error, message string) *Error
- func UnauthenticatedErrf(original error, format string, args ...any) *Error
- func Unauthenticatedf(format string, args ...any) *Error
- func Unauthorized(message string) *Error
- func UnauthorizedErr(original error, message string) *Error
- func UnauthorizedErrf(original error, format string, args ...any) *Error
- func Unauthorizedf(format string, args ...any) *Error
- func Validation(message string) *Error
- func ValidationErr(original error, message string) *Error
- func ValidationErrf(original error, format string, args ...any) *Error
- func Validationf(format string, args ...any) *Error
- func Wrap(original error, msg ...string) *Error
- func WrapWithMsg(original error, msg string) *Errordeprecated
- func Wrapf(original error, format string, args ...any) *Error
- func (e *Error) Error() string
- func (e *Error) ErrorWithoutFileLine() string
- func (e *Error) Format(s fmt.State, verb rune)
- func (e *Error) HTTPStatusCode() intdeprecated
- 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
const (
TypeInternal errType = [iota](/builtin#iota)
TypeValidation
TypeInputBody
TypeDuplicate
TypeUnauthenticated
TypeUnauthorized
TypeEmpty
TypeNotFound
TypeMaximumAttempts
TypeSubscriptionExpired
TypeDownstreamDependencyTimedout
TypeNotImplemented
TypeContextTimedout
TypeContextCancelled)
While adding a new Type, the respective helper functions should be added, also update the WriteHTTP method accordingly
const (
DefaultMessage = "unknown error occurred")
This section is empty.
As calls the Go builtin errors.As
ErrWithoutTrace is a duplicate of Message, but with clearer name. The boolean is 'true' if the provided err is of type *Error
GRPCStatusCode returns appropriate GRPC response status code based on type of the error. The boolean is 'true' if the provided error is of type *Err. If joined error, boolean is true if all joined errors are of type *Error In case of joined errors, it'll return the status code of the last *Error
GRPCStatusCodeMessage returns the appropriate GRPC status code, message, boolean for the error the boolean value is true if the error was of type *Error, false otherwise.
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. If joined error, boolean is true if all joined errors are of type *Error In case of joined errors, it'll return the status code of the last *Error
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.
HasType will check if the provided err type is available anywhere nested in the error
Is calls the Go builtin errors.Is
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 SetDefaultType(e errType)
SetDefaultType will set the default error type, which is used in the 'New' function
Stacktrace returns a string representation of the stacktrace, where each trace is separated by a newline and tab '\t'
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
Stacktrace returns a string representation of the stacktrace, as a slice of string where each element represents the error message and traces.
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
Type returns the errType as integer if it's an instance of *Error, -1 otherwise
Unwrap calls the Go builtin errors.UnUnwrap
WriteHTTP is a convenience method which will check if the error is of type *Error and respond appropriately
Error is the struct which holds custom attributes
func ContextCancelled(message string) *Error
ContextCancelled is a helper function to create a new error of type TypeContextCancelled
func ContextCancelledErr(original error, message string) *Error
ContextCancelledErr is a helper function to create a new error of type TypeContextCancelled which also accepts an original error
ContextCancelledErrf is a helper function to create a new error of type TypeContextCancelled which also accepts an original error, with formatted message
func ContextCancelledf(format string, args ...any) *Error
ContextCancelledf is a helper function to create a new error of type TypeContextCancelled, with formatted message
func ContextTimedout(message string) *Error
TypeContextTimedout is a helper function to create a new error of type TypeContextTimedout
func ContextTimedoutf(format string, args ...any) *Error
ContextTimedoutf is a helper function to create a new error of type TypeContextTimedout, with formatted message
func DownstreamDependencyTimedout(message string) *Error
DownstreamDependencyTimedout is a helper function to create a new error of type TypeDownstreamDependencyTimedout
func DownstreamDependencyTimedoutErr(original error, message string) *Error
DownstreamDependencyTimedoutErr is a helper function to create a new error of type TypeDownstreamDependencyTimedout which also accepts an original error
func DownstreamDependencyTimedoutErrf(original error, format string, args ...any) *Error
DownstreamDependencyTimedoutErrf is a helper function to create a new error of type TypeDownstreamDependencyTimedout which also accepts an original error, with formatted message
func DownstreamDependencyTimedoutf(format string, args ...any) *Error
DownstreamDependencyTimedoutf is a helper function to create a new error of type TypeDownstreamDependencyTimedout, with formatted message
Duplicate is a helper function to create a new error of type TypeDuplicate
DuplicateErr is a helper function to create a new error of type TypeDuplicate which also accepts an original error
DuplicateErrf is a helper function to create a new error of type TypeDuplicate which also accepts an original error, with formatted message
Duplicatef is a helper function to create a new error of type TypeDuplicate, with formatted message
Empty is a helper function to create a new error of type TypeEmpty
EmptyErr is a helper function to create a new error of type TypeEmpty which also accepts an original error
EmptyErr is a helper function to create a new error of type TypeEmpty which also accepts an original error, with formatted message
Emptyf is a helper function to create a new error of type TypeEmpty, with formatted message
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 InputBody ¶
InputBody is a helper function to create a new error of type TypeInputBody
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
Internal helper method for creating internal errors
InternalErr helper method for creation internal errors which also accepts an original error
InternalErr helper method for creation internal errors which also accepts an original error, with formatted message
Internalf helper method for creating internal errors with formatted message
func MaximumAttempts(message string) *Error
MaximumAttempts is a helper function to create a new error of type TypeMaximumAttempts
MaximumAttemptsErr is a helper function to create a new error of type TypeMaximumAttempts which also accepts an original error
MaximumAttemptsErr is a helper function to create a new error of type TypeMaximumAttempts which also accepts an original error, with formatted message
func MaximumAttemptsf(format string, args ...any) *Error
MaximumAttemptsf is a helper function to create a new error of type TypeMaximumAttempts, with formatted message
New returns a new instance of Error with the relavant fields initialized
func NewWithErrMsgType(original error, message string, etype errType) *Error
NewWithErrMsgType returns an error instance with custom error type and message
func NewWithErrMsgTypef(original error, etype errType, format string, args ...any) *Error
NewWithErrMsgTypef returns an error instance with custom error type and formatted message
func NewWithType(msg string, etype errType) *Error
NewWithType returns an error instance with custom error type
func NewWithTypef(etype errType, format string, args ...any) *Error
NewWithTypef returns an error instance with custom error type. And formatted message
NotFound is a helper function to create a new error of type TypeNotFound
NotFoundErr is a helper function to create a new error of type TypeNotFound which also accepts an original error
NotFoundErrf is a helper function to create a new error of type TypeNotFound which also accepts an original error, with formatted message
NotFoundf is a helper function to create a new error of type TypeNotFound, with formatted message
func NotImplemented(message string) *Error
NotImplemented is a helper function to create a new error of type TypeNotImplemented
NotImplementedErr is a helper function to create a new error of type TypeNotImplemented which also accepts an original error
NotImplementedErrf is a helper function to create a new error of type TypeNotImplemented which also accepts an original error, with formatted message
func NotImplementedf(format string, args ...any) *Error
NotImplementedf is a helper function to create a new error of type TypeNotImplemented, with formatted message
func SubscriptionExpired(message string) *Error
SubscriptionExpired is a helper function to create a new error of type TypeSubscriptionExpired
func SubscriptionExpiredErr(original error, message string) *Error
SubscriptionExpiredErr is a helper function to create a new error of type TypeSubscriptionExpired which also accepts an original error
func SubscriptionExpiredErrf(original error, format string, args ...any) *Error
SubscriptionExpiredErrf is a helper function to create a new error of type TypeSubscriptionExpired which also accepts an original error, with formatted message
func SubscriptionExpiredf(format string, args ...any) *Error
SubscriptionExpiredf is a helper function to create a new error of type TypeSubscriptionExpired, with formatted message
func Unauthenticated(message string) *Error
Unauthenticated is a helper function to create a new error of type TypeUnauthenticated
UnauthenticatedErr is a helper function to create a new error of type TypeUnauthenticated which also accepts an original error
UnauthenticatedErrf is a helper function to create a new error of type TypeUnauthenticated which also accepts an original error, with formatted message
func Unauthenticatedf(format string, args ...any) *Error
Unauthenticatedf is a helper function to create a new error of type TypeUnauthenticated, with formatted message
func Unauthorized(message string) *Error
Unauthorized is a helper function to create a new error of type TypeUnauthorized
UnauthorizedErr is a helper function to create a new error of type TypeUnauthorized which also accepts an original error
UnauthorizedErrf is a helper function to create a new error of type TypeUnauthorized which also accepts an original error, with formatted message
func Unauthorizedf(format string, args ...any) *Error
Unauthorizedf is a helper function to create a new error of type TypeUnauthorized, with formatted message
func Validation(message string) *Error
Validation is a helper function to create a new error of type TypeValidation
ValidationErr helper method for creation validation errors which also accepts an original error
ValidationErr helper method for creation validation errors which also accepts an original error, with formatted message
Validationf is a helper function to create a new error of type TypeValidation, with formatted message
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
Deprecated: WrapWithMsg [deprecated, use `Wrap`] wrap error with a user friendly message
Error is the implementation of error interface
func (e *Error) ErrorWithoutFileLine() string
ErrorWithoutFileLine prints the final string without the stack trace / file+line number
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 (e *Error) HTTPStatusCode() int
Deprecated: HTTPStatusCode is a convenience method used to get the appropriate HTTP response status code for the respective error type. deprecated to free the Error type from protocol specific features
Is implements the Is interface required by Go
Message returns the user friendly message stored in the error struct. It will ignore all errors which are not of type *Error
func (e *Error) ProgramCounters() []uintptr
StackTraceCustomFormat lets you prepare a stacktrace in a custom format
Supported directives: %m - message %p - file path %l - line %f - function
func (e *Error) StackTraceNoFormat() []string
func (e *Error) Type() errType
Type returns the error type as integer
Unwrap implement's Go 1.13's Unwrap interface exposing the wrapped error