Documentation ¶
Index ¶
- Constants
- func ConcatErrors(errs ...error) error
- func ConcatErrorsMessage(errs ...error) string
- func DontPanic(f func()) (err error)
- func E(args ...interface{}) error
- func EqualsCode(lCode, rCode Code) bool
- func Errorf(s string, params ...interface{}) error
- func GetRootError(err error) error
- func GetRootErrorWithKV(err error) error
- func New(s string) error
- func SameCode(lError, rError error) bool
- type Code
- type Error
- type KeyValue
- type Op
- type Severity
Examples ¶
Constants ¶
const ( // SeverityUnset indicates the severity was not set SeverityUnset = Severity("") // SeverityRuntime indicates the error is returned for an operation that should/could be executed again. For example, timeouts. SeverityRuntime = Severity("runtime") // SeverityFatal indicates the error is unrecoverable and the execution should stop, or not being retried. SeverityFatal = Severity("fatal") // SeverityInput indicates an expected, like a bad user input/request. For example, an invalid email. SeverityInput = Severity("input") )
const ( // CodeEmpty is the zero-value for error codes CodeEmpty = Code("") )
Variables ¶
This section is empty.
Functions ¶
func ConcatErrors ¶
ConcatErrors returns an error with a message that is the concatenation of all messages of the given errors
func ConcatErrorsMessage ¶
ConcatErrorsMessage concatenates all the error messages from the given errors
func DontPanic ¶
func DontPanic(f func()) (err error)
DontPanic executes f and, if f panics, recovers from the panic and returns the panic wrapped as an Error.
func E ¶
func E(args ...interface{}) error
E is a helper function for building errors.
If called with no arguments, it returns an error solely containing a message informing that.
This method can be called with any set of parameters of any type, but it requires either an error or a string at least, otherwise it will return nil.
Parameters can be passed in any order and even be of repeated types, although only the last value of each type will be considered, except for KeyValue and []KeyValue, which will be concatenated to the struct's KVs value.
Types other than string, Code, Severity, error, Op, KeyValue, or []KeyValue will simply be ignored.
Example ¶
// Calling E with no arguments results in an error with a message saying // that the function was called with no arguments errNoArgs := E() fmt.Println(errNoArgs) // Calling E without a string or an error will result in a nil return errNil := E(Op("Error Example"), Code("ERROR_EXAMPLE_NIL")) fmt.Println(errNil) // E requires either a string or an err to return an error withString := E("This string will be used to build an error") previous := errors.New("Previous error") withError := E(previous) fmt.Println(withString, withError) // We can pass parameters of the same type more than once, but only the last // one will be considered multiOp := E("Multi op", Op("Op 1"), Op("Op 2"), Op("Op 3")) fmt.Println(multiOp) // Except for KeyValue and []KeyValue which will be concatenated kv := KeyValue{Key: "key1", Value: "val1"} multiKv := E("Multi kv", kv, kv, kv) fmt.Println(multiKv) // Values of unexpected types will be ignored intErr := E("Int err", 1, 2, 3) fmt.Println(intErr) // Full example op := Op("errors.errorExample") code := Code("ERROR_EXAMPLE") sev := SeverityRuntime kv = KeyValue{Key: "key", Value: "val"} err := E("Error example", op, code, sev, kv) fmt.Println(err)
Output:
func EqualsCode ¶
EqualsCode returns true if @lCode and @rCode holds the same value, and false otherwise
func Errorf ¶
Errorf returns a error based on given params. It is a wrap of the fmt.Errorf method that returns nil when given an empty string
func GetRootError ¶
GetRootError returns the deepest error in the Err stack. That is, while an Error has a previous Error, keep getting the previous and returns when previous no longer has an Err
func GetRootErrorWithKV ¶
GetRootErrorWithKV returns the Err field of Error struct or the error itself if it is of another type
Types ¶
type Code ¶
type Code string
Code is the error code
var CodePanic Code = "PANIC"
CodePanic represents the error code for panic
func GetCode ¶
GetCode returns the error code. If the error doesn't contains an error code, returns ErrorCodeEmpty
func (Code) MarshalZerologObject ¶
MarshalZerologObject allows for zerolog to log the error code as 'error_code': '...'
type Error ¶
type Error struct { // Severity contains information about the nature of the error so you can // decide what to do Severity Severity // Err is the previous error, usually given by a lower level layer to be // wrapped Err error // Code is an application friendly way to describe the error Code Code // Op is the operation that was happening when the error occurred. In other // words, is where the error happened Op Op // KVs is a map os values you can use to enrich your error with relevant // information KVs []KeyValue }
Error is the error struct that should be returned in all functions. It is ready to hold useful data about the error and has methods that make building and extracting information very easy. It also implements the Go's error interface
func NewFromRecover ¶
func NewFromRecover(r interface{}) Error
NewFromRecover returns a new Error created from the result of a recover. If r is an Error, this will be used so Op and KV are preserved
func (Error) Error ¶
Error formats the error information into a string. By implementing this method, we implement Go's error interface
type KeyValue ¶
type KeyValue struct { Key interface{} Value interface{} }
KeyValue is used to store a key-value pair within the error
type Severity ¶
type Severity string
Severity is the error severity. It's used to classify errors in groups to be easily handled by the code. For example, a retry layer should be only checking for Runtime errors to retry. Or in an HTTP layer, errors of input type are always returned a 400 status.
func GetSeverity ¶
GetSeverity returns the error severity. If there is not severity, SeverityUnset is returned.
func (Severity) MarshalZerologObject ¶
MarshalZerologObject allows for zerolog to log the error severity as 'error_severity': '...'