8000 GitHub - axelzv9/errorx: Native Golang error extension
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

axelzv9/errorx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Golang native error extension

Helper for customization and wrapping actual errors, hiding from users

Usage example

// wrap inner error
err := errors.New("new inner error")
err = errorx.New("user_friendly_text", errorx.WithInner(err))
...
// add caller line
err = errorx.New("user_friendly_text", errorx.WithInner(err), errorx.WithCaller())
...
// add custom fields
err = errorx.New("user_friendly_text", 
	errorx.WithInner(err), 
	errorx.WithString("key", "value"),
	errorx.WithInt("key2", 123),
)
...
// typed error
const CustomType errorx.Type = 1

err = errorx.New("user_friendly_text", errorx.WithInner(err), errorx.WithType(CustomType))
...
// extract custom fields to json
if errX := errorx.AsError(err); errX != nil {
    str, _ := errX.JSONString()
    log.Printf("error: %s fields: %s", err, str)
}
...
// extract custom fields
if errX := errorx.AsError(err); errX != nil {
    for _, field := range errX.Fields() {
		var key, value string
		key = field.Key
        value = field.Value
    }
}
...
// check custom type
switch errX.Type() {
    case CustomType:
		// handle custom type err
    default:
        // do something else
}

See full integration example here.

About

Native Golang error extension

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

0