Open alephao opened 3 days ago
Originally I setup the HTTP error handling to provide a simple error type (HTTPError) but give the user the ability to provide their own error type (by conforming to HTTPResponseError).
I'm concerned adding an additional type confuses the API. Suddenly we supply two error types and it isn't overly clear why you would choose one over the other.
Adding a custom body to HTTPError might work, but I'm not sure what advantage is giving the user the chance to add a ByteBuffer to the error instead of an message string. Given the amount of additional work they would have to do to generate that ByteBuffer (for every time they create the error), they might as well just create their own error type.
In hummingbird we can throw Swift Errors to halt a request handler. There are multiple instances of this pattern in
hummingbird-examples
repo. Hummingbird providesHTTPError
as the default way to throw a valid HTTPResponse
, but the current implementation ofHTTPType
doesn't let us create a custom response body, the only possible format is to return a JSON with the structure{"message": "..."}
.The issue is that in real world projects, you'll probably want to return an HTML response, a custom JSON or something else, but to achieve this currently in hummingbird, you'll need to implement your own type conforming to
HTTPResponseError
or conformResponse
toHTTPResponseError
.So since using errors to halt execution of handlers is so common, the proposal here is for to hummingbird provide a way create a completely custom
Response
that can be thrown. Some options below:Option 1. Conform
Response
toHTTPResponseError
.The idea is to just conform
Response
toHTTPResponseError
so you can throw responses likethrow Response(status: .badRequest)
orthrow SomeCodableType().response(...)
Option 2. Use a proxy type
We could create a proxy type for
Response
and conform it toHTTPResponseError
. This provides the same flexibility of theResponse
type, but with a separation between a throwable and a non throwable type. We could initialize this type exactly the same as we would initialize aResponse
:3. Expand
HTTPError
to accept a custom bodyThis is similar to option 2, but instead of creating a new type, we could expand the HTTPError type. I believe this would require some gymnastics to prevent breaking changes on 2.x, so would have to be looked into more carefully.