package
0.2.4
Repository: https://github.com/iconmobile-dev/go-core.git
Documentation: pkg.go.dev

# 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/iconmobile-dev/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)

# Functions

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.
E builds an error value from its arguments.
Is calls stdlib errors.Is to report whether any error in err's chain matches target.
IsKind reports whether err is an *Error of the given Kind.
ToHTTPResponse creates a string to be used for HTTP response by chaining the underlying application errors HTTPMessage.
ToHTTPStatus converts an error to an HTTP status code.
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.

# Constants

Bad gateway (502).
Bad Request (400).
Conflict (409).
Forbidden (403).
Gone (410).
Internal server error (500).
Not found (404).
Unclassified error.
Unauthorized (401).
Unprocessable, invalid request data (422).

# Variables

Separator defines the string used to separate nested errors.

# Structs

Error defines a standard application error.

# Type aliases

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