Top Related Projects
A Go (golang) package for representing a list of errors as a single error.
A comprehensive error handling library for Go
Error handling library with readable stack traces and flexible formatting support 🎆
Quick Overview
pkg/errors is a Go package that provides simple error handling primitives. It aims to enhance the standard library's error handling capabilities by adding stack traces, wrapping errors, and providing additional context to errors.
Pros
- Adds stack traces to errors, making debugging easier
- Allows wrapping errors with additional context
- Maintains compatibility with the standard library's error interface
- Provides a simple and intuitive API for error handling
Cons
- Adds a dependency to projects that could otherwise use the standard library
- May increase memory usage due to storing stack traces
- Can lead to verbose error handling code if overused
- Not part of the Go standard library, which may limit adoption in some projects
Code Examples
- Creating a new error with stack trace:
import "github.com/pkg/errors"
func someFunction() error {
return errors.New("something went wrong")
}
- Wrapping an existing error:
import "github.com/pkg/errors"
func doSomething() error {
err := someOtherFunction()
if err != nil {
return errors.Wrap(err, "failed to do something")
}
return nil
}
- Accessing the cause of an error:
import "github.com/pkg/errors"
func handleError(err error) {
cause := errors.Cause(err)
switch cause.(type) {
case *MyCustomError:
// Handle specific error type
default:
// Handle other errors
}
}
Getting Started
To use pkg/errors in your Go project, follow these steps:
-
Install the package:
go get github.com/pkg/errors
-
Import the package in your Go code:
import "github.com/pkg/errors"
-
Start using the package to create and handle errors:
func example() error { err := someFunction() if err != nil { return errors.Wrap(err, "failed in example function") } return nil }
Competitor Comparisons
A Go (golang) package for representing a list of errors as a single error.
Pros of go-multierror
- Allows combining multiple errors into a single error value
- Provides error formatting options for better readability
- Supports custom error types and interfaces
Cons of go-multierror
- Less widely adopted compared to errors
- May introduce additional complexity for simple error handling scenarios
- Limited stack trace functionality
Code Comparison
errors:
err := errors.New("error message")
wrappedErr := errors.Wrap(err, "additional context")
go-multierror:
var result error
result = multierror.Append(result, errors.New("error 1"))
result = multierror.Append(result, errors.New("error 2"))
Key Differences
- Error aggregation: go-multierror focuses on combining multiple errors, while errors emphasizes error wrapping and context.
- Stack traces: errors provides built-in stack trace support, which is not a primary feature in go-multierror.
- Usage complexity: errors is generally simpler for basic error handling, while go-multierror offers more flexibility for complex error scenarios.
Use Cases
-
Choose errors for:
- Simple error wrapping and context addition
- Projects requiring detailed stack traces
- Widespread compatibility with other Go libraries
-
Choose go-multierror for:
- Scenarios involving multiple potential errors
- Custom error formatting requirements
- Projects already using other HashiCorp libraries
A comprehensive error handling library for Go
Pros of errorx
- Offers more advanced error handling features like error types, error traits, and error namespaces
- Provides better error formatting and printing capabilities
- Includes built-in support for error wrapping and unwrapping
Cons of errorx
- Has a steeper learning curve due to its more complex API
- May be considered overkill for simpler projects or applications
- Requires more code to set up and use effectively
Code Comparison
errorx:
err := errorx.DecorateMany("operation failed",
errorx.NewType("custom_error"),
errors.New("underlying error"))
errors:
err := errors.Wrap(errors.New("underlying error"), "operation failed")
Summary
errorx offers more advanced error handling features and better formatting capabilities compared to errors. However, it has a steeper learning curve and may be considered overkill for simpler projects. errors provides a simpler API and is easier to use, but lacks some of the more advanced features found in errorx. The choice between the two depends on the specific needs of your project and the level of error handling complexity required.
Error handling library with readable stack traces and flexible formatting support 🎆
Pros of eris
- Supports error wrapping with additional context and stack traces
- Provides a more comprehensive set of error handling functions
- Offers better performance in some scenarios, especially when dealing with large error stacks
Cons of eris
- Slightly more complex API compared to pkg/errors
- May require more code changes when migrating from standard library errors
Code Comparison
eris:
err := eris.New("base error")
wrappedErr := eris.Wrap(err, "additional context")
fmt.Println(eris.ToString(wrappedErr, true))
pkg/errors:
err := errors.New("base error")
wrappedErr := errors.Wrap(err, "additional context")
fmt.Printf("%+v\n", wrappedErr)
Both libraries provide similar functionality for creating and wrapping errors. However, eris offers more advanced features for error handling and formatting, while pkg/errors focuses on simplicity and compatibility with the standard library.
eris is generally more feature-rich and performant, but may require a steeper learning curve. pkg/errors is simpler and more widely adopted, making it easier to integrate into existing projects. The choice between the two depends on the specific needs of your project and your preference for simplicity versus advanced features.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
errors
Package errors provides simple error handling primitives.
go get github.com/pkg/errors
The traditional error handling idiom in Go is roughly akin to
if err != nil {
return err
}
which applied recursively up the call stack results in error reports without context or debugging information. The errors package allows programmers to add context to the failure path in their code in a way that does not destroy the original value of the error.
Adding context to an error
The errors.Wrap function returns a new error that adds context to the original error. For example
_, err := ioutil.ReadAll(r)
if err != nil {
return errors.Wrap(err, "read failed")
}
Retrieving the cause of an error
Using errors.Wrap
constructs a stack of errors, adding context to the preceding error. Depending on the nature of the error it may be necessary to reverse the operation of errors.Wrap to retrieve the original error for inspection. Any error value which implements this interface can be inspected by errors.Cause
.
type causer interface {
Cause() error
}
errors.Cause
will recursively retrieve the topmost error which does not implement causer
, which is assumed to be the original cause. For example:
switch err := errors.Cause(err).(type) {
case *MyError:
// handle specifically
default:
// unknown error
}
Read the package documentation for more information.
Roadmap
With the upcoming Go2 error proposals this package is moving into maintenance mode. The roadmap for a 1.0 release is as follows:
- 0.9. Remove pre Go 1.9 and Go 1.10 support, address outstanding pull requests (if possible)
- 1.0. Final release.
Contributing
Because of the Go2 errors changes, this package is not accepting proposals for new functionality. With that said, we welcome pull requests, bug fixes and issue reports.
Before sending a PR, please discuss your change by raising an issue.
License
BSD-2-Clause
Top Related Projects
A Go (golang) package for representing a list of errors as a single error.
A comprehensive error handling library for Go
Error handling library with readable stack traces and flexible formatting support 🎆
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot