python-flask handling application errors

user6434902

I've built a flask application and I try to catch unhandled application errors from my routes using the errorhandler decorator.

I've a main.py which looks like this,

app = Flask(__name__)
api = Api(app)

api.add_resource(Ping, '/ping')

@app.errorhandler(500)
def internal_server_error(error):
    print "caught internal server error"
    return "This page does not exist", 500

The route Ping is in another file, here is a sample version of the file

class Ping(Resource):
    def get(self):

        raise
        return {}, 200

I've placed a raise to try and reproduce a 500 Internal server error. Here is a sample error raised by my application

[2019-01-26 10:37:36,449] ERROR in app: Exception on /events/v1/monitoring/ping [GET]
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/usr/local/lib64/python2.7/site-packages/flask_restful/__init__.py", line 480, in wrapper
    resp = resource(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/flask/views.py", line 88, in view
    return self.dispatch_request(*args, **kwargs)
  File "/usr/local/lib64/python2.7/site-packages/flask_restful/__init__.py", line 595, in dispatch_request
    resp = meth(*args, **kwargs)
  File "/usr/lib/python2.7/site-packages/myapi/ping.py", line 17, in get
    raise
TypeError: exceptions must be old-style classes or derived from BaseException, not NoneType
127.0.0.1 - - [26/Jan/2019 10:37:36] "GET /ping HTTP/1.0" 500 -

For reasons I haven't been able to figure, the @app.errorhandler decorator doesn't catch any 500 error that come are raised by the application.

j2logo

I think you are using flask-restful. In order to your error handler be catched you should set the config param PROPAGATE_EXCEPTIONS to True in your config file.

Flask restful has its own error handler which differs from that of Flask. This is an extract:

[...]

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),
    }
[...]

After setting PROPAGATE_EXCEPTIONS to True you should add at least these two error handlers:

@app.errorhandler(Exception)
def internal_server_error(e):
    return jsonify({'msg': 'Internal server error'}), 500

@app.errorhandler(500)
def internal_server_error_500(e):
    return jsonify({'msg': 'Internal server error'}), 500

So, the first one will catch any exception raised at application level, even your own custom exceptions and the second one will catch any 500 error, for example raised on abort(500).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Flask handling multiple errors

From Dev

pymongo import errors in python3 flask application

From Dev

Calling Python Eve from an Flask application leads to weird errors

From Dev

Various errors in Python Flask

From Dev

Handling SheetsAPI errors - Python 2.7

From Dev

Handling errors in python with multiple tasks

From Dev

handling unicode encode errors in python

From Dev

Python Error Handling & Connection Errors

From Dev

Python flask application login

From Dev

Python Web Scraping - handling page 404 errors

From Dev

Running bash commands in python and handling errors

From Java

Python Multiprocessing: Handling Child Errors in Parent

From Dev

Handling different errors for different versions of python

From Dev

Handling errors

From Dev

Error while Handling Custom Exceptions in Flask (Python)

From Dev

Handling network errors from an external API across an application

From Dev

Python Flask - Errors after creating a new project

From Dev

Python Flask application stuck with no response

From Dev

Python Flask heroku application error

From Dev

port management in python/flask application

From Dev

Method Not Allowed with flask application in python

From Dev

Flask web application (Python 2.7)

From Dev

Running a python flask application on windows

From Dev

Else clause application in python exceptions handling

From Dev

Handling errors with gst-rtsp-server Python bindings

From Dev

Handling database connection errors / operational error exceptions in Python / Django?

From Dev

Handling Encoding Errors in a UTF-8 File with Python3

From Dev

Passing Errors Types into a Custom Error Handling Class, Python

From Dev

Viewing all the File Handling Methods and all the types of Errors in Python

Related Related

  1. 1

    Flask handling multiple errors

  2. 2

    pymongo import errors in python3 flask application

  3. 3

    Calling Python Eve from an Flask application leads to weird errors

  4. 4

    Various errors in Python Flask

  5. 5

    Handling SheetsAPI errors - Python 2.7

  6. 6

    Handling errors in python with multiple tasks

  7. 7

    handling unicode encode errors in python

  8. 8

    Python Error Handling & Connection Errors

  9. 9

    Python flask application login

  10. 10

    Python Web Scraping - handling page 404 errors

  11. 11

    Running bash commands in python and handling errors

  12. 12

    Python Multiprocessing: Handling Child Errors in Parent

  13. 13

    Handling different errors for different versions of python

  14. 14

    Handling errors

  15. 15

    Error while Handling Custom Exceptions in Flask (Python)

  16. 16

    Handling network errors from an external API across an application

  17. 17

    Python Flask - Errors after creating a new project

  18. 18

    Python Flask application stuck with no response

  19. 19

    Python Flask heroku application error

  20. 20

    port management in python/flask application

  21. 21

    Method Not Allowed with flask application in python

  22. 22

    Flask web application (Python 2.7)

  23. 23

    Running a python flask application on windows

  24. 24

    Else clause application in python exceptions handling

  25. 25

    Handling errors with gst-rtsp-server Python bindings

  26. 26

    Handling database connection errors / operational error exceptions in Python / Django?

  27. 27

    Handling Encoding Errors in a UTF-8 File with Python3

  28. 28

    Passing Errors Types into a Custom Error Handling Class, Python

  29. 29

    Viewing all the File Handling Methods and all the types of Errors in Python

HotTag

Archive