Documentation ¶
Overview ¶
Package erk defines errors with kinds for Go 1.13+.
Index ¶
- Constants
- func GetKindString(err error) string
- func IsKind(err error, kind Kind) bool
- func New(kind Kind, message string) error
- func NewWith(kind Kind, message string, params Params) error
- func WithParam(err error, key string, value interface{}) error
- func WithParams(err error, params Params) error
- func Wrap(kind Kind, message string, err error) error
- func WrapAs(erkError error, err error) error
- func WrapWith(erkError error, err error, params Params) error
- type BaseExport
- type DefaultKind
- type DefaultPtrKind
- type Erkable
- type Error
- func (e *Error) Error() string
- func (e *Error) Export() ExportedErkable
- func (e *Error) ExportRawMessage() string
- func (e *Error) IndentError(indentLevel string) string
- func (e *Error) Is(err error) bool
- func (e *Error) Kind() Kind
- func (e *Error) MarshalJSON() ([]byte, error)
- func (e *Error) Params() Params
- func (e *Error) Unwrap() error
- func (e *Error) WithParams(params Params) error
- type ErrorIndentable
- type Exportable
- type ExportedErkable
- type ExportedError
- type Kind
- type Kindable
- type Paramable
- type Params
Constants ¶
const IndentSpaces = " "
IndentSpaces are the spaces to indent errors.
const OriginalErrorParam = "err"
OriginalErrorParam is the param key that contains the wrapped error.
This allows the original error to be used in message templates. Also, errors can be unwrapped, using errors.Unwrap(err).
Variables ¶
This section is empty.
Functions ¶
func GetKindString ¶ added in v0.2.0
GetKindString returns a string identifying the kind of the error.
If the kind embeds erk.DefaultKind, this will be a string with the package and type of the error's kind. This string can be overridden by implementing a KindStringFor method on a base kind, and embedding that in the error kind.
erk.DefaultKind Example:
erk.GetKindString(err) // Output: "github.com/username/package:ErkYourKind"
func WithParam ¶ added in v0.2.0
WithParam adds a parameter to an error.
If err does not satisfy Paramable, the original error is returned. A nil param value deletes the param key.
func WithParams ¶ added in v0.2.0
WithParams adds parameters to an error.
If err does not satisfy Paramable, the original error is returned. A nil param value deletes the param key.
Types ¶
type BaseExport ¶ added in v0.3.0
type BaseExport struct { Kind string `json:"kind"` Message string `json:"message"` Params Params `json:"params,omitempty"` }
BaseExport error that satisfies the ExportedErkable interface and is useful for JSON marshalling.
func (*BaseExport) ErrorKind ¶ added in v0.3.0
func (e *BaseExport) ErrorKind() string
ErrorKind returns the error kind.
func (*BaseExport) ErrorMessage ¶ added in v0.3.0
func (e *BaseExport) ErrorMessage() string
ErrorMessage returns the error message.
func (*BaseExport) ErrorParams ¶ added in v0.3.0
func (e *BaseExport) ErrorParams() Params
ErrorParams returns the error params.
type DefaultKind ¶ added in v0.2.0
type DefaultKind struct{}
DefaultKind should be embedded in most Kinds.
It is recommended to create new error kinds in each package. This allows erk to get the package name the error occurred in.
Example: See Kind.
func (DefaultKind) CloneKind ¶ added in v0.5.1
func (DefaultKind) CloneKind(kind Kind) Kind
CloneKind to a shallow copy.
If the kind is not a pointer, it is directly returned (since it was passed by value). If the kind is not a struct, it is directly returned (not supported for now). Otherwise, a shallow new copy of the struct is created using reflection, and the first layer of the struct is copyied using Set.
func (DefaultKind) KindStringFor ¶ added in v0.3.5
func (DefaultKind) KindStringFor(kind Kind) string
KindStringFor the provided kind.
func (DefaultKind) TemplateFuncsFor ¶ added in v0.5.0
func (DefaultKind) TemplateFuncsFor(kind Kind) template.FuncMap
TemplateFuncsFor the provided kind.
type DefaultPtrKind ¶ added in v0.5.2
type DefaultPtrKind struct{}
DefaultPtrKind is equivalent to DefaultKind, but enforces that the kinds are pointers.
func (*DefaultPtrKind) CloneKind ¶ added in v0.5.2
func (*DefaultPtrKind) CloneKind(kind Kind) Kind
CloneKind to a shallow copy.
func (*DefaultPtrKind) KindStringFor ¶ added in v0.5.2
func (*DefaultPtrKind) KindStringFor(kind Kind) string
KindStringFor the provided kind.
func (*DefaultPtrKind) TemplateFuncsFor ¶ added in v0.5.2
func (*DefaultPtrKind) TemplateFuncsFor(kind Kind) template.FuncMap
TemplateFuncsFor the provided kind.
type Erkable ¶ added in v0.3.0
type Erkable interface { Paramable Kindable Exportable error }
Erkable errors that have Params and a Kind, and can be exported.
type Error ¶ added in v0.2.0
type Error struct {
// contains filtered or unexported fields
}
Error stores details about an error with kinds and a message template.
func (*Error) Error ¶ added in v0.2.0
Error processes the message template with the provided params.
func (*Error) Export ¶ added in v0.3.0
func (e *Error) Export() ExportedErkable
Export creates a visible copy of the Error that can be used outside the erk package. A common use case is marshalling the error to JSON.
func (*Error) ExportRawMessage ¶ added in v0.5.6
ExportRawMessage without executing the template.
func (*Error) IndentError ¶ added in v0.4.1
IndentError processes the message template with the provided params and indentation.
The indentLevel represents the indentation of wrapped errors. Thus, it should start with " ".
func (*Error) MarshalJSON ¶ added in v0.4.2
MarshalJSON by exporting the error and then marshalling.
func (*Error) Unwrap ¶ added in v0.2.0
Unwrap implements the Go 1.13+ Unwrap interface for use with errors.Unwrap.
func (*Error) WithParams ¶ added in v0.3.0
WithParams adds parameters to a copy of the Error.
A nil param value deletes the param key.
type ErrorIndentable ¶ added in v0.4.1
ErrorIndentable allows you to specify an indent level for an error.
type Exportable ¶ added in v0.3.0
type Exportable interface { ExportRawMessage() string Export() ExportedErkable }
Exportable errors that support being exported to a JSON marshal friendly format.
type ExportedErkable ¶ added in v0.3.0
ExportedErkable is an exported readonly version of the Erkable interface.
func Export ¶ added in v0.3.0
func Export(err error) ExportedErkable
Export creates a visible copy of the error that can be used outside the erk package. A common use case is marshalling the error to JSON. If err is not an erk.Erkable, it is wrapped first.
type ExportedError ¶ added in v0.3.0
type ExportedError struct { Kind *string `json:"kind"` Type *string `json:"type,omitempty"` Message string `json:"message"` Params Params `json:"params,omitempty"` ErrorStack []ExportedErkable `json:"errorStack,omitempty"` }
ExportedError that can be used outside the erk package. A common use case is marshalling the error to JSON.
func (*ExportedError) ErrorKind ¶ added in v0.5.8
func (e *ExportedError) ErrorKind() string
ErrorKind returns the error kind.
func (*ExportedError) ErrorMessage ¶ added in v0.5.8
func (e *ExportedError) ErrorMessage() string
ErrorMessage returns the error message.
func (*ExportedError) ErrorParams ¶ added in v0.5.8
func (e *ExportedError) ErrorParams() Params
ErrorParams returns the error params.
type Kind ¶ added in v0.2.0
Kind represents an error kind.
Example:
package hello type ( ErkJSONUnmarshalling struct { erk.DefaultKind } ErkJSONMarshalling struct { erk.DefaultKind } ) ... // Creating an error with the kind err := erk.New(ErkJSONUnmarshalling, "failed to unmarshal JSON: '{{.json}}'") // Usually this would be a global error variable err = erk.WithParams(err, "json", originalJSON) ...
type Kindable ¶ added in v0.3.0
type Kindable interface {
Kind() Kind
}
Kindable errors that support housing an error Kind.
type Params ¶ added in v0.2.0
type Params map[string]interface{}
Params are key value parameters that are usuable in the message template.
func GetParams ¶ added in v0.2.0
GetParams returns the error's parameters.
If err does not satisfy Paramable, nil is returned.
func (Params) MarshalJSON ¶ added in v0.3.2
MarshalJSON by converting the "err" element to a string.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package erg allows grouping errors into an error group.
|
Package erg allows grouping errors into an error group. |
Package erkjson allows exporting errors as JSON.
|
Package erkjson allows exporting errors as JSON. |
Package erkmock allows creating erk errors to be returned from mocked interfaces.
|
Package erkmock allows creating erk errors to be returned from mocked interfaces. |
Package erkstrict controls if erk is running in strict mode.
|
Package erkstrict controls if erk is running in strict mode. |