Provide a way to intercept requests and (optionally) prevent further handler(s) from being executed
See original GitHub issueThe problem I’m experiencing is pretty simple: I need to intercept/filter a request and check a global variable that tells me whether the request can be processed or if, in turn, an error should be returned straight away, by-passing any handler configured for the path in question.
Just a little bit a code to illustrate:
path("/v1", () -> {
// global "failer", toggled by end-users
before(ctx -> {
if (alwaysReturnError) {
ctx.response().sendError(INTERNAL_SERVER_ERROR_500);
}
});
path("/run", () -> {
post(ctx -> {
recordCall(RUN, ctx);
ctx.status(ACCEPTED_202).json(EMPTY_JSON_OBJECT);
});
});
The before
approach shown above does not work as expected, since the handler for /run
is executed even after ctx.response().sendError(INTERNAL_SERVER_ERROR_500);
was executed.
Is there any way to prevent (i.e. cut) the further processing of the request from within a before
method? I strongly need this feature, so either recommending how to do it or adding it to the framework would be really helpful.
Kind Regards.
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Cypress cy.intercept Problems - Gleb Bahmutov
The solution. Make sure the network intercept is registered before the application makes the call. In our case, we need to register the...
Read more >cy.intercept() not stubbing API in Cypress - Stack Overflow
Supply a response body to stub in the matching route. In cy.intercept(method, url, routeHandler?) , routeHandler is a more complicated beast.
Read more >intercept | Cypress Documentation
Spy and stub network requests and responses. Tip: We recommend you read the Network Requests guide first. All intercepts are automatically cleared before....
Read more >Request Not Intercepted By Defined Handler #1123 - GitHub
I run my jest testing with --runInBand . The setup above is manually included in every test suite. Other handlers optionally added appear...
Read more >A Spring Handler Interceptor to Manage Sessions - Baeldung
This tutorial shows how to intercept web requests using Spring MVC's HandlerInterceptor in order to manually do session management/timeout. As ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks, I’ve added
and
Javalin has a
HaltException
which is handled before other exceptions. It can be used to short-circuit the request lifecycle. If you throw aHaltException
in abefore
-handler, noendpoint
-handler will fire.Silly me. You’re right. Perhaps mentioning the word “filter” and/or “interceptor” might help people find
before
andafter
easier.Regarding
HaltException
it only states “which is handled before other exceptions”. Perhaps adding something more like “it can be used to prevent any other handler from processing the request” might help.Actually, the combination of
before
andHaltException
seems to be Javalin’s way to implement something close to a web filter, perhaps it makes sense to add a section for that?