Top Related Projects
A Go (golang) package for representing a list of errors as a single error.
Error handling library with readable stack traces and flexible formatting support 🎆
Quick Overview
The go-errors/errors package is an enhanced error handling library for Go. It provides a drop-in replacement for the standard errors package, offering additional functionality such as stack traces, error wrapping, and custom error types. This library aims to improve error debugging and handling in Go applications.
Pros
- Seamless integration with existing Go code as a drop-in replacement
- Provides stack traces for easier debugging
- Supports error wrapping and unwrapping
- Offers custom error types for more detailed error information
Cons
- May introduce a slight performance overhead compared to the standard errors package
- Requires developers to adapt to new error handling patterns
- Could potentially increase the size of compiled binaries
Code Examples
Creating a new error with stack trace:
import "github.com/go-errors/errors"
err := errors.New("something went wrong")
fmt.Println(err.(*errors.Error).ErrorStack())
Wrapping an existing error:
import "github.com/go-errors/errors"
originalErr := someFunction()
wrappedErr := errors.Wrap(originalErr, "additional context")
Creating a custom error type:
import "github.com/go-errors/errors"
type MyCustomError struct {
*errors.Error
Code int
}
func NewMyCustomError(message string, code int) *MyCustomError {
return &MyCustomError{
Error: errors.New(message),
Code: code,
}
}
Getting Started
To start using go-errors/errors in your Go project:
-
Install the package:
go get github.com/go-errors/errors -
Import the package in your Go code:
import "github.com/go-errors/errors" -
Replace standard error creation with go-errors/errors:
// Instead of: // err := errors.New("something went wrong") // Use: err := errors.New("something went wrong") -
Utilize additional features like stack traces and error wrapping as needed in your error handling logic.
Competitor Comparisons
A Go (golang) package for representing a list of errors as a single error.
Pros of go-multierror
- Allows aggregating multiple errors into a single error type
- Provides methods for error formatting and custom error handling
- Supports concurrent error collection with goroutines
Cons of go-multierror
- More complex API compared to errors
- Requires additional dependency in projects
- May introduce overhead for simple error handling scenarios
Code Comparison
errors:
err := errors.New("operation failed")
err = errors.Wrap(err, "additional context")
go-multierror:
var result error
multierror.Append(result, errors.New("error 1"))
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.
- API complexity: go-multierror offers more features but has a more complex API, whereas errors provides a simpler interface for basic error handling.
- Use cases: go-multierror is better suited for scenarios involving multiple potential errors, while errors excels in adding context to individual errors.
Conclusion
Choose go-multierror when dealing with multiple errors that need to be aggregated, especially in concurrent scenarios. Opt for errors when simplicity and error context are the primary concerns. Consider the specific requirements of your project to determine which library best fits your needs.
Error handling library with readable stack traces and flexible formatting support 🎆
Pros of eris
- Provides more comprehensive error wrapping and formatting options
- Includes stack trace capture and printing functionality
- Offers additional utility functions for error handling and manipulation
Cons of eris
- Slightly more complex API compared to errors
- May have a small performance overhead due to additional features
Code Comparison
errors:
err := errors.New("original error")
wrappedErr := errors.Wrap(err, "wrapped error")
fmt.Println(wrappedErr)
eris:
err := eris.New("original error")
wrappedErr := eris.Wrap(err, "wrapped error")
fmt.Println(eris.ToString(wrappedErr, true))
Both libraries provide error wrapping functionality, but eris offers more advanced formatting options and stack trace information. The eris library also includes additional utility functions for error handling, such as root cause extraction and error type comparison.
While eris provides more features, it may have a slightly steeper learning curve compared to the simpler errors package. However, for projects requiring more sophisticated error handling and debugging capabilities, eris offers significant advantages.
The choice between these libraries depends on the specific needs of your project, with eris being more suitable for complex applications that require detailed error information and advanced error handling techniques.
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
go-errors/errors
Package errors adds stacktrace support to errors in go.
This is particularly useful when you want to understand the state of execution when an error was returned unexpectedly.
It provides the type *Error which implements the standard golang error interface, so you can use this library interchangeably with code that is expecting a normal error return.
Usage
Full documentation is available on godoc, but here's a simple example:
package crashy
import "github.com/go-errors/errors"
var Crashed = errors.Errorf("oh dear")
func Crash() error {
return errors.New(Crashed)
}
This can be called as follows:
package main
import (
"crashy"
"fmt"
"github.com/go-errors/errors"
)
func main() {
err := crashy.Crash()
if err != nil {
if errors.Is(err, crashy.Crashed) {
fmt.Println(err.(*errors.Error).ErrorStack())
} else {
panic(err)
}
}
}
Meta-fu
This package was original written to allow reporting to Bugsnag from bugsnag-go, but after I found similar packages by Facebook and Dropbox, it was moved to one canonical location so everyone can benefit.
This package is licensed under the MIT license, see LICENSE.MIT for details.
Changelog
- v1.1.0 updated to use go1.13's standard-library errors.Is method instead of == in errors.Is
- v1.2.0 added
errors.Asfrom the standard library. - v1.3.0 BREAKING updated error methods to return
errorinstead of*Error.
Code that needs access to the underlying
*Errorcan use the new errors.AsError(e)// before errors.New(err).ErrorStack() // after . errors.AsError(errors.Wrap(err)).ErrorStack()
- v1.4.0 BREAKING v1.4.0 reverted all changes from v1.3.0 and is identical to v1.2.0
- v1.4.1 no code change, but now without an unnecessary cover.out file.
- v1.4.2 performance improvement to ErrorStack() to avoid unnecessary work https://github.com/go-errors/errors/pull/40
- v1.5.0 add errors.Join() and errors.Unwrap() copying the stdlib https://github.com/go-errors/errors/pull/40
- v1.5.1 fix build on go1.13..go1.19 (broken by adding Join and Unwrap with wrong build constraints)
Top Related Projects
A Go (golang) package for representing a list of errors as a single error.
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