prettyerr

package
v0.0.0-...-3cc3ff2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 17, 2021 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultTextFormat = "{{ .Message }}\n{{ range $key, $value := .Details }}\t{{ $key }}: {{ JSON $value }}\n{{ end }}"
View Source
const (
	// GenericErrorCode is for errors which does not implement Prettifier and cannot be recognized with a default strategy
	GenericErrorCode = "GenericError"
)

Variables

View Source
var NoPrettifierErr = errors.New("prettifier is not defined")

NoPrettifierErr occurs when Prettify or NewStackWithStrategy cannot match a prettifier to the given error.

Functions

func StandardErr

func StandardErr(err error) error

StandardErr prettifies error instance

func TextFormatter

func TextFormatter(stack Errors, nodeTemplate string) (string, error)

TextFormatter represents Errors as a text. - stack is the Error stack - nodeTemplate is the template for a single stack element. Example: "({{.Code}}) {{.Message}}\n"

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.

func (Error) Error

func (e Error) Error() string

func (Error) Unwrap

func (e Error) Unwrap() error

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL