handle 405s

Handle werkzeug http errors separately. Also, improve the generic
`Exception` handler to be more flexible when exceptions don't look
like http errors.

note: because they're 405s the route isn't matched, so the v2 error
handlers don't trip properly. this might be an issue because the
internal endpoints expect a different return format. Might have to
think about how to handle this.
This commit is contained in:
Leo Hemsted
2019-07-26 13:26:20 +01:00
parent 359016de6a
commit 2c2eb2abad
2 changed files with 37 additions and 10 deletions

View File

@@ -3,7 +3,7 @@ import random
import string
import uuid
from flask import _request_ctx_stack, request, g, jsonify
from flask import _request_ctx_stack, request, g, jsonify, make_response
from flask_sqlalchemy import SQLAlchemy as _SQLAlchemy
from flask_marshmallow import Marshmallow
from flask_migrate import Migrate
@@ -12,6 +12,7 @@ from notifications_utils.clients.zendesk.zendesk_client import ZendeskClient
from notifications_utils.clients.statsd.statsd_client import StatsdClient
from notifications_utils.clients.redis.redis_client import RedisClient
from notifications_utils import logging, request_helper
from werkzeug.exceptions import HTTPException as WerkzeugHTTPException
from werkzeug.local import LocalProxy
from app.celery.celery import NotifyCelery
@@ -263,7 +264,17 @@ def init_app(app):
def exception(error):
app.logger.exception(error)
# error.code is set for our exception types.
return jsonify(result='error', message=error.message), error.code or 500
msg = getattr(error, 'message', str(error))
code = getattr(error, 'code', 500)
return jsonify(result='error', message=msg), code
@app.errorhandler(WerkzeugHTTPException)
def werkzeug_exception(e):
return make_response(
jsonify(result='error', message=e.description),
e.code,
e.get_headers()
)
@app.errorhandler(404)
def page_not_found(e):