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.

HTTPException handler catches non-error exceptions like RequestRedirect

See original GitHub issue

This is related to https://github.com/pallets/flask/issues/2841, but the emphasis is different.

Expected Behavior

Non-error exceptions should not be caught by registered error handlers.

app.register_error_handler(HTTPException, my_error_handler)

@app.route('/path-with-slash/')
def my_route():
    return '', 204

Attempting to visit /path-with-slash should redirect to /path-with-slash/, without invoking my_error_handler.

Actual Behavior

As the RequestRedirect raised internally in Werkzeug is a subclass of HTTPException, my_error_handler will get called. This is unexpected because no error has occurred, and the only appropriate way to handle this case is to add boilerplate to re-raise any Werkzeug RoutingExceptions.

Environment

  • Python version: 3.7.0
  • Flask version: 1.0.2
  • Werkzeug version: 0.14.1

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:14 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
taioncommented, Nov 1, 2018

I’ve added https://github.com/pallets/flask/pull/2986 as a strawman to demonstrate what the fix would look like, and updated the test to demonstrate the behavior that it enables.

0reactions
taioncommented, Nov 1, 2018

I disagree with that statement.

For example, returning flask.redirect does not in general raise a RequestRedirect, so one cannot use error handlers on RequestRedirect to implement custom request behavior in the first place.

If one wishes to customize Werkzeug’s routing logic, the correct way to handle that would be to do so in the router, rather than by using what should be the app’s error handling logic.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handling Errors - FastAPI
When raising an HTTPException , you can pass any value that can be converted to JSON as the parameter detail , not only...
Read more >
Handling Application Errors — Flask Documentation (2.2.x)
werkzeug.exceptions.HTTPException subclasses like BadRequest and their HTTP codes are interchangeable when registering handlers. ( BadRequest.code == 400 ).
Read more >
Global error handler for any exception - python - Stack Overflow
You can use @app.errorhandler(Exception) : Demo (the HTTPException check ensures that the status code is preserved): from flask import Flask, abort, ...
Read more >
Everything you wanted to know about exceptions - PowerShell
When an exception is thrown, that call stack is checked in order for an exception handler to catch it. Terminating and non-terminating errors....
Read more >
Exceptions | Sanic Framework
You can also create a catchall handler by catching Exception . ... In that case, you can subclass Sanic's default error handler 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