Documentation
¶
Index ¶
- func Details(err error) []any
- func ErrorCode(err error) int
- func IsBadRequest(err error) bool
- func IsClientError(err error) bool
- func IsForbidden(err error) bool
- func IsInformational(err error) bool
- func IsInternalServerError(err error) bool
- func IsMisdirectedRequest(err error) bool
- func IsNil(i error) bool
- func IsNilOrNotFound(err error) bool
- func IsNotFound(err error) bool
- func IsNotImplemented(err error) bool
- func IsRedirection(err error) bool
- func IsServerError(err error) bool
- func IsSuccess(err error) bool
- func IsTeapot(err error) bool
- func IsUnauthorized(err error) bool
- func IsValidationError(err error) bool
- func Location(err error) string
- func Message(err error) string
- func NilOrNotFound(err error) booldeprecated
- func NotFound(err error) booldeprecated
- func NotNil(err error) bool
- func Report(err error)
- func ReportAndReturn(err error) error
- func RootCause(err error) error
- func URL(err error) string
- func Wrap(inner error, location string, message string, details ...any) error
- type DetailsGetter
- type Error
- func BadRequestError(location string, message string, details ...any) Error
- func ForbiddenError(location string, message string, details ...any) Error
- func InternalError(location string, message string, details ...any) Error
- func MisdirectedRequestError(location string, message string, details ...any) Error
- func NewBadRequestError(location string, message string, details ...any) Errordeprecated
- func NewForbiddenError(location string, message string, details ...any) Errordeprecated
- func NewInternalError(location string, message string, details ...any) Errordeprecated
- func NewMisdirectedRequestError(location string, message string, details ...any) Errordeprecated
- func NewNotFoundError(location string, message string, details ...any) Errordeprecated
- func NewNotImplementedError(location string, message string, details ...any) Errordeprecated
- func NewTeapotError(location string, message string, details ...any) Errordeprecated
- func NewUnauthorizedError(location string, message string, details ...any) Errordeprecated
- func NewValidationError(message string, details ...any) Errordeprecated
- func NotFoundError(location string, message string, details ...any) Error
- func NotImplementedError(location string, details ...any) Error
- func TeapotError(location string, message string, details ...any) Error
- func UnauthorizedError(location string, message string, details ...any) Error
- func ValidationError(message string, details ...any) Error
- func (err Error) Error() string
- func (err Error) GetDetails() []any
- func (err Error) GetErrorCode() int
- func (err Error) GetLocation() string
- func (err Error) GetMessage() string
- func (err Error) GetTimeStamp() int64
- func (err Error) GetURL() string
- func (err Error) GetWrappedValue() error
- func (err Error) Unwrap() error
- type ErrorCodeGetter
- type LocationGetter
- type MessageGetter
- type Option
- type Plugin
- type PluginList
- type URLGetter
- type Unwrapper
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ErrorCode ¶
ErrorCode returns an error code for any error. It tries to read the error code from objects matching the ErrorCodeGetter interface. If the provided error does not match this interface, then it assigns a generic "Internal Server Error" code 500.
func IsBadRequest ¶ added in v0.32.2
IsBadReqeust returns TRUE if this is a 400 / Bad Request error.
func IsClientError ¶
IsClientError returns TRUE if the error `Code` is a 4xx / Client Error error. https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_client_errors
func IsForbidden ¶ added in v0.32.2
IsForbidden returns TRUE if this is a 403 / Forbidden error.
func IsInformational ¶
IsInformational returns TRUE if the error `Code` is a 1xx / Informational error. https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#1xx_informational_response
func IsInternalServerError ¶ added in v0.32.2
IsInternalServerError returns TRUE if this is a 500 / Internal Server Error error.
func IsMisdirectedRequest ¶ added in v0.32.2
IsMisdirectedRequest returns TRUE if this is a 421 / Misdirected Request error.
func IsNil ¶ added in v0.32.2
IsNil performs a robust nil check on an error interface Shout out to: https://medium.com/@mangatmodi/go-check-nil-interface-the-right-way-d142776edef1
func IsNilOrNotFound ¶ added in v0.32.1
NilOrNotFound returns TRUE if the error is nil or a 404 / Not Found error. All other errors return FALSE
func IsNotFound ¶ added in v0.32.1
IsNotFound returns TRUE if this is a 404 / Not Found error.
func IsNotImplemented ¶ added in v0.32.2
IsNotImplemented returns TRUE if this is a 501 / Not Implemented error.
func IsRedirection ¶
IsRedirection returns TRUE if the error `Code` is a 3xx / Redirection error. https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_redirection
func IsServerError ¶
IsServerError returns TRUE if the error `Code` is a 5xx / Server Error error. https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#5xx_server_errors
func IsSuccess ¶
IsSuccess returns TRUE if the error `Code` is a 2xx / Success error. https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#2xx_success
func IsUnauthorized ¶ added in v0.32.2
IsUnauthorized returns TRUE if this is a 401 / Unauthorized error.
func IsValidationError ¶ added in v0.32.2
IsValidationError returns TRUE if this is a 422 / Validation error.
func NilOrNotFound
deprecated
func Report ¶
func Report(err error)
Report takes ANY error (hopefully a derp error) and attempts to report it via all configured error reporting mechanisms.
func ReportAndReturn ¶
ReportAndReturn reports an error to the logger and also returns it to the caller.
func RootCause ¶
RootCause digs into the error stack and returns the original error that caused the DERP
func Wrap ¶
Wrap encapsulates an existing derp.Error
Example ¶
// Derp errors can be nested, containing detailed information
// about the entire call stack, with specifics about what went
// wrong at every level
innerErr := NewNotFoundError("Inner Function", "Original Error")
middleErr := Wrap(innerErr, "Middleware Function", "Error calling 'innerErr'", "parameter", "list", "here")
outerErr := Wrap(middleErr, "Error in Main Function", "Error calling 'middleErr'", "suspected", "cause", "of", "the", "error")
Report(outerErr)
Example (StandardErrors) ¶
// Wrap also works with standard library errors
// so you can add information to errors that are
// exported by other packages that don't use derp.
thisBreaks := func() error {
return errors.New("Something failed")
}
// Try something that fails
if err := thisBreaks(); err != nil {
// Populate a derp.Error with everything you know about the error
result := Wrap(err, "Example", "Something broke in `thisBreaks`", WithCode(404), "additional details go here")
// Call .Report() to send an error to Ops. This is a system-wide
// configuration that's set up during initialization.
Report(result)
}
Types ¶
type DetailsGetter ¶ added in v0.32.3
type DetailsGetter interface {
// Details returns a list of details about the error.
GetDetails() []any
}
DetailsGetter interface wraps the GetDetails method, which returns a list of details about the error
type Error ¶
type Error struct {
Code int `json:"code"` // Numeric error code (such as an HTTP status code) to report to the client.
Location string `json:"location"` // Function name (or other location description) of where the error occurred
Message string `json:"message"` // Primary (top-level) error message for this error
URL string `json:"url,omitempty"` // URL to a web page with more information about this error
Details []any `json:"details,omitempty"` // Additional information related to this error message, such as parameters to the function that caused the error.
TimeStamp int64 `json:"timestamp"` // Unix Epoch timestamp of the date/time when this error was created
WrappedValue error `json:"innerError,omitempty"` // An underlying error object used to identify the root cause of this error.
}
Error represents a runtime error. It includes
func BadRequestError ¶ added in v0.32.1
BadRequestError returns a (400) Bad Request error which indicates that the request is not properly formatted. https://www.rfc-editor.org/rfc/rfc9110.html#name-400-bad-request
func ForbiddenError ¶ added in v0.32.1
ForbiddenError returns a (403) Forbidden error which indicates that the current user does not have permissions to access the requested resource. https://www.rfc-editor.org/rfc/rfc9110.html#name-403-forbidden
func InternalError ¶ added in v0.32.1
InternalError returns a (500) Internal Server Error which represents a generic error message, given when an unexpected condition was encountered and no more specific message is suitable. https://www.rfc-editor.org/rfc/rfc9110.html#name-500-internal-server-error
func MisdirectedRequestError ¶ added in v0.32.1
MisdirectedRequestError returns a (421) Misdirected Request error. which indicates that the request was made to the wrong server; that server is not able to produce a response. https://www.rfc-editor.org/rfc/rfc9110.html#name-421-misdirected-request
func NewBadRequestError
deprecated
func NewForbiddenError
deprecated
func NewInternalError
deprecated
func NewMisdirectedRequestError
deprecated
func NewNotFoundError
deprecated
func NewNotImplementedError
deprecated
added in
v0.32.0
func NewTeapotError
deprecated
func NewUnauthorizedError
deprecated
func NewValidationError
deprecated
func NotFoundError ¶ added in v0.32.1
NotFoundError returns a (404) Not Found error which indicates that the requested resource does not exist, such as when database query returns "not found" https://www.rfc-editor.org/rfc/rfc9110.html#name-404-not-found
func NotImplementedError ¶ added in v0.32.1
NotImplementedError returns a (501) Not Implemented error which indicates that the server does not support the functionality required to fulfill the request. https://www.rfc-editor.org/rfc/rfc9110.html#name-501-not-implemented
func TeapotError ¶ added in v0.32.1
TeapotError returns a (418) I'm a Teapot error which indicates that the server is a teapot that cannot serve HTTP requests. https://www.rfc-editor.org/rfc/rfc7168.html#name-418-im-a-teapot https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#418
func UnauthorizedError ¶ added in v0.32.1
UnauthorizedError returns a (401) Unauthorized error which indicates that the request requires user authentication. https://www.rfc-editor.org/rfc/rfc9110.html#name-401-unauthorized
func ValidationError ¶ added in v0.16.0
ValidationError returns a (422) Validation error which indicates that the request contains invalid data. https://www.rfc-editor.org/rfc/rfc9110.html#name-422-unprocessable-content
func (Error) Error ¶
Error implements the Error interface, which allows derp.Error objects to be used anywhere a standard error is used.
func (Error) GetDetails ¶ added in v0.32.3
GetDetails returns the error Details embedded in this Error.
func (Error) GetErrorCode ¶
ErrorCode returns the error Code embedded in this Error.
func (Error) GetLocation ¶ added in v0.32.3
GetLocation returns the error Location embedded in this Error.
func (Error) GetMessage ¶
GetMessage returns the error Message embedded in this Error.
func (Error) GetTimeStamp ¶ added in v0.32.3
GetTimestamp returns the error TimeStamp embedded in this Error.
func (Error) GetWrappedValue ¶ added in v0.32.3
GetWrappedValue returns the error WrappedValue embedded in this Error.
type ErrorCodeGetter ¶
type ErrorCodeGetter interface {
// ErrorCode returns a numeric, application-specific code that references this error.
// HTTP status codes are recommended, but not required
GetErrorCode() int
}
ErrorCodeGetter interface wraps the GetErrorCode method, which returns a numeric, application-specific code that references this error
type LocationGetter ¶ added in v0.32.3
type LocationGetter interface {
// Location returns the location of the error in the source code.
GetLocation() string
}
LocationGetter interface wraps the GetLocation method, which returns the location of the error
type MessageGetter ¶
type MessageGetter interface {
// Message returns a human-friendly string representation of the error.
GetMessage() string
}
MessageGetter interface wraps the GetMessage method, which returns a human-friendly string representation of the error
type Option ¶
type Option func(*Error)
Option defines a function that modifies a derp.Error
func WithBadRequest ¶
func WithBadRequest() Option
WithBadRequest returns an option that sets the derp.Error code to 400 (Bad Request)
func WithForbidden ¶
func WithForbidden() Option
WithForbidden returns an option that sets the derp.Error code to 403 (Forbidden)
func WithInternalError ¶
func WithInternalError() Option
WithInternalError returns an option that sets the derp.Error code to 500 (Internal Server Error)
func WithLocation ¶
WithLocation returns an option that sets the derp.Error location
func WithMessage ¶
WithMessage returns an option that sets the derp.Error message
func WithNotFound ¶
func WithNotFound() Option
WithNotFound returns an option that sets the derp.Error code to 404 (Not Found)
func WithWrappedValue ¶
WithWrappedValue returns an option that sets the derp.Error wrapped value
type Plugin ¶
type Plugin interface {
Report(error)
}
Plugin wraps the "Report" method, which reports a derp error to an external source. Reporters are responsible for handling and swallowing any errors they generate.
type PluginList ¶
type PluginList []Plugin
PluginList represents an array of plugins, which will be called in succession whenever the Error.Report() function is called.
var Plugins PluginList
Plugins is the array of objects that are able to report a derp when err.Report() is called.
func (PluginList) Add ¶
func (list PluginList) Add(plugin Plugin) PluginList
Add registers a new plugin to the system-wide configuration. This lets the developer configure and append additional plugins during initialization. It should be called during system startup only.
func (PluginList) Clear ¶
func (list PluginList) Clear() PluginList
Clear removes all plugins from the system-wide configuration. It is useful for removing the library default Console() from the list of plugins, in the event that you don't want to report errors to the console.