Невозможно связать исключения в Python - PullRequest
0 голосов
/ 01 мая 2020

То, что я пытаюсь сделать, - это цепочка моих исключений, поэтому, когда Flask werkzeug.exceptions.HTTPException повышается, оно вызывает мое пользовательское исключение exception.api.Error.

Однако я продолжаю получать следующую ошибку, и я Я не уверен, как это исправить. Есть предложения?

from werkzeug.exceptions import HTTPException
import exception.api as exception

app = Flask(__name__)

@app.errorhandler(exception.Error)
def ExceptionAPI(e):
    r = jsonify( e.response )
    r.status_code = e.status
    return r

@app.errorhandler(HTTPException)
def HTTPException(e):
    raise exception.Error(e.code)

Файл: исключение / api.py

from exception.message import Message

class Error(Exception, Message):
    message = None
    status = None
    response = None

    def __init__(self, message=None, status=None):
        Exception.__init__(self)
        if message is not None:
            m = self.getMessage(message)
            self.response = {'e': m['public']}
            if not status:
                self.status = m['status']
            else:
                self.status = status
        else:
            if status is not None:
                m = self.getStatus(status)
                self.response = {'e': m['public']}
                self.status = status
            else:
                logging.error('else')
                m = self.getStatus(500)
                self.response = {'e': m['public']}
                self.status = m['status']

Ошибка:

 gunicorn[8235]: [2020-04-30 22:24:12 +0000] [8262] [ERROR] Error handling request /api/hello
 gunicorn[8235]: Traceback (most recent call last):
 gunicorn[8235]:   File "/env/lib/python3.8/site-packages/flask/app.py", line 1949, in full_dispatch_request
 gunicorn[8235]:     rv = self.dispatch_request()
 gunicorn[8235]:   File "/env/lib/python3.8/site-packages/flask/app.py", line 1925, in dispatch_request
 gunicorn[8235]:     self.raise_routing_exception(req)
 gunicorn[8235]:   File "/env/lib/python3.8/site-packages/flask/app.py", line 1907, in raise_routing_exception
 gunicorn[8235]:     raise request.routing_exception
 gunicorn[8235]:   File "/env/lib/python3.8/site-packages/flask/ctx.py", line 350, in match_request
 gunicorn[8235]:     result = self.url_adapter.match(return_rule=True)
 gunicorn[8235]:   File "/env/lib/python3.8/site-packages/werkzeug/routing.py", line 1940, in match
 gunicorn[8235]:     raise MethodNotAllowed(valid_methods=list(have_match_for))
 gunicorn[8235]: werkzeug.exceptions.MethodNotAllowed: 405 Method Not Allowed: The method is not allowed for the requested URL.
 gunicorn[8235]: During handling of the above exception, another exception occurred:
 gunicorn[8235]: Traceback (most recent call last):
 gunicorn[8235]:   File "/env/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 134, in handle
 gunicorn[8235]:     self.handle_request(listener, req, client, addr)
 gunicorn[8235]:   File "/env/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 175, in handle_request
 gunicorn[8235]:     respiter = self.wsgi(environ, resp.start_response)
 gunicorn[8235]:   File "/env/lib/python3.8/site-packages/flask/app.py", line 2463, in __call__
 gunicorn[8235]:     return self.wsgi_app(environ, start_response)
 gunicorn[8235]:   File "/env/lib/python3.8/site-packages/flask/app.py", line 2449, in wsgi_app
 gunicorn[8235]:     response = self.handle_exception(e)
 gunicorn[8235]:   File "/env/lib/python3.8/site-packages/flask/app.py", line 1866, in handle_exception
 gunicorn[8235]:     reraise(exc_type, exc_value, tb)
 gunicorn[8235]:   File "/env/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
 gunicorn[8235]:     raise value
 gunicorn[8235]:   File "/env/lib/python3.8/site-packages/flask/app.py", line 2446, in wsgi_app
 gunicorn[8235]:     response = self.full_dispatch_request()
 gunicorn[8235]:   File "/env/lib/python3.8/site-packages/flask/app.py", line 1951, in full_dispatch_request
 gunicorn[8235]:     rv = self.handle_user_exception(e)
 gunicorn[8235]:   File "/env/lib/python3.8/site-packages/flask/app.py", line 1815, in handle_user_exception
 gunicorn[8235]:     return self.handle_http_exception(e)
 gunicorn[8235]:   File "/env/lib/python3.8/site-packages/flask/app.py", line 1743, in handle_http_exception
 gunicorn[8235]:     return handler(e)
 gunicorn[8235]:   File "/erp/endpoint.py", line 28, in HTTPException
 gunicorn[8235]:     raise exception.Error(404)
 gunicorn[8235]: exception.api.Error

Я пробовал: raise exception.Error(e.code) from e также приводит к ошибке

...