Convenience functions for one-liners for building stacks of errors, within built-in error interface. Also handle appending to error with dual-output functions.
e.g.
ifv, err:=someFunc(); err!=nil {
err=fmt.Errorf("Error doing <OPERATION> : %v\n", err)
returninterface{}, err
} else...becomes
import"github.com/noahhai/errors"returnerrors.GetFromTupleAdd("Error during <OPERATION>")(someFunc())import sysErrors "errors"import"github.com/noahhai/errors"mockApiFunc:=func() error {
returnsysErrors.New("unexpected column found")
}
modelFunc:=func() *errors.Error {
returnerrors.From(mockApiFunc()).Add("failed to upsert value")
}
businessFunc:=func() *errors.Error {
returnmodelFunc().AddF("failed to perform daily sync of customer '%d'", 35)
}
// consumehandlerFunc:=func() {
iferr:=businessFunc(); err!=nil {
fmt.Println("Error handling request")
fmt.Println("Error: ")
fmt.Println(err)
fmt.Println()
fmt.Println("Cause:")
fmt.Println(err.Cause())
fmt.Println()
fmt.Println("Symptom:")
fmt.Println(err.Symptom())
}
}
handlerFunc()
// return as built-in Error, e.g. to AWS API GatewayextHandlerFunc:=func() error {
returnbusinessFunc()
}
_=extHandlerFunc()