haskell-servant / haskell-servant/servant
Returning a header regardless of error
- Dominant language
- Haskell
- Stars
- 2k
- Forks
- 427
- Avg merge
- 2d 23h
- Merged PRs (30d)
- 5
Description
I would like to have an endpoint that sets a header both when it returns successfully and when it fails with an error. As far as I can tell, currently there is no way to do this _easily_.
In particular:
1. `Headers` are attached to the endpoint’s return value type at type-level, so there is no way to assert that an endpoint sets a header on error.
2. Headers are attached to the return value at value-level, so there is just no way to set a header when failing.
I tried to come up with some kind of combinator that would do this, but I couldn’t: it all boils down to the fact that combinators are polymorphic over the underlying monad, so there is no way for them to do anything with the error in the error case.
What I was thinking instead is a custom handler monad, that will essentially be `ExceptT (headers, ServerError) IO (headers, a)` and then hoist it to `Handler a` by prepending the headers to the error or... and then I’m not sure how to add the headers to the normal response. And, also, this way the headers set will need to be the same for _all_ endpoints, which is not great.
Ultimately, I would be ok with sacrificing 1 and not indicating in the type that a header is set, but 2, together with my previous paragraph, seems to be a big roadblock.
So, I guess, I’m looking for ideas of how this can be achieved and, ideally, eventually upstreamed for the future users.
Contributor guide
Assessment
This issue has not been assessed yet.