failure

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2022 License: MIT Imports: 2 Imported by: 27

README

Failure

The failure package builds upon the errors package, implementing a strategy called Opaque errors which I first learned about from an article Don't just check errors handle them gracefully by Dave Cheney who I believe is also the author of the errors package.

Failure is geared towards describing errors that occur while using microservices that generally support rest apis and as such you will notice a slight bias towards that, although I try to separate concerns as much as possible. It is heavily influenced by sls a library used to develop AWS Serverless applications using Golang and Terraform.

Usage

func SomeFunc() error {
  result, err := some-api.Execute()
  if err != nil {
    return failure.ToSystem(err, "some-api.Execute failed")
  }

  return nil	
}


func OtherFunc() error {
  if err := SomeFunc(); err != nil {
    if failure.IsSystem(err) {
      // do something different here			
    } 	
		
	// normal error handling here	
  } 	

  return nil
}

Documentation

Overview

Package failure implements an opaque error pattern based several of the most common types of errors that occur when developing microservices.

Index

Constants

View Source
const (
	SystemMsg           = "system failure"
	ServerMsg           = "server failure"
	NotFoundMsg         = "not found failure"
	NotAuthorizedMsg    = "not authorized failure"
	NotAuthenticatedMsg = "not authenticated failure"
	ForbiddenMsg        = "access is forbidden"
	ValidationMsg       = "validation failure"
	DeferMsg            = "failure occurred inside defer"
	IgnoreMsg           = "ignore failure"
	ConfigMsg           = "config failure"
	InvalidParamMsg     = "invalid param failure"
	ShutdownMsg         = "system shutdown failure"
	BadRequestMsg       = "bad request"
	TimeoutMsg          = "timeout failure"
	NoMoreRetriesMsg    = "retry limit reached"
)

Variables

This section is empty.

Functions

func BadRequest added in v0.6.0

func BadRequest(format string, a ...interface{}) error

BadRequest is used to signal that the app should shut down.

func Config added in v0.3.0

func Config(format string, a ...interface{}) error

Config is used to signify that error occurred when processing the application configuration

func Defer added in v0.2.0

func Defer(format string, a ...interface{}) error

Defer is used to signify errors that originate inside a defer function

func Forbidden added in v0.7.0

func Forbidden(format string, a ...interface{}) error

Forbidden is used to signify either not authenticated or not authorized

func Ignore

func Ignore(format string, a ...interface{}) error

Ignore is used to signify that error should not be acted on, it's up to the handler to decide to log these errors or not.

func IgnoreRetry added in v0.8.0

func IgnoreRetry(format string, a ...interface{}) error

IgnoreRetry is used to signal that this operation should not be retried. Useful in http client middleware that provides operations that are mixed into retry or backoff functions

func InvalidParam added in v0.4.0

func InvalidParam(format string, a ...interface{}) error

InvalidParam is to indicate that the param of a function or any parameter in general is invalid

func IsAnyAuthFailure added in v0.7.0

func IsAnyAuthFailure(e error) bool

IsAnyAuthFailure can be used to determine if any of the following we used: NotAuthenticated, NotAuthorized, Forbidden

func IsBadRequest added in v0.6.0

func IsBadRequest(e error) bool

func IsConfig added in v0.3.0

func IsConfig(e error) bool

func IsDefer added in v0.2.0

func IsDefer(e error) bool

func IsForbidden added in v0.7.0

func IsForbidden(e error) bool

func IsIgnore

func IsIgnore(e error) bool

func IsIgnoreRetry added in v0.8.0

func IsIgnoreRetry(e error) bool

func IsInvalidParam added in v0.4.0

func IsInvalidParam(e error) bool

func IsNoMoreRetries added in v0.8.0

func IsNoMoreRetries(e error) bool

func IsNotAuthenticated added in v0.7.0

func IsNotAuthenticated(e error) bool

