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.
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 ¶
AppendedWith gives another Wrapper with the additional string appended to the prefix which will be added to any subsequent errors.