microerror

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2020 License: Apache-2.0 Imports: 6 Imported by: 3,624

README

godoc CircleCI

microerror

Go library to facilitate error handling, based on juju/errgo

Documentation

Overview

Package microerror provides project wide helper functions for a more convenient and efficient error handling.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cause

func Cause(err error) error

Cause returns the cause of the given error. If the cause of the err can not be found it returns the err itself.

Cause is the usual way to diagnose errors that may have been wrapped by Mask or Maskf.

func Desc

func Desc(err error) string

Desc returns the description of a microerror.Error.

func Mask

func Mask(err error) error

Mask is a simple error masker. Masked errors act as tracers within the source code. Inspecting an masked error shows where the error was passed through within the code base. This is gold for debugging and bug hunting.

func Maskf

func Maskf(err error, f string, v ...interface{}) error

Maskf is like Mask. In addition to that it takes a format string and variadic arguments like fmt.Sprintf. The format string and variadic arguments are used to annotate the given errgo error.

func New

func New(s string) error

New returns a new error with the given error message. It is a drop-in replacement for errors.New from the standard library.

func Newf

func Newf(f string, v ...interface{}) error

Newf returns a new error with the given printf-formatted error message.

func Stack

func Stack(err error) string

Stack prints the error with the stack if its argument is underlying microerror error or result of Error function otherwise. Its main purpose is to be used for a value for "stack" micrologger key.

Example:

logger.LogCtx(ctx, "level", "error", "message", "failed to do a thing", "stack", microerror.Stack(err))

Types

type ErrgoHandler

type ErrgoHandler struct {
	// contains filtered or unexported fields
}

ErrgoHandler implements Handler interface.

func NewErrgoHandler

func NewErrgoHandler(config ErrgoHandlerConfig) *ErrgoHandler

func (*ErrgoHandler) Cause

func (h *ErrgoHandler) Cause(err error) error

func (*ErrgoHandler) Mask

func (h *ErrgoHandler) Mask(err error) error

func (*ErrgoHandler) Maskf

func (h *ErrgoHandler) Maskf(err error, f string, v ...interface{}) error

func (*ErrgoHandler) New

func (h *ErrgoHandler) New(s string) error

func (*ErrgoHandler) Newf

func (h *ErrgoHandler) Newf(f string, v ...interface{}) error

type ErrgoHandlerConfig

type ErrgoHandlerConfig struct {
	// CallDepth is useful when creating a wrapper for ErrgoHandler. Its
	// value is used to push stack location and skip wrapping function
	// location as an origin. The default value is 0.
	CallDepth int
}

func DefaultErrgoHandlerConfig

func DefaultErrgoHandlerConfig() ErrgoHandlerConfig

type Error

type Error struct {
	Desc string `json:"desc"`
	Docs string `json:"docs"`
	Kind string `json:"kind"`
}

Error is a predefined error structure whose purpose is to act as container for meta information associated to a specific error. The specific error type matching can be used as usual. The usual error masking and cause gathering can be used as usual. Using Error might look as follows. In the beginning is a usual error defined, along with its matcher. This error is the root cause once emitted during runtime.

var notEnoughWorkersError = &microerror.Error{
    Desc: "The amount of requested tenant cluster workers exceeds the available number of control plane nodes.",
    Docs: "https://github.com/giantswarm/ops-recipes/blob/master/349-not-enough-workers.md",
    Kind: "notEnoughWorkersError",
}

func IsNotEnoughWorkers(err error) bool {
    return microerror.Cause(err) == notEnoughWorkersError
}

func (*Error) Error

func (e *Error) Error() string

func (*Error) GoString

func (e *Error) GoString() string

func (*Error) String

func (e *Error) String() string

type Handler

type Handler interface {
	// New returns a new error with the given error message. It is
	// a drop-in replacement for errors.New from the standard library.
	//
	// NOTE deprecated
	//
	New(s string) error

	// Newf returns a new error with the given printf-formatted error
	// message.
	//
	// NOTE deprecated
	//
	Newf(f string, v ...interface{}) error

	// Cause returns the cause of the given error. If the cause of the err can not
	// be found it returns the err itself.
	//
	// Cause is the usual way to diagnose errors that may have been wrapped by Mask
	// or Maskf.
	Cause(err error) error

	// Mask is a simple error masker. Masked errors act as tracers within the
	// source code. Inspecting an masked error shows where the error was passed
	// through within the code base. This is gold for debugging and bug hunting.
	Mask(err error) error

	// Maskf is like Mask. In addition to that it takes a format string and
	// variadic arguments like fmt.Sprintf. The format string and variadic
	// arguments are used to annotate the given error.
	Maskf(err error, f string, v ...interface{}) error
}

Jump to

Keyboard shortcuts

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