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.

Integration with flask-restful & error handling

See original GitHub issue

Similar to #86, errors like SignatureExpiry are returning 500 rather than 4xx errors.

However I am using flask-restful which does not have an error_handler, so the solutions in the referenced issue do not apply.

Flask-restful is briefly mentioned on the releases page here , but setting application.config['PROPAGATE_EXCEPTIONS'] = True didn’t have any effect for me.

Any insight into how I can correctly deal with error handling using flask-restful?

Issue Analytics

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

github_iconTop GitHub Comments

28reactions
cesarmarroquincommented, May 8, 2018

I also encountered this issue. I could not replicate it at all in development, but was getting 500s instead of 401s in production and staging.

I was able to reproduce it by setting the debug setting to false in development mode, and the 500s showed up.

I was able to fix it by setting application.config['PROPAGATE_EXCEPTIONS'] = True

8reactions
alexander-dingcommented, Dec 29, 2019

@paurakhsharma

I think I found a workaround to integrate custom errors. Here’s my current setup: Set PROPAGATE_EXCEPTIONS = False Then overwrite the error handler flask_restful.Api object as such:

from flask import Blueprint
from flask_restful import Api as _Api

# some custom errors
class RoomDoesNotExist(Exception):
    pass

CUSTOM_ERRORS = {
'RoomDoesNotExist': {
        'message': "A room by that name does not exist.",
        'status': 404,
    },
}

class Api(_Api):
    def error_router(self, original_handler, e):
        """ Override original error_router to only custom errors and parsing error (from webargs)"""
        error_type = type(e).__name__.split(".")[-1] # extract the error class name as a string
        # if error can be handled by flask_restful's Api object, do so
        # otherwise, let Flask handle the error
        # the 'UnprocessableEntity' is included only because I'm also using webargs
        # feel free to omit it
        if self._has_fr_route() and error_type in list(CUSTOM_ERRORS) + ['UnprocessableEntity']:
            try:
                return self.handle_error(e)
            except Exception:
                pass  # Fall through to original handler

        return original_handler(e)

api_bp = Blueprint('api', __name__)
api = Api(api_bp, errors=CUSTOM_ERRORS)

This way, flask_restful’s errors are handled by flask_restful’s Api object, and other errors fall through to Flask’s error handler.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Flask-restful - Custom error handling - Stack Overflow
Flask -RESTful will call the handle_error() function on any 400 or 500 error that happens on a Flask-RESTful route, and leave other routes...
Read more >
Handling Application Errors — Flask Documentation (2.2.x)
When an error occurs in Flask, an appropriate HTTP status code will be returned. 400-499 indicate errors with the client's request data, or...
Read more >
How To Handle Errors in a Flask Application - DigitalOcean
Step 1 — Using The Flask Debugger · Step 2 — Creating Custom Error Pages · Step 3 — Using Logging to Track...
Read more >
Flask Rest API -Part:4- Exception Handling
So, to solve such issues we are going to use Exception Handling to catch such exceptions and send a proper error message to...
Read more >
Developing RESTful APIs with Python and Flask - Auth0
We'll update the code soon with latest versions of Flask and Python. To answer this again the error you encountered was because of...
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 Hashnode Post

No results found