errors

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

README

Errors

Package errors provides simple error handling.

It allows to set the kind of error this is, a HTTP message and record the strack trace of error.

Feel free to add new functions or improve the existing code.

Install

go get github.com/iconimpact/go-core/errors

Usage and Examples

errors.E builds an error value from its arguments.

There must be at least one argument or E panics.
The type of each argument determines its meaning.
If more than one argument of a given type is presented,
only the last one is recorded.

The types are:
    string
        The HTTP message for the API user.
    errors.Kind
        The class of error, such as permission failure.
    error
        The underlying error that triggered this one.

If the error is printed, only those items that have been
set to non-zero values will appear in the result.

If Kind is not specified or Other, we set it to the Kind of
the underlying error.
errors.E(errors.Unprocessable, "HTTP response message")
err := db.Get(&obj, query)
if err != nil {
    if errors.Is(err, sql.ErrNoRows) {
        return errors.E(err, errors.NotFound)
    }

    return errors.E(err, errors.Internal)
}

errors.ToHTTPResponse creates a string to be used for HTTP response by chaining the underlying application errors HTTPMessage.

err := errors.E(errors.Unprocessable, "HTTP response message 1")
err = errors.E(err, "HTTP response message 2")

errors.ToHTTPResponse(err)
response: "HTTP response message 1: HTTP response message 2"

Kinds of errors

When the error is used in a router the Kind is usually mapped to a HTTP response code.

Other                     // Unclassified error
BadRequest                // Bad Request (400)
Unauthorized              // Unauthorized (401)
Forbidden                 // Forbidden (403)
NotFound                  // Not found (404)
Conflict                  // Conflict (409)
Unprocessable             // Unprocessable, invalid request data (422)
Internal                  // Internal server error (500)
BadGateway                // Bad gateway (502)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Separator = " »» "

Separator defines the string used to separate nested errors.

Functions

func As

func As(err error, target interface{}) bool

As calls stdlib errors.As to find the first error in err's chain that matches target, and if so, sets target to that error value and returns true.

func E

func E(args ...interface{}) error

E builds an error value from its arguments. There must be at least one argument or E panics. The type of each argument determines its meaning. If more than one argument of a given type is presented, only the last one is recorded.

The types are:

string
	The HTTP message for the API user.
errors.Kind
	The class of error, such as permission failure.
error
	The underlying error that triggered this one.

If the error is printed, only those items that have been set to non-zero values will appear in the result.

If Kind is not specified or Other, we set it to the Kind of the underlying error.

func Is

func Is(err, target error) bool

Is calls stdlib errors.Is to report whether any error in err's chain matches target.

func IsKind

func IsKind(kind Kind, err error) bool

IsKind reports whether err is an *Error of the given Kind. If err is nil then Is returns false.

func ToHTTPResponse

func ToHTTPResponse(e *Error) string

ToHTTPResponse creates a string to be used for HTTP response by chaining the underlying application errors HTTPMessage.

func ToHTTPStatus

func ToHTTPStatus(e *Error) int

ToHTTPStatus converts an error to an HTTP status code.

func Unwrap

func Unwrap(err error) error

Unwrap calls stdlib errors.Unwrap to return the result of calling the Unwrap method on err, if err's type contains an Unwrap method returning error. Otherwise, Unwrap returns nil.

Types

type Error

type Error struct {
	// application specific fields.
	HTTPMessage string

	// logical operation and nested error.
	Kind Kind
	Err  error
	// contains filtered or unexported fields
}

Error defines a standard application error.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Is

func (e *Error) Is(target error) bool

Is provides compatibility for Go 1.13 error chains.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap provides compatibility for Go 1.13 error chains.

type Kind

type Kind int

Kind defines the error type this is, mostly for use by routers that must set different response status depending on the error.

const (
	Other         Kind = iota // Unclassified error
	BadRequest                // Bad Request (400)
	Unauthorized              // Unauthorized (401)
	Forbidden                 // Forbidden (403)
	NotFound                  // Not found (404)
	Conflict                  // Conflict (409)
	Gone                      // Gone (410)
	Unprocessable             // Unprocessable, invalid request data (422)
	Internal                  // Internal server error (500)
	BadGateway                // Bad gateway (502)
)

Kinds types.

func (Kind) String

func (k Kind) String() string

String transforms Kind type to text.

Jump to

Keyboard shortcuts

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