question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide a way to intercept requests and (optionally) prevent further handler(s) from being executed

See original GitHub issue

The 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:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tipsycommented, Feb 24, 2018

Thanks, I’ve added

  • You might know before-handlers as filters, interceptors, or middleware from other libraries.
  • You might know endpoint-handlers as routes or middleware from other libraries.
  • You might know after-handlers as filters, interceptors, or middleware from other libraries.

and

Javalin has a HaltException which is handled before other exceptions. It can be used to short-circuit the request lifecycle. If you throw a HaltException in a before-handler, no endpoint-handler will fire.

0reactions
ClintEsteMaderacommented, Feb 24, 2018

Silly me. You’re right. Perhaps mentioning the word “filter” and/or “interceptor” might help people find before and after 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 and HaltException seems to be Javalin’s way to implement something close to a web filter, perhaps it makes sense to add a section for that?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found