errors

package
v0.1.1-0...-4a89cbd Latest Latest
Warning

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

Go to latest
Published: May 21, 2019 License: MPL-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package errors wraps Dave Cheney's errors package, by adding an error type and context to the error. Our custom errors implement the errors.Wrap interface, allowing a Cause function to work properly and allow the application to retrieve the cause of the error, and allows us to add context to errors by wrapping them.

err := fmt.Errorf("no such file")
err = Wrap(err, "open failed")
err = Wrap(err, "read config failed")
fmt.Println(err.Error()) // "read config failed: open failed: no such file"

If we wanted to find the original error, then we can use the Cause function, which returns the first error that doesn't implement a Cause() function.

originalError := Cause(err)
fmt.Println(originalError.Error()) // "no such file"

Additionally, we can add error types to the custom error, of a type ErrorType. By default, a NoType error is created, but we would also like to be able to create other types of errors, which can be done using

err = BadRequest.New("no such file")

and

err = BadRequest.Wrap(err, "open failed")

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cause

func Cause(err error) error

Cause returns the very original error

func New

func New(msg string, args ...interface{}) error

func Wrap

func Wrap(err error, msg string) error

func Wrapf

func Wrapf(err error, msg string, args ...interface{}) error

func XWrap

func XWrap(err error, msg string) error

XWrap is a convenience function around XWrapf

func XWrapf

func XWrapf(err error, msg string, args ...interface{}) error

XWrapf takes an error, and wraps it using Errorf by appending a ": %w" string to the end of the message, and appending the errors to the list of args. The resulting error implements the xerrors.Wrapper interface

Types

type ErrorType

type ErrorType uint

ErrorType is the type of an error.

const (
	NoType             ErrorType = iota // NoType error
	BadRequest                          // BadRequest error, data formatted badly
	Unauthorized                        // Unauthorized error, unauthenticated error
	Forbidden                           // Forbidden error, authentcated user without proper rights
	NotFound                            // NotFound error, resource is not found
	ServiceUnavailable                  // ServiceUnavailable error, Service is down
)

Each ErrorType can be mapped to the corresponding HTTP.Status error. For example, BadRequest ErrorType maps to http.StatusBadRequest.

func GetType

func GetType(err error) ErrorType

func (ErrorType) New

func (errorType ErrorType) New(msg string, args ...interface{}) error

New Creates a new custom error, with a defined error type. Function signature allows

func (ErrorType) Wrap

func (errorType ErrorType) Wrap(err error, msg string) error

Wrap implements the errors.Wrap interface, allow us to wrap a custom error in another custom error. Calls Wrapf under the hood.

func (ErrorType) Wrapf

func (errorType ErrorType) Wrapf(err error, msg string, args ...interface{}) error

Wrapf calls is like Wrap but allows additional arguments.

Jump to

Keyboard shortcuts

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