Skip to content
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

SoftwareEngineerChris
Copy link
Contributor

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 in HTTPResponder using HBHTTPResponseError if the errorResponseHandler 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 by HBHTTPResponseError itself at a global level.

…rrors to be intercepted and a custom response sent to the client
@codecov
Copy link

codecov bot commented Mar 15, 2021

Codecov Report

Merging #61 (3042783) into main (438ab21) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            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              
Impacted Files Coverage Δ
Sources/Hummingbird/Application.swift 92.45% <ø> (ø)
...Hummingbird/Server/Application+HTTPResponder.swift 86.11% <100.00%> (+0.81%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 754421a...3042783. Read the comment docs.

@adam-fowler
Copy link
Member

adam-fowler commented Mar 15, 2021

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 HTTPResponder.respond is for a logging. Instead of converting them to an HBHTTPResponse at the that point I can return them as an error.

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 ErrorMiddleware I setup for hummingbird-lambda https://github.com/hummingbird-project/hummingbird-lambda/blob/main/Sources/HummingbirdLambda/ErrorMiddleware.swift

@SoftwareEngineerChris
Copy link
Contributor Author

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).

@adam-fowler
Copy link
Member

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

@SoftwareEngineerChris
Copy link
Contributor Author

I think LambdaErrorMiddleware definitively points me in the right direction - thanks again!

@SoftwareEngineerChris
Copy link
Contributor Author

The middleware approach worked perfectly - didn’t even need to change any of the code, just shifted it into the middleware. Thanks again Adam.

adam-fowler added a commit that referenced this pull request Oct 17, 2023
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants