danielgtaylor / danielgtaylor/huma

Feature request - Ability to write errors to huma.Context during a StreamResponse

Open
#574 1 comment 1 reaction 0 assignees View on GitHub
enhancement help wanted
Dominant language
Go
Stars
4.4k
Forks
285
Avg merge
40m
Merged PRs (30d)
1

Description

When implementing a streaming API, there are some instances where processing may fail inside of the `Body` function, that could be returned as errors.

I understand that making the signature `Body: func(ctx huma.Context) (error)` instead of `Body: func(ctx huma.Context)` doesn't make much sense, because once streaming starts returning an error is not possible.

However, *before* streaming starts it's still possible to return / serialize a JSON error. Doing that requires a bit of boilerplate to set the status header / serialize the response to JSON. I think it would be useful to be able to have helpers that can take a `huma.StatusError` and write it out to the response through `huma.Context`. I'm thinking something along the lines of [huma.WriteErr](https://github.com/danielgtaylor/huma/blob/cce45695f18a6bdd3e6dd85163640098999f8144/error.go#L254-L273), but easier to use. Having access to an `api` instance in this scenario doesn't feel like the right design / usage.

Thoughts?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.