Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
github.com/bdlm/errors
bdlm/errors
provides simple, concise, useful error handling and annotation.
One of the biggest frustrations with Go error handling is the lack of forensic and meta information errors should provide. By default errors are just a string and possibly a type. They can't tell you where they occurred or the path through the call stack they followed. The error implementation in Go is robust enough to control program flow but it's not very efficient for troubleshooting or analysis.
Since the idom in Go is that we pass the error back up the stack anyway:
if nil != err {
return err
}
it's trivial to make errors much more informative with a simple error package. bdlm/errors
makes this easy and supports tracing the call stack and the error callers with relative ease. Custom error types are also fully compatible with this package and can be used freely.
All notable changes to this project are documented in the CHANGELOG
. The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.
See the documentation for more examples.
go get github.com/bdlm/errors
var MyError = errors.New("My error")
var MyError = errors.Errorf("My error #%d", 1)
if nil != err {
return errors.Wrap(err, "the operation failed")
}
err := try1()
if nil != err {
err2 := try2()
if nil != err2 {
return errors.Wrap(err, err2)
}
return err
}
err := doWork()
if prevErr := errors.Unwrap(err); nil != prevErr {
...
}
var MyError = errors.New("My error")
func main() {
err := doWork()
if errors.Is(err, MyError) {
...
}
}
var MyError = errors.New("My error")
func main() {
err := doWork()
if errors.Has(err, MyError) {
...
}
}
err := doWork()
for nil != err {
fmt.Println(err)
err = errors.Unwrap(err)
}
See the documentation for more examples.
Error
interfaceThe exported package methods return an interface that exposes additional metadata and troubleshooting information:
// Error defines the error interface.
type Error interface {
// Caller returns the associated Caller instance.
Caller() Caller
// Error implements error.
Error() string
// Has tests to see if the test error exists anywhere in the error
// stack.
Has(test error) bool
// Is tests to see if the test error matches most recent error in the
// stack.
Is(test error) bool
// Unwrap returns the next error, if any.
Unwrap() Error
}
// Caller holds runtime.Caller data.
type Caller interface {
// File returns the file in which the call occurred.
File() string
// Func returns the name of the function in which the call occurred.
Func() string
// Line returns the line number in the file in which the call occurred.
Line() int
// Pc returns the program counter.
Pc() uintptr
// Trace returns the call stack.
Trace() []Caller
}
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.