Documentation ¶
Index ¶
Constants ¶
const DefaultTextFormat = "{{ .Message }}\n{{ range $key, $value := .Details }}\t{{ $key }}: {{ JSON $value }}\n{{ end }}"
const (
// GenericErrorCode is for errors which does not implement Prettifier and cannot be recognized with a default strategy
GenericErrorCode = "GenericError"
)
Variables ¶
var NoPrettifierErr = errors.New("prettifier is not defined")
NoPrettifierErr occurs when Prettify or NewStackWithStrategy cannot match a prettifier to the given error.
Functions ¶
Types ¶
type Error ¶
type Error struct { // Original error, might be empty. Original error `json:"-"` // Message is a custom pretty error message. Might be different from the original error message Message string `json:"message"` // Details are some details to extend the error information Details map[string]interface{} `json:"details"` // Code is a constant error code to let consumers referring to the rror by its code. // The message might be changed, but the code should not Code string `json:"code"` // Suggestion is the advise to users how to fix the error. Suggestion string `json:"suggestion"` }
Error describes the error with details, organized in a standard structure. Error also implements `error` interface.
type Errors ¶
type Errors []Error
func Prettify ¶
func Prettify(err error, opts ...StackOptionFn) (Errors, error)
Prettify builds a Errors from the given error and all the wrapped errors: prettify, unwrap, prettify, repeat. By default, PrettifierStrategy is used. You can override the strategy using the options: Prettify(err, func(o *StackOptions) { o.Strategy = SomeStrategy{} })
The function expects the strategy returns a prettifier for each error. NoPrettifierErr is returned if strategy does not match a prettifier for the error or any wrapped error.
type Prettifier ¶
type Prettifier interface { // Prettify the error and return Error Prettify() Error }
Prettifier builds Error
type PrettifierStrategy ¶
type PrettifierStrategy struct{}
PrettifierStrategy is a strategy based on calling Prettify method on Prettifier. If the error does not implement Prettifier, it fallbacks to the generic one.
func (PrettifierStrategy) Match ¶
func (PrettifierStrategy) Match(err error) Prettifier
type PrettyfierFunc ¶
type PrettyfierFunc func() Error
PrettyfierFunc is a functional way to write Pretifier.
func (PrettyfierFunc) Prettify ¶
func (f PrettyfierFunc) Prettify() Error
type StackOptionFn ¶
type StackOptionFn func(*StackOptions)
type StackOptions ¶
type StackOptions struct {
Strategy Strategy
}
type Strategy ¶
type Strategy interface {
Match(error) Prettifier
}
Strategy selects and returns a prettifier for the given error