Documentation
¶
Index ¶
- Constants
- func ErrorCode(err error) int
- func Invalid(path string, message string) error
- func NotFound(err error) bool
- func Report(err error) error
- func RootCause(err error) error
- func SetErrorCode(err error, code int)
- type ErrorCodeGetter
- type ErrorCodeSetter
- type MultiError
- type Plugin
- type PluginList
- type SingleError
- type Unwrapper
- type ValidationError
Examples ¶
Constants ¶
const ( // CodeBadRequestError indicates that the request is not properly formatted. CodeBadRequestError = 400 // CodeForbiddenError means that the current user does not have the required permissions to access the requested resource. CodeForbiddenError = 403 // CodeNotFoundError represents a request for a resource that does not exist, such as a database query that returns "not found" CodeNotFoundError = 404 // CodeValidationError represents a request that has invalid input. CodeValidationError = 422 // CodeInternalError represents a generic error message, given when an unexpected condition was encountered and no more specific message is suitable. CodeInternalError = 500 )
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 Invalid ¶ added in v0.16.0
Invalid returns a fully populated ValidationError to the caller
Example ¶
// Derp includes a custom error type for data validation, that tracks
// the name (or path) of the invalid field and the reason that it is invalid
err := Invalid("field.name", "Field is required, or is too short, or is something else we don't like.")
// ValidationErrors work anywhere that a standard error works
fmt.Println(err.Error())
// Derp also calculates the HTTP error code for ValidationErrors, which is 422 "Unprocessable Entity".
fmt.Println(ErrorCode(err))
Output: Field is required, or is too short, or is something else we don't like. 422
func Report ¶
Report takes ANY error (hopefully a derp error) and attempts to report it via all configured error reporting mechanisms.
func RootCause ¶
RootCause digs into the error stack and returns the original error that caused the DERP
func SetErrorCode ¶ added in v0.16.0
SetErrorCode tries to set an error code for the provided error. If the error matches the ErrorCodeSetter interface, then the code is set directly in the error. Otherwise, it has no effect.
Types ¶
type ErrorCodeGetter ¶
type ErrorCodeGetter interface {
// ErrorCode returns a numeric, application-specific code that references this error.
// HTTP status codes are recommended, but not required
ErrorCode() int
}
ErrorCodeGetter interface describes any error that can also "get" an error code value
type ErrorCodeSetter ¶ added in v0.16.0
type ErrorCodeSetter interface {
// SetErrorCode sets a numeric, application-specific code that for this error.
// HTTP status codes are recommended, but not required
SetErrorCode(int)
}
ErrorCodeSetter interface describes any error that can also "set" an error code value
type MultiError ¶ added in v0.16.0
type MultiError struct {
Errors []error
}
MultiError represents a runtime error. It includes
Example ¶
// MultiError type contains multiple errors in a single data structure
err1 := New(500, "Code Location", "Error Message", "works with native derp errors")
err2 := Invalid("user.name", "Works with validation errors")
err3 := errors.New("Works with standard library errors")
// Multiple errors appended into a single slice
multi := Append(err1, err2, err3)
// MultiErrors can be used anywhere a standard Error can be
fmt.Println(multi.Error())
Output: Code Location: Error Message Works with validation errors Works with standard library errors
func Append ¶ added in v0.16.0
func Append(errs ...error) *MultiError
Append takes one or more errors and combines them into this MultiError. If one of the arguments is itself a MultiError, then its slice of errors is flattened into this one, so that the final result is a single, one-dimensional slice of errors.
func (*MultiError) Error ¶ added in v0.16.0
func (err *MultiError) Error() string
Error implements the Error interface, which allows derp.Error objects to be used anywhere a standard error is used.
func (*MultiError) ErrorCode ¶ added in v0.16.0
func (err *MultiError) ErrorCode() int
ErrorCode returns the error Code embedded in this Error. This is useful for matching interfaces in other package.
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.
type SingleError ¶ added in v0.16.0
type SingleError 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
TimeStamp time.Time `json:"timestamp"` // Unix Epoch timestamp of the date/time when this error was created
Details []interface{} `json:"details"` // Additional information related to this error message, such as parameters to the function that caused the error.
InnerError error `json:"innerError"` // An underlying error object used to identify the root cause of this error.
}
SingleError represents a runtime error. It includes
func New ¶
func New(code int, location string, message string, details ...interface{}) *SingleError
New returns a new Error object
Example ¶
// Derp errors work anywhere that you use normal errors.
// They just contain more information about what actually happened.
// Here's how to create a new error to report back to a caller
err := New(404, "Code Location", "Error Message", "additional details here", 12345, map[string]interface{}{})
// Pluggable error reporting interface can dump errors to the console
// or anywhere else that you want to send them.
Report(err)
func Wrap ¶
func Wrap(inner error, location string, message string, details ...interface{}) *SingleError
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 := New(404, "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`", "additional details go here")
// Additional data (such as a custom error code) can be added here.
SetErrorCode(result, 404)
// Call .Report() to send an error to Ops. This is a system-wide
// configuration that's set up during initialization.
Report(result)
}
func (*SingleError) Error ¶ added in v0.16.0
func (err *SingleError) Error() string
Error implements the Error interface, which allows derp.Error objects to be used anywhere a standard error is used.
func (*SingleError) ErrorCode ¶ added in v0.16.0
func (err *SingleError) ErrorCode() int
ErrorCode returns the error Code embedded in this Error. This is useful for matching interfaces in other package.
func (*SingleError) SetErrorCode ¶ added in v0.16.0
func (err *SingleError) SetErrorCode(code int)
SetErrorCode returns the error Code embedded in this Error. This is useful for matching interfaces in other package.
func (*SingleError) Unwrap ¶ added in v0.16.0
func (err *SingleError) Unwrap() error
Unwrap supports Go 1.13+ error unwrapping
type Unwrapper ¶
type Unwrapper interface {
// Unwrap returns the inner error bundled inside of an outer error.
Unwrap() error
}
Unwrapper interface describes any error that can be "unwrapped". It supports the Unwrap method added in Go 1.13+
type ValidationError ¶ added in v0.16.0
type ValidationError struct {
Path string `json:"path"` // Identifies the PATH (or variable name) that has invalid input
Message string `json:"message"` // Human-readable message that explains the problem with the input value.
}
ValidationError represents an input validation error, and includes fields necessary to report problems back to the end user.
func (*ValidationError) Error ¶ added in v0.16.0
func (v *ValidationError) Error() string
Error returns a string representation of this ValidationError, and implements the builtin errors.error interface.
func (*ValidationError) ErrorCode ¶ added in v0.16.0
func (v *ValidationError) ErrorCode() int
ErrorCode returns CodeValidationError for this ValidationError It implements the ErrorCodeGetter interface.