errors

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2022 License: MIT Imports: 4 Imported by: 17

README

Package Errors

Goals:
  • Simple
  • Predictable
  • All methods will return non nil objects that satifies the standard error interface
  • Errors have types that can be comparable
  • No reflect
Usage
err := readToken(user)
if err != nil {
    return errors.Unauthorized(err)
}

errors.Unauthorized will return an error object configured as follows:

  • type: errors.ErrUnauthorized
  • code: http.StatusUnauthorized

output

{
  "code": 401,
  "error": "Cannot read token",
  "type": "Unauthorized"
}
Error types
var ErrDatabase = errors.New("Database")
var ErrInternal = errors.New("Internal")
var ErrForbiden = errors.New("Forbiden")
var ErrUnauthorized = errors.New("Unauthorized")
var ErrNotFound = errors.New("Record not found")
var ErrDuplicated = errors.New("Duplicated record")
var ErrCreate = errors.New("Cannot create record")
var ErrUpdate = errors.New("Cannot update record")
var ErrDelete = errors.New("Cannot delete record")
var ErrValidation = errors.New("Validation")
var ErrAssertion = errors.New("Assertion")
Comparing Errors
errors.IsKind(err, errors.ErrUnauthorized)
Internals

This package works with the concept of error stacks.

type ErrorStack struct {
	Errors []Error
}

An ErrorStack contains an array of errors. You can chain multiple errors, one after another in order to get a stack trace from the original error until the final error.

// Error represents an application error. It does contain:
// - a textual representation of the current error Also
// - an error type that can be compared with standard errors
// - an http status code
type Error struct {
	ID      int
	Message string
	Kind    error
	HTTPStatusCode int
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAssertion = errors.New("Assertion")
View Source
var ErrCreate = errors.New("Cannot create record")
View Source
var ErrDatabase = errors.New("Database")
View Source
var ErrDelete = errors.New("Cannot delete record")
View Source
var ErrDuplicated = errors.New("Duplicated record")
View Source
var ErrForbidden = errors.New("Forbidden")
View Source
var ErrInternal = errors.New("Internal")
View Source
var ErrNotFound = errors.New("Record not found")
View Source
var ErrUnauthorized = errors.New("Unauthorized")
View Source
var ErrUpdate = errors.New("Cannot update record")
View Source
var ErrValidation = errors.New("Validation")

Functions

func IsKind

func IsKind(err error, kind error) bool

IsKind checks if an error is an error stack, and if it is, checks if the last error of the stack is of a given kind. Notice that the kind is also an error. The kind comparision is done not by the string representation of the two error kinds but direct instance comparision.

func IsRootOfKind

func IsRootOfKind(err error, kind error) bool

IsRootOfKind checks if an error is an error stack, and if it is, checks if the root error of the stack is of a given kind. Notice that the kind is also an error. The kind comparision is done not by the string representation of the two error kinds but direct instance comparision.

Types

type Error

type Error struct {
	ID             int
	Message        string
	Kind           error
	HTTPStatusCode int
}

Error represents an application error. It does contain: - an array of previous errors, - a textual representation of the current error, - an error type that can be compared with standard errors, - an http status code.

func (Error) Error

func (e Error) Error() string

Error returns a textual representation of the error. Satisfies the standard error interface.

func (*Error) MarshalJSON

func (e *Error) MarshalJSON() ([]byte, error)

MarshalJSON returns an array of bytes which contains a marshaled json version of the error. Satisfies the Marshaler interface.

func (*Error) StatusCode

func (e *Error) StatusCode() int

StatusCode returns an HTTP status code Satisfies GoKit StatusCoder interface.

type ErrorStack

type ErrorStack struct {
	Errors []Error
}

func Assertion

func Assertion(e interface{}) *ErrorStack

func Create

func Create(e interface{}, extras ...string) *ErrorStack

func Database

func Database(e interface{}) *ErrorStack

func Default

func Default(e interface{}) *ErrorStack

Default receives an object (error or string) and creates an ErrorStack. If object is already an ErrorStack then it appends the current error to the existing stack.

func Delete

func Delete(e interface{}) *ErrorStack

func Duplicated

func Duplicated(e interface{}) *ErrorStack

func Forbidden

func Forbidden(e interface{}) *ErrorStack

func MethodNotAllowed

func MethodNotAllowed(e interface{}) *ErrorStack

func NotFound

func NotFound(e interface{}) *ErrorStack

func Unauthorized

func Unauthorized(e interface{}) *ErrorStack

func Update

func Update(e interface{}) *ErrorStack

func Validation

func Validation(e interface{}, extras ...string) *ErrorStack

func (ErrorStack) Error

func (e ErrorStack) Error() string

Error returns a textual representation of the error. Satisfies the standard error interface.

func (*ErrorStack) MarshalJSON

func (e *ErrorStack) MarshalJSON() ([]byte, error)

MarshalJSON returns an array of bytes which contains a marshaled json version of the error. Satisfies the Marshaler interface.

func (*ErrorStack) StatusCode

func (e *ErrorStack) StatusCode() int

StatusCode returns an HTTP status code Satisfies GoKit StatusCoder interface.

func (*ErrorStack) UnmarshalJSON

func (e *ErrorStack) UnmarshalJSON(data []byte) error

UnmarshalJSON allows to unmarshal an ErrorStack from his json representation.

Jump to

Keyboard shortcuts

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