Get support for emperror/errors
If you're new to LTH, please see our FAQ for more information on what it is we do.
Support Options
Unfortunately, there are currently no active helpers for this repository on the platform. Until they become available, we reccomend the following actions:
View Open IssuesTake a look to see if anyone else has experienced the same issue as you and if they managed to solve it.
Open an IssueMake sure to read any relevant guidelines for opening issues on this repo before posting a new issue.
Sponsor directlyCheck out the page and see if there are any options to sponsor this project or it's developers directly.
emperror/errors
Emperror: Errors 
Drop-in replacement for the standard library errors
package and github.com/pkg/errors.
This is a single, lightweight library merging the features of standard library errors
package
and github.com/pkg/errors. It also backports a few features
(like Go 1.13 error handling related features).
Standard library features:
-
New
creates an error with stack trace -
Unwrap
supports both Go 1.13 wrapper (interface { Unwrap() error }
) and pkg/errors causer (interface { Cause() error }
) interface - Backported
Is
andAs
functions
github.com/pkg/errors features:
-
New
,Errorf
,WithMessage
,WithMessagef
,WithStack
,Wrap
,Wrapf
functions behave the same way as in the original library -
Cause
supports both Go 1.13 wrapper (interface { Unwrap() error }
) and pkg/errors causer (interface { Cause() error }
) interface
Additional features:
-
NewPlain
creates a new error without any attached context, like stack trace -
Sentinel
is a shorthand type for creating constant error -
WithStackDepth
allows attaching stack trace with a custom caller depth -
WithStackDepthIf
,WithStackIf
,WrapIf
,WrapIff
only annotate errors with a stack trace if there isn't one already in the error chain - Multi error aggregating multiple errors into a single value
-
NewWithDetails
,WithDetails
andWrap*WithDetails
functions to add key-value pairs to an error - Match errors using the
match
package
Installation
go get emperror.dev/errors
Usage
package main
import "emperror.dev/errors"
// ErrSomethingWentWrong is a sentinel error which can be useful within a single API layer.
const ErrSomethingWentWrong = errors.Sentinel("something went wrong")
// ErrMyError is an error that can be returned from a public API.
type ErrMyError struct {
Msg string
}
func (e ErrMyError) Error() string {
return e.Msg
}
func foo() error {
// Attach stack trace to the sentinel error.
return errors.WithStack(ErrSomethingWentWrong)
}
func bar() error {
return errors.Wrap(ErrMyError{"something went wrong"}, "error")
}
func main() {
if err := foo(); err != nil {
if errors.Cause(err) == ErrSomethingWentWrong { // or errors.Is(ErrSomethingWentWrong)
// handle error
}
}
if err := bar(); err != nil {
if errors.As(err, &ErrMyError{}) {
// handle error
}
}
}
Match errors:
package main
import (
"emperror.dev/errors"
"emperror.dev/errors/match"
)
// ErrSomethingWentWrong is a sentinel error which can be useful within a single API layer.
const ErrSomethingWentWrong = errors.Sentinel("something went wrong")
type clientError interface{
ClientError() bool
}
func foo() error {
// Attach stack trace to the sentinel error.
return errors.WithStack(ErrSomethingWentWrong)
}
func main() {
var ce clientError
matcher := match.Any{match.As(&ce), match.Is(ErrSomethingWentWrong)}
if err := foo(); err != nil {
if matcher.MatchError(err) {
// you can use matchers to write complex conditions for handling (or not) an error
// used in emperror
}
}
}
Development
Contributions are welcome! :)
- Clone the repository
- Make changes on a new branch
- Run the test suite:
./pleasew build ./pleasew test ./pleasew gotest ./pleasew lint
- Commit, push and open a PR
License
The MIT License (MIT). Please see License File for more information.
Certain parts of this library are inspired by (or entirely copied from) various third party libraries. Their licenses can be found in the Third Party License File.
Our Mission
We want to make open source more sustainable. The entire platform was born from this and everything we do is in aid of this.
From the Blog
Interesting Articles
-
Generating income from open source
Jun 23 • 8 min read
-
2023 State of OSS
Apr 23 • 45 min read ★
-
A funding experiment...
Aug 19 • 10 min read
-
But You Said I could
Aug 19 • 2 min read
Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated
p-e622a1a2