errdef enhances error handling in Go by introducing a structured approach that separates error definitions from their instances. This ensures more typed, uniform, and flexible error management while seamlessly integrating with the standard Go ecosystem. It allows for detailed formatting, marshaling, and structured logging, improving overall error visibility and maintainability.
errdef is a sophisticated error handling library for Go that introduces a definition-first approach. It enables users to separate error definitions from error instances, ensuring that errors are consistently typed, structured, and uniform. By integrating seamlessly with Go's standard library functions such as errors.Is and errors.As, as well as providing enhanced features like stack traces and detailed error composition, errdef enhances the overall error management experience in Go applications.
%+v.slog): Implement structured logging with minimal configuration, streamlining error tracking and diagnostics.Redacted wrapper.Here’s a simple example demonstrating the library’s capabilities:
package main
import (
"context"
"errors"
"fmt"
"github.com/shiwano/errdef"
)
var (
ErrNotFound = errdef.Define("not_found", errdef.HTTPStatus(404))
UserID, UserIDFrom = errdef.DefineField[string](https://github.com/shiwano/errdef/blob/main/"user_id")
)
func findUser(ctx context.Context, id string) error {
return ErrNotFound.With(ctx, UserID(id)).New("user not found")
}
func main() {
err := findUser(context.TODO(), "u123")
if errors.Is(err, ErrNotFound) {
fmt.Println("user not found")
}
if userID, ok := UserIDFrom(err); ok {
fmt.Println("user id:", userID)
}
}
errdef maintains strong performance metrics while providing thorough error handling capabilities. Benchmarks indicate that error creation and management do not become bottlenecks in typical applications, allowing developers to focus on maintaining application integrity and clarity.
Contributions to errdef are welcomed, with a focus on enhancing functionality and addressing any issues that arise. Interested parties can participate by raising issues or submitting pull requests to the project's GitHub repository.
No comments yet.
Sign in to be the first to comment.