era

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2018 License: MIT Imports: 5 Imported by: 0

README

Era: Go Errors

codecov CircleCI

A reusable package aimed at simplifying errors and tracing their source. Heavily inspired by Failure is your Domain.

Why?

Era was made to solve two primary issues faced in writing go code. Firstly, Era aims to make error checking easier through the use of error codes. When using Era, standard codes or custom codes can be easily applied to errors, checking these codes can then be done using era.Code(err) which will return the lowest error code

The second and more pressing concern behind the creation of Era is the separation of internal and external error messages. Serving a single error message to both developers trying to debug their code, and customers wondering why their request failed is infeasible in many situations and makes for poorer less descriptive error messages for one party. Era aims to fix this issue by separating errors, the era.Error.Message is made to be external/customer friendly, this message should provide a safe to expose explanation of what went wrong. The root of an era.Error stack (stack in the essence that errors are nested) should point to a regular error, this error explains to the developer what wen't wrong and can delve into technical detail that external users would not want to see. Using this format, errors that are shipped to the customer can be done so using the era.Safe(e error) function. This function returns a regular error that only includes details of the operation, the error hash and the message, these are all details that should be customer friendly.

Another useful extra provided by the Era package is the era.Hash(err error) function, this computes a 5 character hexadecimal hash of the passed error. If the error is a regular error the hash will be 00000, otherwise the hash is computed on all the code, message and operations of the error stack. This hash is part of the return string of the era.Error.Error() method and the era.Safe(err error) function. The hash is useful in correlating errors from customers, as a customer can send the error hash they recieved as part of a support request. The hash will always be the same for the same error stack.

The struct

The Error struct is designed as follows.

// Error defines a standard application error.
type Error struct {
	// Machine-readable error code.
	// Error codes can be custom, but a set of predefined codes are defined in codes.go.
	Code string

	// Human-readable message, potentially consumer facing error.
	// This message can be delivered to a consumer as an indication of what happened and what they should do next.
	Message string

	// Logical operation
	Op string
	// Nested Error
	Err error
}

Documentation

Index

Constants

View Source
const (
	// EConflict Action cannot be performed
	EConflict = "conflict"
	// EInternal internal error
	EInternal = "internal"
	// EInvalid validation failed
	EInvalid = "invalid"
	// ENotFound entity not found/doesn't exist
	ENotFound = "not_found"
	// EAlreadyExists entity already exists
	EAlreadyExists = "already_exists"
	// EPermissionDenied Authenticated user does not have permission
	EPermissionDenied = "permission_denied"
	// EUnauthenticated Requestor does not have valid authentication to perform to operation
	EUnauthenticated = "unauthenticated"
	// ECannotDecode Data could not be decoded
	ECannotDecode = "cannot_decode"
	// ECannotEncode Data could not be encoded
	ECannotEncode = "cannot_encode"
	// ECannotParse Data could not be parsed
	ECannotParse = "cannot_parse"
)

Variables

This section is empty.

Functions

func Code

func Code(err error) string

Code returns the error code of the uppermost error object if type is of era.Error; If the error is a regular error or no code is specified, Code returns era.EInternal; Providing the code of the highest error object encourages codes to be applied at each level of the stack.

func Hash

func Hash(err error) string

Hash returns a five character hexadecimal hash of the error stack. This is useful for customer service and log analytics usecases as errors from the same cause can be quickly correlated. The hash function traverses down the error stack if the error is an era.Error, calculating the hash based on the Code, Message and Op. If the passed error is a regular error, Hash returns five zero characters.

func HighestCode

func HighestCode(err error) string

HighestCode returns the highest defined error code of the error object if type is of era.Error; If the error is a regular error or no code is specified in the entire error stack, Code returns era.EInternal; Unlike Code(), HighestCode() will recurse down in order to find the first defined code.

func LowestCode

func LowestCode(err error) string

LowestCode returns the lowest error code of the error, if available. Otherwise returns EInternal.

func Message

func Message(err error) string

Message returns the human-readable message of the error, if available. The message will be the lowest message in the stack. Otherwise returns a generic error message.

func Safe

func Safe(err error) error

Safe should be used when errors leave the scope of the service, and enter into a consumers domain. Safe provides the consumer with the error code and message, neither of these components should expose sensitive information. Safe returns an error for compatability reasons, an http handler may expect an error to be returned in the case of failure.

Types

type Error

type Error struct {
	// Machine-readable error code.
	// Error codes can be custom, but a set of predefined codes are defined in codes.go.
	Code string

	// Human-readable message, potentially consumer facing error.
	// This message can be delivered to a consumer as an indication of what happened and what they should do next.
	Message string

	// Logical operation
	Op string
	// Nested Error
	Err error
}

Error defines a standard application error.

func New

func New(err string) *Error

New returns a new Error, with the just the Err set to the m as an error.

func WithCode

func WithCode(err error, code string) *Error

WithCode adds an error code to the provided error. If the err is an era.Error && err.Code is undefined, the code is applied to the era.Error; If the err is an era.Error && err.Code is defined, the err is wrapped and the code is applied; If the err is a regular error, the error is wrapped and the code is applied.

func (*Error) Error

func (e *Error) Error() string

Error returns the string representation of the error message. Error returns a string prefixed with the top

func (*Error) WithCode

func (e *Error) WithCode(code string) *Error

WithCode adds an error code to the provided custom Error. The withcode method is useful as opposed to the era.WithCode() function, as it allows for errors to be composed in a chain like format. If WithCode is applied to a custom Error with a defined code, it is wrapped.

func (*Error) WithErr

func (e *Error) WithErr(err error) *Error

WithError applies the provided err to the *Error, Err should be a standard error but does not need to be, this method is useful when constructing a *Error at the beginning of a function call and then setting the Err field later.

Directories

Path Synopsis
examples
simple command
pkg

Jump to

Keyboard shortcuts

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