ergo: github.com/pnordahl/ergo Index | Files

package ergo

import "github.com/pnordahl/ergo"

Package ergo provides better errors for Go.

Index

Package Files

builtin.go error.go

Constants

const (
    // TrivialSeverity can be safely ignored, used for informative errors
    // that don't have a meaningful impact on program execution. For example, a
    // failed HTTP request to a remote API that will be retried multiple times.
    // Repeated TrivialSeverity errors will eventually result in a higher severity
    // being thrown. For this reason, it's optional to log these types of errors,
    // as the program is designed to handle them, else result in an error that will
    // be logged.
    TrivialSeverity uint8 = 1

    // LowSeverity is for significant, but not crash-worthy errors. For example,
    // one might assign LowSeverity to an error that is used to denote an HTTP
    // request that was tried multiple times but resulted in too many failures.
    // As this is typically not crash-worthy, errors of this type should never
    // result in a panic; they should be logged and squawked about in whatever
    // manner desired.
    LowSeverity uint8 = 4

    // HighSeverity is for errors that require the program to do something to
    // recover, else panic.
    HighSeverity uint8 = 6

    // CriticalSeverity errors should always result in a panic. Anything
    // receiving this error should pass the Error() method's contents to a
    // panic. A good example of this error is an inability to connect to a
    // database or remote resource that is critical to program function.
    CriticalSeverity uint8 = 8
)

func AddTemplate Uses

func AddTemplate(name string, template Template) error

AddTemplate adds a template to the package template registry. It is an error to register two templates by the same name.

type Context Uses

type Context interface {
    Error() string
    AddData(key string, data Data)
}

Context is what actually implements the standard library error interface. Before returning a Context, optionally call AddContextData to provide additional information that is available in the Error method for printing. Note that once you return a Context as a generic error, you will lose the ability to add more data to the error (since objects that implement error don't necessarily implement Context). However, since you've got a generic 'error', you could conceivably use a template's Enhance method on it. There's likely to be virtually no situations where this is advisable.

type ContextData Uses

type ContextData struct {
    Key  string
    Data Data
}

type Data Uses

type Data interface{}

type DataMap Uses

type DataMap map[string]Data

type Error Uses

type Error struct {
    ErrorTemplate
    OriginalError string       `json:"original_error,omitempty"`
    Data          ErrorDataMap `json:"data,omitempty"`
}

func (*Error) AddData Uses

func (self *Error) AddData(key string, data Data)

func (*Error) Error Uses

func (self *Error) Error() string

type ErrorDataMap Uses

type ErrorDataMap DataMap // alias preserves backwards compatibility for builtin

type ErrorTemplate Uses

type ErrorTemplate struct {
    Id           string `json:"id"`
    Severity     uint8  `json:"severity"`
    Message      string `json:"message"`
    Category     string `json:"category,omitempty"`
    OutputFormat string `json:"-"`
}

func (*ErrorTemplate) Enhance Uses

func (self *ErrorTemplate) Enhance(err error, data ...*ContextData) Context

Enhance creates a new Context from a provided error and template. This allows for the creation of enhanced errors from basic ones.

func (*ErrorTemplate) Render Uses

func (self *ErrorTemplate) Render(data ...*ContextData) Context

Render dumps the Template's information into a Context, optionally tacking on additional data before returning the Context.

type Template Uses

type Template interface {
    Render(...*ContextData) Context
    Enhance(error, ...*ContextData) Context
}

Templates can be declared as package-scoped globals and render them as needed, optionally inflating the error with any extra data you want to appear in the Error() output of the resulting Context.

func GetTemplate Uses

func GetTemplate(name string) Template

GetTemplate returns a template registered by AddTemplate, if it exists.

Package ergo imports 2 packages (graph). Updated 2016-07-15. Refresh now. Tools for package owners.