Show HN: errdef – typed, structured error definitions in Go

Share This Post

Hi HN, I’m the author of errdef.

This library grew from a core idea: that by separating static error definitions (the “what”) from dynamic instances (the “why”), we could resolve the common trade-offs between context, type identity, and boilerplate.

This design allows you to create errors with context-specific, type-safe fields without losing their original identity.

Key features:

* Consistent structure for logging, metrics, and APIs

* Compile-time safety for fields (DefineField[T]).

* Clean integration with the Go ecosystem (errors.Is/As, fmt, json, and slog).

* Extensible deserialization (JSON, Protobuf, etc.)

* Built-in redaction for sensitive data (Redacted[T]).

Quick example:

  // Define once
  var ErrNotFound = errdef.Define("not_found", errdef.HTTPStatus(404))
  var UserID, _ = errdef.DefineField[string]("user_id")

  // Create instances
  err := ErrNotFound.With(ctx, UserID("u123")).New("user not found")

  // Still works with standard `errors.Is`
  errors.Is(err, ErrNotFound) // true

I’d love to get your feedback. How does this approach compare to the error handling patterns you use?


Comments URL: https://news.ycombinator.com/item?id=45563279

Points: 1

# Comments: 0

Source: github.com

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Do You Want To Boost Your Business?

drop us a line and keep in touch

We are here to help

One of our technicians will be with you shortly.