simplerr

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 11, 2022 License: MIT Imports: 3 Imported by: 0

README

Simplerr

Simplerr aims to provide a cleaner, more maintainable and rich Go error handling experience.

Objectives:

  • Less boilerplate for error detection, propagation and translation
  • Standardized error codes
  • Provide a middleware-friendly error implementation
  • Ability to distinguish between benign and non-benign errors
  • Ability to ignore logging of benign errors
  • More detailed error logging via contextual attributes

Documentation

Index

Constants

View Source
const NumberOfReservedCodes = 100

NumberOfReservedCodes is the code number, under which, are reserved for use by this library.

Variables

This section is empty.

Functions

func HasErrorCode

func HasErrorCode(err error, code Code) bool

HasErrorCode checks the error code of an error if it is a SimpleError{}

func IsBenign

func IsBenign(err error) (reason string, benign bool)

IsBenign checks the error or any error in the chain, is benign

func IsNotFoundError

func IsNotFoundError(err error) bool

func IsSilent

func IsSilent(err error) bool

IsSilent checks the error or any error in the chain, is silent

func SetRegistry

func SetRegistry(r *Registry)

SetRegistry sets the default error registry

Types

type Code

type Code int
const (
	CodeUnknown Code = iota
	CodeAlreadyExists
	CodeNotFound
	CodeNothingUpdated
	CodeNothingDeleted
	CodeInvalidParameter
	CodeInvalidAuth
	CodeConstraintViolated
	CodeNotSupported
	CodeNotImplemented
	CodeMissingParameter
	CodeTimedOut
	CodeCanceled
)

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 NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new registry without any defaults

func (*Registry) ErrorCodes

func (r *Registry) ErrorCodes() map[Code]string

ErrorCodes returns a copy of the registered error codes and their descriptions

func (*Registry) RegisterErrorCodes

func (r *Registry) RegisterErrorCodes(newCodes map[Code]string)

RegisterErrorCodes 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

func ContextDeadlineExceeded

func ContextDeadlineExceeded(err error) *SimpleError

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(err error) *SimpleError

func NewNotFoundErrorW

func NewNotFoundErrorW(err error, fmt string, args ...interface{}) *SimpleError

func Newf

func Newf(_fmt string, args ...interface{}) *SimpleError

Newf creates a new SimpleError from a formatted string

func Wrap

func Wrap(err error, msg string, a ...interface{}) *SimpleError

Wrap returns a new SimpleError by wrapping an error with a formatted message string

func (*SimpleError) Benign

func (e *SimpleError) Benign() *SimpleError

Benign marks the error as "benign"

func (*SimpleError) BenignReason

func (e *SimpleError) BenignReason(reason string) *SimpleError

BenignReason marks the error as "benign" and attaches a reason it was marked benign.

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) GetBenignReason

func (e *SimpleError) GetBenignReason() (string, bool)

GetBenignReason returns the benign reason and whether the error was marked as benign

func (*SimpleError) GetCode

func (e *SimpleError) GetCode() Code

GetCode returns the error code as defined in the registry

func (*SimpleError) GetSilent

func (e *SimpleError) GetSilent() bool

GetSilent returns if the error is silent

func (*SimpleError) Silence

func (e *SimpleError) Silence() *SimpleError

Silence sets the error as silent

func (*SimpleError) Unwrap

func (e *SimpleError) Unwrap() error

Unwrap implement the interface required for error unwrapping. It returns the underlying (wrapped) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL