Documentation ¶
Overview ¶
Package errors wraps Dave Cheney's errors package, by adding an error type and context to the error. Our custom errors implement the errors.Wrap interface, allowing a Cause function to work properly and allow the application to retrieve the cause of the error, and allows us to add context to errors by wrapping them.
err := fmt.Errorf("no such file") err = Wrap(err, "open failed") err = Wrap(err, "read config failed") fmt.Println(err.Error()) // "read config failed: open failed: no such file"
If we wanted to find the original error, then we can use the Cause function, which returns the first error that doesn't implement a Cause() function.
originalError := Cause(err) fmt.Println(originalError.Error()) // "no such file"
Additionally, we can add error types to the custom error, of a type ErrorType. By default, a NoType error is created, but we would also like to be able to create other types of errors, which can be done using
err = BadRequest.New("no such file")
and
err = BadRequest.Wrap(err, "open failed")
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ErrorType ¶
type ErrorType uint
ErrorType is the type of an error.
const ( NoType ErrorType = iota // NoType error BadRequest // BadRequest error, data formatted badly Forbidden // Forbidden error, authentcated user without proper rights NotFound // NotFound error, resource is not found )
Each ErrorType can be mapped to the corresponding HTTP.Status error. For example, BadRequest ErrorType maps to http.StatusBadRequest.
func (ErrorType) New ¶
New Creates a new custom error, with a defined error type. Function signature allows