Documentation ¶
Index ¶
- Constants
- Variables
- func DefaultFormatter(e *SimpleError) string
- func ExtractAuxiliary(err error) map[string]interface{}
- func GetAttribute(err error, key interface{}) (interface{}, bool)
- func HasErrorCode(err error, code Code) bool
- func IsBenign(err error) (string, bool)
- func IsRetriable(err error) bool
- func IsSilent(err error) bool
- func SetRegistry(r *Registry)
- type Call
- type Code
- type Registry
- type SimpleError
- func (e *SimpleError) Attr(key, value interface{}) *SimpleError
- func (e *SimpleError) Aux(kv ...interface{}) *SimpleError
- func (e *SimpleError) AuxMap(aux map[string]interface{}) *SimpleError
- func (e *SimpleError) Benign() *SimpleError
- func (e *SimpleError) BenignReason(reason string) *SimpleError
- func (e *SimpleError) Code(code Code) *SimpleError
- func (e *SimpleError) Error() string
- func (e *SimpleError) GetAttribute(key interface{}) (interface{}, bool)
- func (e *SimpleError) GetAuxiliary() map[string]interface{}
- func (e *SimpleError) GetBenignReason() (string, bool)
- func (e *SimpleError) GetCode() Code
- func (e *SimpleError) GetDescription() string
- func (e *SimpleError) GetMessage() string
- func (e *SimpleError) GetRetriable() bool
- func (e *SimpleError) GetSilent() bool
- func (e *SimpleError) Message(msg string, args ...interface{}) *SimpleError
- func (e *SimpleError) Retriable() *SimpleError
- func (e *SimpleError) Silence() *SimpleError
- func (e *SimpleError) StackFrames() []uintptr
- func (e *SimpleError) StackTrace() []Call
- func (e *SimpleError) Unwrap() error
Constants ¶
const NumberOfReservedCodes = 100
NumberOfReservedCodes is the code number, under which, are reserved for use by this library.
Variables ¶
var Formatter = DefaultFormatter
Formatter is the error string formatting function.
Functions ¶
func DefaultFormatter ¶ added in v0.1.1
func DefaultFormatter(e *SimpleError) string
DefaultFormatter is the default error string formatting.
func ExtractAuxiliary ¶ added in v0.1.3
ExtractAuxiliary extracts a superset of auxiliary data from all errors in the chain. Wrapper error auxiliary data take precedent over later errors.
func GetAttribute ¶ added in v0.1.4
GetAttribute gets the first instance of the key in the error chain. This can be used to define attributes on the error that do not have first-class support with simplerr. Much like keys in the `context` package, the `key` should be a custom type so it does not have naming collisions with other values.
func HasErrorCode ¶
HasErrorCode checks the error code of an error if it is a SimpleError{}. nil errors or errors that are not SimplErrors return false.
func IsBenign ¶
IsBenign checks the error or any error in the chain, is marked as benign. It also returns the reason the error was marked benign. Benign errors should be logged or handled less severely than non-benign errors. For example, you may choose to log benign errors at INFO level, rather than ERROR.
func IsRetriable ¶ added in v1.3.0
IsRetriable checks the error or any error in the chain is retriable, meaning the caller should retry the operation which caused this error in hopes of it succeeding. Errors are assumed not retriable by default unless an error in the chain says otherwise. A single error in the chain that is retriable will make the entire error retriable.
Types ¶
type Code ¶
type Code int
Code is an error code that indicates the category of error
const ( // CodeUnknown is the default code for errors that are not classified CodeUnknown Code = 0 // CodeAlreadyExists means an attempt to create an entity failed because one // already exists. CodeAlreadyExists Code = 1 // CodeNotFound means some requested entity (e.g., file or directory) was not found. CodeNotFound Code = 2 // CodeInvalidArgument indicates that the caller specified an invalid argument. CodeInvalidArgument Code = 3 // CodeMalformedRequest indicates the syntax of the request cannot be interpreted (eg JSON decoding error) CodeMalformedRequest Code = 4 // CodeUnauthenticated indicates the request does not have valid authentication credentials for the operation. CodeUnauthenticated Code = 5 // CodePermissionDenied indicates that the identity of the user is confirmed but they do not have permissions // to perform the request CodePermissionDenied Code = 6 // CodeConstraintViolated indicates that a constraint in the system has been violated. // Eg. a duplicate key error from a unique index CodeConstraintViolated Code = 7 // CodeNotSupported indicates that the request is not supported CodeNotSupported Code = 8 // CodeNotImplemented indicates that the request is not implemented CodeNotImplemented Code = 9 // CodeMissingParameter indicates that a required parameter is missing or empty CodeMissingParameter Code = 10 // CodeDeadlineExceeded indicates that a request exceeded it's deadline before completion CodeDeadlineExceeded Code = 11 // CodeCanceled indicates that the request was canceled before completion CodeCanceled Code = 12 // CodeResourceExhausted indicates that some limited resource (eg rate limit or disk space) has been reached CodeResourceExhausted Code = 13 CodeUnavailable Code = 14 )
These are common impact error codes that are found throughout our services
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry is a registry of information on how to handle and serve simple errors
func GetRegistry ¶ added in v0.1.2
func GetRegistry() *Registry
GetRegistry gets the currently set registry
func NewRegistry ¶
func NewRegistry() *Registry
NewRegistry creates a new registry without any defaults
func (*Registry) CodeDescription ¶ added in v0.1.1
CodeDescription returns the description of the error code
func (*Registry) ErrorCodes ¶
ErrorCodes returns a copy of the registered error codes and their descriptions
func (*Registry) RegisterErrorCode ¶ added in v0.1.1
RegisterErrorCode registers custom error codes in the registry. This call will panic if the error code is already registered. Error codes 0-99 are reserved for simplerr. This method should be called early on application startup.
type SimpleError ¶
type SimpleError struct {
// contains filtered or unexported fields
}
SimpleError is an implementation of the `error` interface which provides functionality to ease in the operating and handling of errors in applications.
func As ¶
func As(err error) *SimpleError
As attempts to find a SimpleError in the chain of errors, similar to errors.As(). Note that this will NOT match structs which embed a *SimpleError.
func New ¶
func New(_fmt string, args ...interface{}) *SimpleError
New creates a new SimpleError from a formatted string
func Wrap ¶
func Wrap(err error) *SimpleError
Wrap wraps the error in a SimpleError. It defaults the error code to CodeUnknown.
func Wrapf ¶ added in v0.1.1
func Wrapf(err error, msg string, a ...interface{}) *SimpleError
Wrapf returns a new SimpleError by wrapping an error with a formatted message string. It defaults the error code to CodeUnknown
func (*SimpleError) Attr ¶ added in v0.1.4
func (e *SimpleError) Attr(key, value interface{}) *SimpleError
Attr attaches an attribute to the error that can be detected when handling the error. Attr() behaves similarly to `context.WithValue()`. Keys should be custom types in order to avoid naming collisions. Use `GetAttribute()` to get the value of the attribute.
func (*SimpleError) Aux ¶ added in v0.1.1
func (e *SimpleError) Aux(kv ...interface{}) *SimpleError
Aux attaches auxiliary informational data to the error as key value pairs. All keys must be of type `string` and have a value. Keys without values are ignored. This auxiliary data can be retrieved by using `ExtractAuxiliary()` and attached to structured loggers. Do not use this to detect any attributes on the error, instead use Attr().`
func (*SimpleError) AuxMap ¶ added in v0.1.1
func (e *SimpleError) AuxMap(aux map[string]interface{}) *SimpleError
AuxMap attaches auxiliary informational data to the error from a map[string]interface{}. This auxiliary data can be retrieved by using `ExtractAuxiliary()` and attached to structured loggers. Do not use this to detect any attributes on the error, instead use Attr().`
func (*SimpleError) Benign ¶
func (e *SimpleError) Benign() *SimpleError
Benign marks the error as "benign". A benign error is an error that depends on the context of the caller. eg a NotFoundError is only an error if the caller is expecting the entity to exist. These errors can usually be logged less severely (ie at INFO rather than ERROR level)
func (*SimpleError) BenignReason ¶
func (e *SimpleError) BenignReason(reason string) *SimpleError
BenignReason marks the error as "benign" and attaches a reason it was marked benign. A benign error is an error depending on the context of the caller. eg a NotFoundError is only an error if the caller is expecting the entity to exist These errors can usually be logged less severely (ie at INFO rather than ERROR level)
func (*SimpleError) Code ¶
func (e *SimpleError) Code(code Code) *SimpleError
Code sets the error code. The assigned code should be defined in the registry.
func (*SimpleError) Error ¶
func (e *SimpleError) Error() string
Error satisfies the `error` interface. It uses the `simplerr.Formatter` to generate an error string.
func (*SimpleError) GetAttribute ¶ added in v0.1.9
func (e *SimpleError) GetAttribute(key interface{}) (interface{}, bool)
GetAttribute gets an attribute attached to this specific SimpleError. It does NOT traverse the error chain. This can be used to define attributes on the error that do not have first-class support with simplerr. Much like keys in the `context` package, the `key` should be a custom type so it does not have naming collisions with other values.
func (*SimpleError) GetAuxiliary ¶ added in v0.1.1
func (e *SimpleError) GetAuxiliary() map[string]interface{}
GetAuxiliary gets the auxiliary informational data attached to this error. This key-value data can be attached to structured loggers.
func (*SimpleError) GetBenignReason ¶
func (e *SimpleError) GetBenignReason() (string, bool)
GetBenignReason returns the benign reason and whether the error was marked as benign ie. This error can be logged at INFO level and then discarded.
func (*SimpleError) GetCode ¶
func (e *SimpleError) GetCode() Code
GetCode returns the error code as defined in the registry
func (*SimpleError) GetDescription ¶ added in v0.1.2
func (e *SimpleError) GetDescription() string
GetDescription returns the description of the error code on the error.
func (*SimpleError) GetMessage ¶ added in v0.1.1
func (e *SimpleError) GetMessage() string
GetMessage gets the error string for this error, exclusive of any wrapped errors.
func (*SimpleError) GetRetriable ¶ added in v1.3.0
func (e *SimpleError) GetRetriable() bool
GetRetriable returns a flag that signals that the operation which created this error is transient and that the user should retry the operation in hopes of it succeeding.
func (*SimpleError) GetSilent ¶
func (e *SimpleError) GetSilent() bool
GetSilent returns a flag that signals that this error should be recorded or logged silently on the server side ie. This error should not be logged at all
func (*SimpleError) Message ¶ added in v0.1.2
func (e *SimpleError) Message(msg string, args ...interface{}) *SimpleError
Message sets the message text on the error. This message it used to wrap the underlying error, if it exists.
func (*SimpleError) Retriable ¶ added in v1.3.0
func (e *SimpleError) Retriable() *SimpleError
Retriable sets the error as retriable.
func (*SimpleError) Silence ¶
func (e *SimpleError) Silence() *SimpleError
Silence sets the error as silent. Silent errors can be ignored by loggers.
func (*SimpleError) StackFrames ¶ added in v0.1.11
func (e *SimpleError) StackFrames() []uintptr
StackFrames returns a slice of pointers to program counters This method is primarily used to better integrate with sentry stack trace extraction
func (*SimpleError) StackTrace ¶ added in v0.1.1
func (e *SimpleError) StackTrace() []Call
StackTrace returns the stack trace at the point at which the error was raised.
func (*SimpleError) Unwrap ¶
func (e *SimpleError) Unwrap() error
Unwrap implement the interface required for error unwrapping. It returns the underlying (wrapped) error.