Skip to content

Commit

Permalink
refactor: remove transformAndWrite for handling handler error
Browse files Browse the repository at this point in the history
  • Loading branch information
notjustmoney committed Nov 6, 2024
1 parent 3e99abd commit b66498a
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions huma.go
Original file line number Diff line number Diff line change
Expand Up @@ -1402,21 +1402,18 @@ func Register[I, O any](api API, op Operation, handler func(context.Context, *I)
}

status := http.StatusInternalServerError

// handle status error
var se StatusError
if errors.As(err, &se) {
status = se.GetStatus()
err = se
} else {
err = NewErrorWithContext(ctx, http.StatusInternalServerError, "unexpected error occurred", err)
writeStatusError(api, ctx, se)
return
}

ct, _ := api.Negotiate(ctx.Header("Accept"))
if ctf, ok := err.(ContentTypeFilter); ok {
ct = ctf.ContentType(ct)
if err := WriteErr(api, ctx, status, "unexpected error occurred", err); err != nil {
ctx.BodyWriter().Write([]byte("internal server error"))
panic(fmt.Errorf("failed to write error response for %s %s %d: %w", ctx.Operation().Method, ctx.Operation().Path, status, err))

Check warning on line 1415 in huma.go

View check run for this annotation

Codecov / codecov/patch

huma.go#L1413-L1415

Added lines #L1413 - L1415 were not covered by tests
}

ctx.SetHeader("Content-Type", ct)
transformAndWrite(api, ctx, status, ct, err)
return
}

Expand Down

0 comments on commit b66498a

Please sign in to comment.