Documentation ¶
Index ¶
- Constants
- Variables
- func DefaultFormatter(e *SimpleError) string
- func ExtractAuxiliary(err error) map[string]interface{}
- func GetAttribute(err error, key interface{}) interface{}
- func HasErrorCode(err error, code Code) bool
- func IsBenign(err error) (reason string, benign bool)
- func IsSilent(err error) bool
- func SetRegistry(r *Registry)
- type Call
- type Code
- type ErrorConversion
- type Registry
- type SimpleError
- func As(err error) *SimpleError
- func ContextCanceled(err error) *SimpleError
- func ContextDeadlineExceeded(err error) *SimpleError
- func Convert(err error) *SimpleError
- func New(_fmt string, args ...interface{}) *SimpleError
- func Wrap(err error) *SimpleError
- func Wrapf(err error, msg string, a ...interface{}) *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) 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) GetSilent() bool
- func (e *SimpleError) Message(msg string, args ...interface{}) *SimpleError
- func (e *SimpleError) Silence() *SimpleError
- 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
func GetAttribute(err error, key interface{}) interface{}
GetAttribute gets the first instance of the key in the error chain
func HasErrorCode ¶
HasErrorCode checks the error code of an error if it is a SimpleError{}
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 ErrorConversion ¶
type ErrorConversion func(err error) *SimpleError
ErrorConversion is a function that converts an `error` into a simple SimpleError{}
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 may panic if a reserved error code is used. This method should be called early on application startup.
func (*Registry) RegisterErrorConversions ¶
func (r *Registry) RegisterErrorConversions(funcs ...ErrorConversion)
RegisterErrorConversions registers an error conversion function
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()
func ContextCanceled ¶
func ContextCanceled(err error) *SimpleError
ContextCanceled wraps a context.Canceled as a SimpleError with the code CodeCanceled
func ContextDeadlineExceeded ¶
func ContextDeadlineExceeded(err error) *SimpleError
ContextDeadlineExceeded wraps a context.DeadlineExceeded as a SimpleError with the code CodeDeadlineExceeded
func Convert ¶
func Convert(err error) *SimpleError
Convert converts a regular error to a SimpleError. If it is already a SimpleError then it simply returns it as a SimpleError
func New ¶
func New(_fmt string, args ...interface{}) *SimpleError
New creates a new SimpleError from a formatted string
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
func (*SimpleError) Attr ¶ added in v0.1.4
func (e *SimpleError) Attr(key, value interface{}) *SimpleError
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.
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{}.
func (*SimpleError) Benign ¶
func (e *SimpleError) Benign() *SimpleError
Benign marks the error as "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) 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
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
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
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) 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
func (*SimpleError) Silence ¶
func (e *SimpleError) Silence() *SimpleError
Silence sets the error as silent
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