zio / zio-http

A next-generation Scala framework for building scalable, correct, and efficient HTTP clients and servers
https://zio.dev/zio-http
Apache License 2.0
800 stars 403 forks source link

Customize `HttpCodec` errors with `Endpoint` api #3138

Closed anqit closed 2 months ago

anqit commented 2 months ago

Is your feature request related to a problem? Please describe. I'd like hooks in the Endpoint API to be able to customize error responses resulting for codec failures, rather than having to rely on the default response.

Describe the solution you'd like With an endpoint defined as, for example, Endpoint(..route...).in[User]..., if the request doesn't provide a parseable User, I'd like to be able to customize the response returned in this case easily via a method provided by the Endpoint's fluent API.

Describe alternatives you've considered I tried creating a custom codec mapping the ZIOs returned by HttpContentCodec, but decodeRequest/Response return a Task, so there's not much room to customize the error there.

987Nabil commented 2 months ago

outCodecError let you set a codec to transform the thrown errors into custom responses. The default implementation is here

If what this interface does not offer what you need/want, please open another issue with your exact requirements. Docs on this will follow soon 🙂

anqit commented 2 months ago

Is there an example of how to use this for something like the case I mentioned above? Having some trouble grokking targeting specific errors with outCodecError

987Nabil commented 2 months ago

Unparseable inputs will result in a HttpCodecError.MalformedBody