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 HTTPStatusCodeMessage(err error) (int, string, bool)
- func Is(err, target error) bool
- func SetDefaultType(e errType)
- 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 Duplicate(message string) *Error
- func DuplicateErr(original error, message string) *Error
- func Empty(message string) *Error
- func EmptyErr(original error, message string) *Error
- func InputBody(message string) *Error
- func InputBodyErr(original error, message string) *Error
- func InternalErr(original error, message string) *Error
- func MaximumAttempts(message string) *Error
- func MaximumAttemptsErr(original error, message string) *Error
- func New(msg string) *Error
- func NewWithErrMsgType(e error, message string, etype errType) *Error
- func NewWithType(msg string, etype errType) *Error
- func NotFound(message string) *Error
- func NotFoundErr(original error, message string) *Error
- func SubscriptionExpired(message string) *Error
- func SubscriptionExpiredErr(original error, message string) *Error
- func Unauthenticated(message string) *Error
- func UnauthenticatedErr(original error, message string) *Error
- func Unauthorized(message string) *Error
- func UnauthorizedErr(original error, message string) *Error
- func Validation(message string) *Error
- func ValidationErr(original error, message string) *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 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 SetDefaultType ¶
func SetDefaultType(e errType)
SetDefaultType will set the default error type, which is used in the 'New' function
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 DuplicateErr ¶
DuplicateErr is a helper function to create a new error of type TypeDuplicate which also accepts an original error
func EmptyErr ¶
EmptyErr is a helper function to create a new error of type TypeEmpty which also accepts an original error
func InputBodyErr ¶
InputBodyErr is a helper function to create a new error of type TypeInputBody which also accepts an original error
func InternalErr ¶
InternalErr helper method for creation internal errors which also accepts an original error
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 NewWithErrMsgType ¶
NewWithErrMsgType returns an error instance with custom error type and message
func NewWithType ¶
NewWithType returns an error instance with custom error type
func NotFoundErr ¶
NotFoundErr is a helper function to create a new error of type TypeNotFound which also accepts an original error
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 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 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 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 (*Error) HTTPStatusCode ¶
HTTPStatusCode is a convenience method used to get the appropriate HTTP response status code for the respective error type