failure

package module
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2022 License: MIT Imports: 3 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"
	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"
	InputMsg        = "invalid input"

	DefaultInputSeparator     = ":"
	DefaultInputItemSeparator = ","
)

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 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 Input

func Input(fields map[string]string, format string, a ...interface{}) error

func InputFields added in v0.6.1

func InputFields(e error) (map[string]string, bool)

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 IsBadRequest added in v0.6.0

func IsBadRequest(e error) bool

func IsConfig added in v0.3.0

func IsConfig(err error) bool

func IsDefer added in v0.2.0

func IsDefer(err error) bool

func IsIgnore

func IsIgnore(err error) bool

func IsInput

func IsInput(e error) bool

func IsInvalidParam added in v0.4.0

func IsInvalidParam(err error) bool

func IsNotFound

func IsNotFound(err 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 IsValidation

func IsValidation(err error) bool

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 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 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 ToInvalidParam added in v0.4.0

func ToInvalidParam(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 ToValidation

func ToValidation(e error, format string, a ...interface{}) 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

Types

type InputOpts added in v0.6.1

type InputOpts struct {
	Sep     string
	Fields  map[string]string
	MsgFunc func(msg, sep string, fields map[string]string) string
}

Jump to

Keyboard shortcuts

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