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

View Source

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

View Source

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

%+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