`HTTPException` is unhandled in `Flask-RESTful==0.3.6`

Author: remeikaCreated Oct 17, 2018Updated Jun 6, 2023

Hi there! It seems to me like werkzeug.exceptions.HTTPException is not handled correctly in Flask-RESTful>0.3.3

Would love to know what I am doing wrong in the following application:

python
from flask import Flask
from flask_restful import Resource, Api
from werkzeug.exceptions import HTTPException

class ApiError(HTTPException):
    def __init__(self, message=None):
        self.message = message
        self.code = 400

ERRORS = {
    'ApiError': {
        'message': "A user with that username already exists.",
        'status': 409,
    }
}

class Fail(Resource):
    def get(self):
        raise ApiError("This will fail weirdly")

app = Flask(__name__)
api = Api(app, errors=ERRORS)
api.add_resource(Fail, '/fail')

Works as expected with Flask-RESTful==0.3.3

When run in the with Flask-RESTful 0.3.3 environment, it returns the expected response:

Working requirements file:

Flask==1.0.2
Flask-RESTful==0.3.3

I receive the expected response:

$ curl http://127.0.0.1:5000/fail
{"status": 409, "message": "A user with that username already exists."}

Fails with Flask-RESTful==0.3.6

Failing requirements file:

Flask==1.0.2
Flask-RESTful==0.3.6

Unexpected response:

$ curl http://127.0.0.1:5000/fail
{"message": "Internal Server Error"}

Trackback:

[2018-10-16 23:01:02,261] ERROR in app: Exception on /fail [GET]
Traceback (most recent call last):
  File ".../lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File ".../lib/python2.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File ".../lib/python2.7/site-packages/flask_restful/__init__.py", line 273, in error_router
    return original_handler(e)
  File ".../lib/python2.7/site-packages/flask/app.py", line 1695, in handle_user_exception
    assert exc_value is e
AssertionError

Note that Flask-RESTful==0.3.6 works if I use the builtin Exception class instead of HTTPException above. Any ideas on how I could work around this to continue using HTTPException?

Thanks! James

Source: flask-restful/flask-restful