Documentation ¶
Overview ¶
Package errstack provides a way to augment errors with a functional type and iteratively build new request error.
In software we have 3 types of errors:
* Infrastructure - this errors come from the requests to other services.
* Domain - this is the type of errors when we detect that our internal application state is wrong.
* Request - a user request error.
Request errors maps parameters (or input data fields) to the explanation (why the value of wrong).
Domain errors usually arise form some model bug or unhandled condition.
Infrastructure errors arise from the system or connection error.
It is very handy to know the place (stack) where the error Infrastructure or Domain error arose - that's why they are threatened especially and are augmented with the stack trace.
Index ¶
- func CallAndAssign(err *error, f func() error)
- func CallAndLog(l Logger, f func() error)
- func Cause(err error) error
- func IsTimeout(e error) bool
- func Join(es ...error) error
- func Log(l Logger, err error)
- func Seq(ss ...string) error
- type Builder
- type Domain
- type E
- func NewDomain(s string) E
- func NewDomainF(format string, a ...interface{}) E
- func NewInf(s string) E
- func NewInfF(f string, a ...interface{}) E
- func NewReq(s string) E
- func NewReqDetails(key string, details interface{}) E
- func NewReqF(f string, a ...interface{}) E
- func WrapAsDomain(err error, message string) E
- func WrapAsDomainF(err error, f string, a ...interface{}) E
- func WrapAsInf(e error, messages ...string) E
- func WrapAsInfF(err error, f string, a ...interface{}) E
- func WrapAsReq(err error, message string) E
- func WrapAsReqF(err error, f string, a ...interface{}) E
- type HasStacktrace
- type HasStatusCode
- type HasUnderlying
- type Logger
- type Putter
- type StubPutter
- type UntilFirst
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CallAndAssign ¶
CallAndAssign assigns error returned from the function to the err pointer if needed. Usually useful in defer statements, eg:
CallAndAssign(&err, someFile.Close)
func CallAndLog ¶
CallAndLog calls function which may return error and logs it. The intention of this function is to be used with `go` and `defer` clauses.
func Cause ¶
Cause returns the underlying cause of the error, if possible. An error value has a cause if it implements the following interface:
type HasUnderlying interface { Cause() error }
If the error does not implement Cause, the original error will be returned. If the error is nil, nil will be returned without further investigation.
Types ¶
type Builder ¶
type Builder interface { // Fork creates a new builder which shares the same space but all new added errors // will be assigned to keys prefixed with `prefix` Fork(prefix string) Builder // ForkIdx is a handy function to call Fork with an `int` (eg: index keys / rows) ForkIdx(idx int) Builder // Putter returns a Putter which abstract error setting from error key. Putter(key string) Putter // Puts new error under the key. You can put multiple errors under the same key // and they will be agregated Put(key string, value interface{}) // Get returns errors under `key`. Get is aware about 'prefix' and it will add it // to the the key. Get(key string) interface{} // NotNil checks if there are any errors in the builder. NotNil() bool // Converts the Builder into a request error. ToReqErr() E }
Builder is a type to incrementally build set of errors under common key structure Builder intentionally doesn't implement standard Error interface. You have to explicitly convert it into an Error (using ToReqErr) once all checks are done. Basic idea of builder is to easily combine request errors. Example:
var errb = NewBuilder() if len(obj.Name) < 3 { errb.Put("first_name", "name is too short") } if strings.Contains(obj.Name, "%") { errb.Put("first_name", "name contains invalid characters") } ... return errb.ToReqErr()
func NewBuilder ¶
func NewBuilder() Builder
NewBuilder creates new builders with given prefix being appended to each error keys. This structure is not thread safe.
type Domain ¶
type Domain *infrastructure
Domain error represent an error when our model is in wrong state (eg user is in impossible state from our domain stand). Compared to infrastructure error - when it is related that some operation failed on other service. Domain error is classified as a non Request error.
type E ¶
type E interface { error HasStatusCode HasStacktrace json.Marshaler IsReq() bool WithMsg(string) E }
E is error with more information. It is able to marshall itself to json as response. Result of Error() method should include stacktrace. Therefore it should not be displayed directly to the user
func NewDomain ¶
NewDomain creates new domain error from string Domain error is classified as an Infrastructure error.
func NewDomainF ¶
NewDomainF creates new domain error using string formatter
func NewReqDetails ¶
NewReqDetails creates a request error. Key inform which request parameter was invalid and details contains reason of error
func WrapAsDomain ¶
WrapAsDomain creates new domain error using error and string message Domain error is classified as an Infrastructure error.
func WrapAsDomainF ¶
WrapAsDomainF creates new domain error wrapping given error and using string formatter for description. Domain error is classified as an Infrastructure error.
func WrapAsInf ¶
WrapAsInf creates new infrastructure error from simple error If input argument is nil, nil is returned. If input argument is already errstack.E, it is returned unchanged.
func WrapAsInfF ¶
WrapAsInfF creates new infrastructural error wrapping given error and using string formatter for description.
func WrapAsReq ¶
WrapAsReq creates new request error from simple error If input argument is nil, nil is returned. If input argument is already errstack.E, it is returned unchanged.
func WrapAsReqF ¶
WrapAsReqF creates new request error from simple error and creates message from format If input argument is nil, nil is returned. If input argument is already errstack.E, it is returned unchanged.
type HasStacktrace ¶
HasStacktrace provides a function to return the the root stacktrace
type HasStatusCode ¶
type HasStatusCode interface {
StatusCode() int
}
HasStatusCode provides a function to return the HTTP status code.
type HasUnderlying ¶
type HasUnderlying interface {
Cause() error
}
HasUnderlying describes entity (usually an error) which has underlying error.
type Logger ¶
type Logger interface {
Error(msg string, ctx ...interface{})
}
Logger defines the reporting interface for utils error functions
type Putter ¶
Putter is an interface which provides a way to set an error abstracting from it's identifier (key). Please refer to the Builder documentation to see the example below without using Putter. Example:
validateName(obj.FirstName, errb.Putter("first_name")) validateName(obj.LastName, errb.Putter("last_name")) func validateName(name string, errp.Putter) { if len(name) < 3 { errp.Put(name is too short") } if strings.Contains(name, "%") { errp.Put(name contains invalid characters") } }
type StubPutter ¶
type StubPutter struct {
// contains filtered or unexported fields
}
StubPutter ignores all input. It only contains information whether any input was submitted
func (*StubPutter) HasError ¶
func (rp *StubPutter) HasError() bool
HasError checks if any error occurred
type UntilFirst ¶
type UntilFirst struct {
Err E
}
UntilFirst is a struct to easily chain a sequence of operations until a first error arise
func UntilFirstDo ¶
func UntilFirstDo(f func() E) *UntilFirst
UntilFirstDo is utility method for using UntilFirst