errorutil

package
v0.0.0-...-8cba18c Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2021 License: MIT Imports: 3 Imported by: 5

README

go-common/errorutil

This repository contains the go-common/errorutil library.

To install:

go get github.com/ugorji/go-common/errorutil

Package Documentation

Package errorutil contains functions and types for error management.

In general, idiomatic error handling in go follows the following principles:

  • Panic for exceptional conditions. These include: Unexpected/invalid input parameters, ...
  • Return errors for expected conditions e.g. file may not exist, network may be down

To be explicit, do not return errors for invalid inputs. Instead, check the inputs and panic if invalid. This makes the APIs better (as error checking does not have to be done). It also ensures that input validation should be done by the caller.

Errors occur as a result of a number of things:

  • An action generated an error which MUST be reported to callers with more context
  • An action generated an error which MUST be reported to callers AS IS
  • Something bad happened which MUST be reported to callers

When reporting an error with more context, user MUST add the action being performed

    e.g. "opening passwd.txt"

Generated errors SHOULD include succinct context information (if possible). This way, caller SHOULD NOT need to re-compute context of the error.

    e.g. "missing read permission", "end of file", "index out of bounds (5/4)", "mismatch: 7.5/7.1"

There are times when it is idiomatic to report an error AS IS. These include times where context is not necessary, as the function does a pass-through to another function.

In general, a rich error contains the following:

  • what action was being performed?
  • what error (if any) occurred while performing this action?
  • where in the code did this occur?

In general, follow these rules when generating errors:

  • Error messages are always in lower case
  • never have "error" in front of them
  • always fit in a single line (We already know it's an error message)
  • Always are of form: "generting UUID: got time of 0 unexpectedly"
  • When generated, there is no "action" (The user already knows what action they called. "action" only comes to play when propagating an error)
  • When propagating an error, always propagate a new error appropriately (do not "blindly" throw errors).
  • Errors should be infrequent, so it is ok to determine whether to include "context" information when creating errors. I always do.

This package just provides some helpers for this principle above

  • Wrapper that exposes its wrapped error when Unwrap is called. We did this so it would be aligned with go 1.13 xerrors package.

Exported Package API

func Base(err error) error
func OnError(err *error)
func OnErrorf(err *error, message string, params ...interface{})
type Context struct{ ... }
type Multi []error
type Rich struct{ ... }
    func NewRich(action string, cause error) *Rich
type String string
type Wrapper interface{ ... }

Documentation

Overview

Package errorutil contains functions and types for error management.

In general, idiomatic error handling in go follows the following principles:

  • Panic for exceptional conditions. These include: Unexpected/invalid input parameters, ...
  • Return errors for expected conditions e.g. file may not exist, network may be down

To be explicit, do not return errors for invalid inputs. Instead, check the inputs and panic if invalid. This makes the APIs better (as error checking does not have to be done). It also ensures that input validation should be done by the caller.

Errors occur as a result of a number of things:

  • An action generated an error which MUST be reported to callers with more context
  • An action generated an error which MUST be reported to callers AS IS
  • Something bad happened which MUST be reported to callers

When reporting an error with more context, user MUST add the action being performed

e.g. "opening passwd.txt"

Generated errors SHOULD include succinct context information (if possible). This way, caller SHOULD NOT need to re-compute context of the error.

e.g. "missing read permission", "end of file", "index out of bounds (5/4)", "mismatch: 7.5/7.1"

There are times when it is idiomatic to report an error AS IS. These include times where context is not necessary, as the function does a pass-through to another function.

In general, a rich error contains the following:

  • what action was being performed?
  • what error (if any) occurred while performing this action?
  • where in the code did this occur?

In general, follow these rules when generating errors:

  • Error messages are always in lower case
  • never have "error" in front of them
  • always fit in a single line (We already know it's an error message)
  • Always are of form: "generting UUID: got time of 0 unexpectedly"
  • When generated, there is no "action" (The user already knows what action they called. "action" only comes to play when propagating an error)
  • When propagating an error, always propagate a new error appropriately (do not "blindly" throw errors).
  • Errors should be infrequent, so it is ok to determine whether to include "context" information when creating errors. I always do.

This package just provides some helpers for this principle above

  • Wrapper that exposes its wrapped error when Unwrap is called. We did this so it would be aligned with go 1.13 xerrors package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Base

func Base(err error) error

Base returns the underlying cause of an error. If a *Err, it returns the base of its cause. Else it returns the error passed.

func OnError

func OnError(err *error)

OnError is called to enhance the error passed. If the passed in error is not nil, wrap it with context information.

Most callers use it from defer functions.

IMPORTANT: Call this directly from the call site for which you want to see the file/line context.

func OnErrorf

func OnErrorf(err *error, message string, params ...interface{})

OnErrorf is called to enhance the error passed. Similar to OnError but includes a custom message.

Types

type Context

type Context struct {
	Subsystem string
	File      string
	FuncName  string
	Line      int
	Time      time.Time
}

Context is time and location in code where an error occurred

func (*Context) String

func (x *Context) String() string

String returns a string containing fields of the *Context (subsystem, file, line, etc) e.g. cart [file.go:123 (*struc).Name]

type Multi

type Multi []error

Multi is a slice of errors, which acts as a single error

func (Multi) Error

func (em Multi) Error() string

func (Multi) First

func (e Multi) First() error

func (Multi) HasError

func (e Multi) HasError() (b bool)

func (Multi) NonNil

func (e Multi) NonNil() Multi

Returns the subset of this Multi which are non nil. Note that this is not same as err=nil if they are all nil. Use NonNilError if you need to pass a nil value if non-nils.

func (Multi) NonNilError

func (e Multi) NonNilError() error

type Rich

type Rich struct {

	// Action is what was being performed e.g. "opening passwd.txt", "checking time"
	Action string
	// Cause is the encapsulated error e.g. "incorrect permissions", "end of file reached"
	Cause error
	// Context is where in the code the error occurred
	Context *Context
}

Rich is a rich error encapsulating a cause, program context and an optional cause.

func NewRich

func NewRich(action string, cause error) *Rich

New returns a new Err, with context added if depth >= 0

func (*Rich) Error

func (e *Rich) Error() (s string)

func (*Rich) Unwrap

func (e *Rich) Unwrap() error

type String

type String string

String wraps a string as an error

func (String) Error

func (e String) Error() string

type Wrapper

type Wrapper interface {
	Unwrap() error
}

Jump to

Keyboard shortcuts

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