errors

package
v0.0.0-...-1151264 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2019 License: GPL-3.0 Imports: 1 Imported by: 12

Documentation

Overview

Package errors is intended to basically replace the default errors package. It is intended that one could replace an error import with an import of this package instead and code should (hopefully) not have to change as a result. In exchange, this slight tweak allows for error constants instead of just package-level error variables. It seems that doing so would likely result in much more noise-making in the event that a bug leads to code attempting to overwrite one of these errors.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorString

type ErrorString string

ErrorString is a minimal implementation of the error interface which can be used as either a variable or a constant.

Since the original errors.errorString type was a struct and unexported, it is impossible to use it to create an error constant. However, since even the original errorString type stored only the string in question in its struct, nothing should be lost by literally defining the type as another name for a string. By declaring this ErrorString as an exported type defined to be a string, it is possible to use ErrorString as a variable or a constant.

func (ErrorString) Error

func (e ErrorString) Error() string

Error allows ErrorString to implement the error interface.

Since a type needs only to implement Error and return a string, it is easy enough to make even a constant string trivially implement this type by having the function simply return its backing data structure re-cast as a string.

type New

type New string

New is a minimal implementation of the error interface which can be used as either a variable or a constant without many changes to existing code calling the similarly named constructor from the original errors package.

Since the standard practice is to declare error variables using errors.New, a cheap hack to make this continue to work alongside the desired error constants given by ErrorString is to also declare New as an exported type defined to be a string. Even though this behavior is an arguably more significant departure from the original than the ErrorString behavior is, the syntax for declaring such an error should be identical, and the flexibility achieved thanks to Go's approach to interfaces should make this significant change in nature invisible to all other code.

func (New) Error

func (e New) Error() string

Error allows (the data type) New to implement the error interface.

Just as with ErrorString, by implementing this function, this version of errors.New will look just as much like an error as the original despite a significant change in the nature of the underlying data type.

type Wrapper

type Wrapper string

Wrapper provides a convenient mechanism for wrapping a number of errors with a common prefix.

Example
wrapper := Wrapper("Error in wrapper test")
appendedWrapper := wrapper.AppendedWith("Appended error")

baseErr1 := New("Some error")
err1 := appendedWrapper.Wrap(baseErr1)

err2 := wrapper.Wrap(nil)

baseErr3 := fmt.Errorf("Yet another %s", "error")
err3 := wrapper.Wrap(baseErr3)

fmt.Printf(
	"err1 is = %v\nerr2 is = %v\nerr3 is = %v\n",
	err1,
	err2,
	err3,
)
Output:

err1 is = Error in wrapper test: Appended error: Some error
err2 is = <nil>
err3 is = Error in wrapper test: Yet another error

func (Wrapper) AppendedWith

func (w Wrapper) AppendedWith(s string) Wrapper

AppendedWith gives another Wrapper with the additional string appended to the prefix which will be added to any subsequent errors.

func (Wrapper) Wrap

func (w Wrapper) Wrap(err error) error

Wrap prepends the previously set prefix to the given non-nil error. It will refrain from wrapping a nil error.

Jump to

Keyboard shortcuts

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