func IsNotAuthorized added in v0.7.0

func IsNotAuthorized(e error) bool

func IsNotFound

func IsNotFound(e error) bool

func IsServer

func IsServer(err error) bool

IsServer will return true if the cause is a serverErr

func IsShutdown added in v0.5.0

func IsShutdown(e error) bool

func IsSystem

func IsSystem(err error) bool

func IsTimeout added in v0.8.0

func IsTimeout(e error) bool

func IsValidation

func IsValidation(e error) bool

func NoMoreRetries added in v0.8.0

func NoMoreRetries(format string, a ...interface{}) error

NoMoreRetries is used to signal all available retries have been used up

func NotAuthenticated added in v0.7.0

func NotAuthenticated(format string, a ...interface{}) error

NotAuthenticated is used to signify that a resource's identity verification failed. They are not who they claim to be

func NotAuthorized added in v0.7.0

func NotAuthorized(format string, a ...interface{}) error

NotAuthorized is used to signify that a resource does not have sufficient access to perform a given task

func NotFound

func NotFound(format string, a ...interface{}) error

NotFound is used to signify that whatever resource you were looking for does not exist and that fact it does not exist is an error.

func Server

func Server(format string, a ...interface{}) error

Server has the same meaning as Platform or System, it can be used instead if you don't like how Platform or System reads in your code.

func Shutdown added in v0.5.0

func Shutdown(format string, a ...interface{}) error

Shutdown is used to signal that the app should shut down.

func System

func System(format string, a ...interface{}) error

System is has the same meaning as Platform or Server, it can be used instead if you don't like how Platform reads in your code

func Timeout added in v0.8.0

func Timeout(format string, a ...interface{}) error

Timeout is used to signify that error because something was taking too long

func ToBadRequest added in v0.6.0

func ToBadRequest(e error, format string, a ...interface{}) error

func ToConfig added in v0.3.0

func ToConfig(e error, format string, a ...interface{}) error

func ToDefer added in v0.2.0

func ToDefer(e error, format string, a ...interface{}) error

func ToForbidden added in v0.7.0

func ToForbidden(e error, format string, a ...interface{}) error

func ToIgnore

func ToIgnore(e error, format string, a ...interface{}) error

ToIgnore converts `e` into the root cause of ignoreErr, it informs the system to ignore error. Used typically to log results and do not act on the error itself.

func ToIgnoreRetry added in v0.8.0

func ToIgnoreRetry(e error, format string, a ...interface{}) error

func ToInvalidParam added in v0.4.0

func ToInvalidParam(e error, format string, a ...interface{}) error

func ToNoMoreRetries added in v0.8.0

func ToNoMoreRetries(e error, format string, a ...interface{}) error

func ToNotAuthenticated added in v0.7.0

func ToNotAuthenticated(e error, format string, a ...interface{}) error

func ToNotAuthorized added in v0.7.0

func ToNotAuthorized(e error, format string, a ...interface{}) error

func ToNotFound

func ToNotFound(e error, format string, a ...interface{}) error

func ToServer

func ToServer(e error, format string, a ...interface{}) error

func ToShutdown added in v0.5.0

func ToShutdown(e error, format string, a ...interface{}) error

func ToSystem

func ToSystem(e error, format string, a ...interface{}) error

func ToTimeout added in v0.8.0

func ToTimeout(e error, format string, a ...interface{}) error

func ToValidation

func ToValidation(e error, format string, a ...interface{}) error

func UnwrapIgnoreRetry added in v0.8.0

func UnwrapIgnoreRetry(e error) error

func Validation

func Validation(format string, a ...interface{}) error

Validation is used to signify that a validation rule as been violated

func Wrap

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

Wrap expose errors.Wrapf as our default wrapping style

func WrapIgnoreRetry added in v0.8.0

func WrapIgnoreRetry(e error) error

Types

This section is empty.

Jump to

Keyboard shortcuts

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