Documentation
¶
Overview ¶
Package zrr provides a way to add and inspect type safe error context.
The error context might be useful for example when logging errors which were created in some deeper parts of your code.
Index ¶
- Constants
- Variables
- func GetBool(err error, key string) (bool, bool)
- func GetCode(err error) string
- func GetFloat64(err error, key string) (float64, bool)
- func GetInt(err error, key string) (int, bool)
- func GetInt64(err error, key string) (int64, bool)
- func GetStr(err error, key string) (string, bool)
- func GetTime(err error, key string) (time.Time, bool)
- func HasCode(err error, codes ...string) bool
- func HasKey(err error, key string) bool
- func IsImmutable(err error) bool
- type Error
- func (e *Error) Bool(key string, b bool) *Error
- func (e *Error) ErrCode() string
- func (e *Error) Error() string
- func (e *Error) Float64(key string, f float64) *Error
- func (e *Error) GetMetadata() map[string]interface{}
- func (e *Error) Int(key string, i int) *Error
- func (e *Error) Int64(key string, i int64) *Error
- func (e *Error) MarshalJSON() ([]byte, error)
- func (e *Error) SetErrMetadata(src map[string]interface{}) *Error
- func (e *Error) SetMetadataFrom(src MetadataGetter) *Error
- func (e *Error) Str(key string, s string) *Error
- func (e *Error) StrAppend(key string, s string) *Error
- func (e *Error) Time(key string, t time.Time) *Error
- func (e *Error) UnmarshalJSON(data []byte) error
- func (e *Error) Unwrap() error
- type MetadataErrSetter
- type MetadataGetter
Examples ¶
Constants ¶
const ECInvJSON = "ECInvJSON"
ECInvJSON represents invalid JSON error code.
Variables ¶
var ErrInvJSON = Imm("invalid JSON", ECInvJSON)
ErrInvJSON represents package level error indicating JSON structure or format error.
Functions ¶
func GetBool ¶ added in v0.3.0
GetBool returns the key as a boolean if err is an instance of Error and key exists. If key does not exist, or it is not a boolean it will return false as the second return value.
func GetCode ¶ added in v0.5.0
GetCode returns error code if error err is instance of Error. If error code is not set it will return empty string.
Example ¶
package main import ( "fmt" "github.com/rzajac/zrr" ) func main() { err := zrr.New("message", "ECode").Int("retry", 5) fmt.Println(zrr.GetCode(err)) }
Output: ECode
func GetFloat64 ¶ added in v0.3.0
GetFloat64 returns the key as a float64 if err is an instance of Error and key exists. If key does not exist, or it's not a float64 it will return false as the second return value.
func GetInt ¶ added in v0.3.0
GetInt returns the key as an integer if err is an instance of Error and key exists. If key does not exist, or it's not an integer it will return false as the second return value.
Example ¶
package main import ( "fmt" "github.com/rzajac/zrr" ) func main() { var err error err = zrr.New("message", "ECode").Int("retry", 5) fmt.Println(zrr.GetInt(err, "retry")) // 5 true fmt.Println(zrr.GetInt(err, "not_set")) // 0 false fmt.Println(zrr.HasKey(err, "retry")) // true fmt.Println(zrr.HasKey(err, "not_set")) // false }
Output: 5 true 0 false true false
func GetInt64 ¶ added in v0.5.0
GetInt64 returns the key as an int64 if err is an instance of Error and key exists. If key does not exist, or it's not an int64 it will return false as the second return value.
func GetStr ¶ added in v0.3.0
GetStr returns the key as a string if err is an instance of Error and key exists. If key does not exist, or it's not a string it will return false as the second return value.
func GetTime ¶ added in v0.3.0
GetTime returns the key as a time.Time if err is an instance of Error and key exists. If key does not exist, or it's not a time.Time it will return false as the second return value.
func HasCode ¶ added in v0.3.0
HasCode returns true if error err is instance of Error and has any of the codes.
func HasKey ¶ added in v0.3.0
HasKey returns true if error err is instance of Error and has the key set.
Example ¶
package main import ( "fmt" "github.com/rzajac/zrr" ) func main() { err := zrr.New("message", "ECode").Int("retry", 5) fmt.Println(zrr.GetInt(err, "retry")) fmt.Println(zrr.GetInt(err, "not_set")) }
Output: 5 true 0 false
func IsImmutable ¶ added in v0.3.0
IsImmutable returns true if error err is instance of Error and is immutable.
Types ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error represents an error with metadata key value pairs.
Example ¶
package main import ( "errors" "fmt" "time" "github.com/rzajac/zrr" ) func main() { // Create an error and add a bunch of context fields to it. err := zrr.Wrap(errors.New("std error"), "ECode"). Str("str", "string"). Int("int", 5). Float64("float64", 1.23). Time("time", time.Date(2020, time.October, 7, 23, 47, 0, 0, time.UTC)). Bool("bool", true) fmt.Println(err.Error()) fmt.Println(zrr.GetStr(err, "str")) fmt.Println(zrr.GetInt(err, "int")) fmt.Println(zrr.GetFloat64(err, "float64")) fmt.Println(zrr.GetTime(err, "time")) fmt.Println(zrr.GetBool(err, "bool")) fmt.Println(err.ErrCode()) }
Output: std error string true 5 true 1.23 true 2020-10-07 23:47:00 +0000 UTC true true true ECode
Example (WrappingZrrError) ¶
package main import ( "errors" "fmt" "github.com/rzajac/zrr" ) func main() { err := zrr.New("my error").Str("key", "value") e1 := fmt.Errorf("zrr wrapped: %w", err) fmt.Println(e1) fmt.Println(errors.Is(e1, err)) }
Output: zrr wrapped: my error true
func Imm ¶
Imm is a constructor returning new immutable Error instance.
Immutable error instances are never changed when adding / changing fields. They are good choice for package level errors.
Error code is optional, if more than one code is provided the first one will be used.
Example ¶
package main import ( "errors" "fmt" "github.com/rzajac/zrr" ) func main() { // Create immutable error. var ErrPackageLevel = zrr.Imm("package level error", "ECode") // Somewhere in the code use ErrPackageLevel and add context to it. err := zrr.Wrap(ErrPackageLevel, "ENewCode").Str("path", "/path/to/file") fmt.Println(ErrPackageLevel, zrr.GetCode(ErrPackageLevel)) // Notice the error code has not been changed. fmt.Println(err, zrr.GetCode(err)) fmt.Println(errors.Is(err, ErrPackageLevel)) }
Output: package level error ECode package level error ENewCode true
func Newf ¶
Newf is a constructor returning new Error instance. Arguments are handled in the same manner as in fmt.Errorf.
func Wrap ¶
Wrap wraps err in Error instance. It returns nil if err is nil.
Example ¶
package main import ( "errors" "fmt" "github.com/rzajac/zrr" ) func main() { err := errors.New("some error") e1 := zrr.Wrap(err).Str("key", "value") fmt.Println(e1) fmt.Println(errors.Is(e1, err)) }
Output: some error true
func (*Error) Error ¶
Error implements error interface and returns error message and key value pairs associated with it separated by MsgSep.
Example ¶
package main import ( "fmt" "github.com/rzajac/zrr" ) func main() { err := zrr.New("message").Int("retry", 5) fmt.Println(err.Error()) }
Output: message
func (*Error) GetMetadata ¶ added in v0.15.0
GetMetadata returns error metadata. The returned metadata map should be considered read-only.
func (*Error) MarshalJSON ¶ added in v0.14.0
func (*Error) SetErrMetadata ¶ added in v0.15.0
SetErrMetadata sets error metadata. The returned instance might be different from the one this method is called if the error is immutable.
func (*Error) SetMetadataFrom ¶ added in v0.15.0
func (e *Error) SetMetadataFrom(src MetadataGetter) *Error
SetMetadataFrom is a convenience method setting metadata from an instance which implements MetadataGetter.
func (*Error) StrAppend ¶ added in v0.10.0
StrAppend appends the string s (prefixed with semicolon) to the string represented by key k. The key will be added if it doesn't exist. If the key already exists and is not a string the old key will be overwritten.
func (*Error) UnmarshalJSON ¶ added in v0.14.0
UnmarshalJSON unmarshal error's JSON representation. Notes:
- all metadata numeric values will be unmarshalled as float64
type MetadataErrSetter ¶ added in v0.15.0
type MetadataErrSetter interface { // SetErrMetadata sets error metadata. The returned instance might be // different from the one this method is called if the error is immutable. SetErrMetadata(map[string]interface{}) *Error }
MetadataErrSetter is an interface wrapping SetErrMetadata method.
type MetadataGetter ¶ added in v0.15.0
type MetadataGetter interface { // GetMetadata returns error metadata. The returned metadata map should be // considered read-only. GetMetadata() map[string]interface{} }
MetadataGetter is an interface wrapping GetMetadata method.