haskell-servant / haskell-servant/servant
servant-client doesn't allow HasClient instances to handle non-2xx responses
- Dominant language
- Haskell
- Stars
- 2k
- Forks
- 427
- Avg merge
- 2d 23h
- Merged PRs (30d)
- 5
Description
Hi,
When handling a response with a non-2xx status code, the `RunClient` instance currently directly throws a `ClientError` `FailureResponse`. This contains the raw response body as a ByteString, but since it's an error, it short-circuits and doesn't get parsed in the `HasClient` instance.
The same thing applies to other client libraries like `servant-http-streams` and `servant-client-ghcjs`.
This means that combinators can't specify how to handle non-2xx responses, severely limiting the usefulness of some libraries, see e.g. this [issue in `servant-checked-exceptions`](https://github.com/cdepillabout/servant-checked-exceptions/issues/27).
I know there are some related [discussions](https://github.com/haskell-servant/servant/issues/841) and [work being done](https://github.com/haskell-servant/servant/pull/969) that could move information about expected return codes to the type level, but I'm not sure what the expected timeline for that is.
Do you think it would be worth doing something simple now that allows users of existing solutions like `servant-checked-exceptions` to get access to the responses they care about and handle them as they want?
One possibility I see would be to adapt the `ClientEnv` configuration records to allow supplying the (currently hardcoded) function that decides which responses are considered successful.
I created a [simple proof of concept](https://github.com/mheinzel/servant/compare/some-administration...mheinzel:client-parametrize-accepted-status-codes) or `servant-client` and could do the same for the other client libraries here (plus adding test cases and some docs) if you are interested.
Thanks!
Contributor guide
Assessment
This issue has not been assessed yet.