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.

Different error responses based on DEBUG flag

See original GitHub issue

Not sure if this is a bug or a gap in my understanding (I am very new to Flask, and Python in general).

Since I felt constrained by the dict-based error handling in Flask-Restful, I used the ‘errorhandler’ decorator provided by Flask. The issue is, the errorhandlers work fine when DEBUG=True (and show custom error responses that I have set up), but show a cryptic {'message': 'Internal Server Error'} when DEBUG=False.

After a lot of struggle, I figured out that it was due to 2 reasons:

  1. The PROPAGATE_ERRORS flag being set and unset on DEBUG=True/False.
  2. This snippet of code in the flask_restful/__init__.py file:
        if not isinstance(e, HTTPException) and current_app.propagate_exceptions:
            exc_type, exc_value, tb = sys.exc_info()
            if exc_value is e:
                raise
            else:
                raise e

        headers = Headers()
        if isinstance(e, HTTPException):
            code = e.code
            default_data = {
                'message': getattr(e, 'description', http_status_message(code))
            }
            headers = e.get_response().headers
        else:
            code = 500
            default_data = {
                'message': http_status_message(code),
            }

which raises the exception and lets the flask errorhandler deal with it if propagate_exceptions is True, but handles the exception by itself if otherwise.

I have 2 suggestions:

  1. You should mention in the documentation that Flask-Restful overrides the default Flask error handlers.
  2. Since you are overriding the error handlers, should you even care whether propagate_exceptions is True or False?

It looks weird to me that flask_restful handles error responses differently based on the DEBUG flag. Is there a way I can disable FR error handling? Is there a flexible error handing option in FR so that I can avoid the Flask errorhandler ?

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:6
  • Comments:8

github_iconTop GitHub Comments

9reactions
IngoMeyer441commented, Mar 15, 2018

If debug mode is disabled, the PROPAGATE_EXCEPTIONS setting can still be set to True explicitly:

app.config['PROPAGATE_EXCEPTIONS'] = True
7reactions
ramshaw888commented, Jul 2, 2018

The workaround in #280 also works, but does so by completely bypassing flask_restful error handling which I think is a bit of a blunt solution.

Setting PROPAGATE_EXCEPTIONS = True also works, but is a bad solution. When flask_restful is initialised with a flask app, it hijacks Flask’s native error handler functions - app.handle_user_exception and app.handle_exception.

When PROPAGATE_EXCEPTIONS = True, flask_restful will forego it’s own error handler, and fall back to these original flask app.handle_user_exception and app.handle_exception implementations. This means you can either use flask errorhandlers OR flask_restful’s error handler (you might want both).

What these original Flask error handler functions do is check to see if there is a defined custom handler for that given error (a function decorated by @app.errorhandler) and let that function handle the error - which is what we want. Hence why setting PROPAGATE_EXCEPTIONS = True makes @app.errorhandler functions work.

The problem with this is that Flask also does something with PROPAGATE_EXCEPTIONS. In Flask when this is True and there is no defined handler for the error, it will reraise the exception instead of returning a stock werkzeug.exceptions.InternalServerError when the flag is False. This is bad because this single flag does 2 distinct but deceptively similar things, and there is no way to do one, and not the other.

I propose:

  • The flask_restful error handler should check if there is a flask error handler for the given error and use that.
  • Additionally if we actually want to forego the flask_restful error handler, flask_restful should do so using a different config flag value. Something like USE_FLASK_ERROR_HANDLER perhaps.

Thoughts? Happy to open a PR with these changes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

When to use the different log levels - Stack Overflow
I'd recommend adopting Syslog severity levels: DEBUG, INFO, NOTICE, WARNING, ERROR, CRITICAL, ...
Read more >
Exceptions and debugging - Advanced R. - Hadley Wickham
I often use messages to let the user know what value the function has chosen for an important missing argument. Conditions are usually...
Read more >
Error handling - Apollo GraphQL Docs
Apollo Server's variety of error codes enables requesting clients to respond differently to different error types. You can also create your own custom ......
Read more >
Web API Error Handling: How To Make Debugging Easier
In addition to differentiating between client and server errors, as well as the different status codes, we can also classify errors as either ......
Read more >
Designing Error Messages and a Logging Strategy in Node.js
Both need very different types of error messages. ... rotated when it reaches 100Mb of size and display debug-level logs on the terminal....
Read more >

github_iconTop Related Medium Post

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