Skip to content

~/labs/errx

errx

A transport-agnostic error taxonomy for services: your domain returns a kind, your transport decides what it means.

A transport-agnostic error taxonomy for Go services. Your domain and application layers return *errx.Error with a Kind; your transport layer decides what that means on the wire.

errx itself knows nothing about HTTP handlers, frameworks or response envelopes, and has no dependencies outside the standard library.

Install

go get github.com/oshturhq/errx

Features

  • A fixed set of stable error kinds, each mapped to an HTTP status
  • Constructors for every kind: client kinds take a message, server kinds wrap the underlying error
  • Is, KindOf, StatusOf, From and IsRetryable, working through fmt.Errorf("%w") chains
  • Implements Unwrap, so errors.Is and errors.As keep working
  • IsClient, IsServer and IsRetryable to drive retry policies and requeue decisions
  • Kinds are stable lower snake case strings, usable as machine-readable error codes

Example

func (s *UserService) Get(ctx context.Context, id string) (*User, error) {
    user, err := s.users.ByID(ctx, id)
    if err != nil {
        return nil, errx.NotFound("user not found")
    }

    return user, nil
}