New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds 'errorResponseHandler' to HBApplication to allow HBHTTPResponseErrors to be intercepted and a custom response returned #61
Conversation
…rrors to be intercepted and a custom response sent to the client
Codecov Report
@@ Coverage Diff @@
## main #61 +/- ##
==========================================
+ Coverage 83.17% 83.19% +0.01%
==========================================
Files 47 47
Lines 2372 2374 +2
==========================================
+ Hits 1973 1975 +2
Misses 399 399
Continue to review full report at Codecov.
|
Hi can I ask how you are using Hummingbird. Are you using hummingbird-core or are you just using the router elements of Hummingbird? If it is the latter I have a minor change which will resolve this for you. The only reason I catch errors in If it is the former you could use a Middleware to catch all the errors and process them eg struct ErrorMiddleware: HBMiddleware {
public func apply(to request: HBRequest, next: HBResponder) -> EventLoopFuture<HBResponse> {
return next.respond(to: request).flatMapErrorThrowing { error in
try processError(error)
}
}
} See the |
Thanks Adam, I think the middleware might do the trick. I’m effectively using Hummingbird to proxy requests elsewhere that aren’t matched by the router (whilst still having some paths matched and handled by the router). |
Make sure it is the first middleware you add to ensure you catch errors from any other middleware |
I think LambdaErrorMiddleware definitively points me in the right direction - thanks again! |
The middleware approach worked perfectly - didn’t even need to change any of the code, just shifted it into the middleware. Thanks again Adam. |
* Fix crash when generating attributes for streaming body (#232) * Make AsyncSequenceResponseBodyStreamer public * Update CORSMiddleware.swift (#237) * Fix SVGs in Safari (#238) * Add Swift 5.9, Remove Swift 5.6 (#242) * Rename EventLoopGroupProvider.global to .singleton To keep in line with HB v1.x * Call writeAndFlush for streamed body chunks (#61) * Update swift-nio-http2 version See https://forums.swift.org/t/swift-nio-http2-security-update-cve-2023-44487-http-2-dos/67764 --------- Co-authored-by: Fejziu Bajram <31960279+beldis@users.noreply.github.com> Co-authored-by: Jason Morley <hello@jbmorley.co.uk>
As promised, this is the second PR. It introduces the ability to handle a
HBHTTPResponseError
and provide a custom response, falling back to the default implementation inHTTPResponder
usingHBHTTPResponseError
if theerrorResponseHandler
is not provided.The reason for this PR is that I wanted to continue using standard
HBHTTPErrors
e.g.HBHTTPError(.notFound)
at the higher-levels of the application, but have the ability to create/manipulate a custom response in-place of the ones provided byHBHTTPResponseError
itself at a global level.