Skip to content

Best Practices Error Handling #424

Answered by danielgtaylor
joa23 asked this question in Q&A
Discussion options

You must be logged in to vote

I would typically handle this at the operation handler, converting as you need after bubbling up any of the internal errors without translation. It's a bit of extra work but makes the code very easy to reason about / maintain and explicitly converts between the layers. Something like:

func MyHandler(ctx context.Context, input *MyInput) (*MyOutput, error) {
  result, err := db.Get("some query")
  if errors.Is(err, db.ErrorNotFound) {
    slog.ErrorContext(ctx, "Not found", "err", err, "id", input.ID)
    return nil, huma.Error404NotFound("Thing with ID " + input.ID + " not found")
  }

  if errors.Is(err, db.ConnectionReset) {
    slog.ErrorContext(ctx, "Can't talk to the DB", "err", err, "…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@joa23
Comment options

Answer selected by joa23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants