errors

package module
v0.4.0 Latest Latest
Warning

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

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

README

Errors Package

Lib designed to facilitate and standardize how error handling should be done inside Trivela's systems.

It produces CustomError, that encodes useful information about a given error.

It's supposed to flow within the application in detriment of the the default golang error since its Kind and Code attributes are the keys to express its semantic and uniqueness, respectively.

It should be generated once by the peace of code that found the error (because it's where we have more context about the error), and be by passed to the upper layers of the application.

Documentation

Index

Constants

View Source
const (
	// CodeUnknown is the default code returned when the application doesn't attach any code into the error.
	CodeUnknown CodeType = "UNKNOWN"
	// KindUnexpected is the default kind returned when the application doesn't attach any kind into the error.
	KindUnexpected KindType = "UNEXPECTED"
	// KindConflict are errors caused by requests with data that conflicts with the current state of the system.
	KindConflict KindType = "CONFLICT"
	// KindInternal are errors caused by some internal fail like failed IO calls or invalid memory states.
	KindInternal KindType = "INTERNAL"
	// KindInvalidInput are errors caused by some invalid values on the input.
	KindInvalidInput KindType = "INVALID_INPUT"
	// KindNotFound are errors caused by any required resources that not exists on the data repository.
	KindNotFound KindType = "NOT_FOUND"
	// KindUnauthenticated are errors caused by an unauthenticated call.
	KindUnauthenticated KindType = "UNAUTHENTICATED"
	// KindUnauthorized are errors caused by an unauthorized call.
	KindUnauthorized KindType = "UNAUTHORIZED"
	// KindResourceExhausted indicates some resource has been exhausted, perhaps a per-user quota, or perhaps the entire file system is out of space.
	KindResourceExhausted KindType = "RESOURCE_EXHAUSTED"
)

Variables

View Source
var (
	// ErrResourceNotFound indicates that a desired resource was not found.
	ErrResourceNotFound error = New("resource not found").WithKind(KindNotFound).WithCode("RESOURCE_NOT_FOUND")

	// ErrNotImplemented indicates that a given feature is not implemented yet.
	ErrNotImplemented error = New("feature not implemented yet").WithCode("FEATURE_NOT_IMPLEMENTED")

	// ErrMock is a fake mocked that should be used in test scenarios.
	ErrMock error = New("mocked error").WithCode("MOCKED_ERROR")
)

Functions

func NewMissingRequiredDependency

func NewMissingRequiredDependency(name string) error

NewMissingRequiredDependency creates a new error that indicates a missing required dependency. It should be producing at struct constructors.

func NewValidationError added in v0.4.0

func NewValidationError(desc string) error

NewValidationError creates a Validation error.

func RootError

func RootError(err error) string

RootError tries to convert the given error into a CustomError. If so, it recursively tries to find the root error (non CustomError) in a CustomError RootError chain and returns its message.

Types

type CodeType

type CodeType string

CodeType is a string that contains error's code description.

func Code

func Code(err error) CodeType

Kind this method receives an error, then compares its interface type with the CustomError interface. If the interfaces types matches, returns its Code.

type CustomError

type CustomError struct {
	// contains filtered or unexported fields
}

CustomError is a structure that encodes useful information about a given error. It's supposed to flow within the application in detriment of the the default golang error, since its Kind and Code attributes are the keys to express its semantic and uniqueness, respectively. It should be generated once by the peace of code that found the error (because it's where we have more context about the error), and be by passed to the upper layers of the application.

func New

func New(message string, args ...interface{}) CustomError

New returns a new instance of CustomError with the given message.

func (CustomError) Error

func (ce CustomError) Error() string

Error returns CustomError message.

func (CustomError) WithCode

func (ce CustomError) WithCode(code CodeType) CustomError

WithCode return a copy of the CustomError with the given CodeType filled.

func (CustomError) WithKind

func (ce CustomError) WithKind(kind KindType) CustomError

WithKind return a copy of the CustomError with the given KindType filled.

func (CustomError) WithRootError

func (ce CustomError) WithRootError(err error) CustomError

WithRootError returns a copy of the CustomError with the RootError filled.

type KindType

type KindType string

KindType is a string that contains error's kind description.

func Kind

func Kind(err error) KindType

Kind this method receives an error, then compares its interface type with the CustomError interface. If the interfaces types matches, returns its kind.

Jump to

Keyboard shortcuts

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