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 ¶
var Separator = " »» "
Separator defines the string used to separate nested errors.
Functions ¶
func As ¶
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 IsKind ¶
IsKind reports whether err is an *Error of the given Kind. If err is nil then Is returns false.
func ToHTTPResponse ¶
ToHTTPResponse creates a string to be used for HTTP response by chaining the underlying application errors HTTPMessage.
func ToHTTPStatus ¶
ToHTTPStatus converts an error to an HTTP status code.
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.
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) 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.