We are getting 500 errors when a POST notification has characters that can not be decoded or the json is malformed.

The exception is wrapped and a sensible error message is returned to the client.
This commit is contained in:
Rebecca Law
2018-06-25 16:58:35 +01:00
parent e1a7d99c72
commit 13155e24dc
2 changed files with 18 additions and 3 deletions

View File

@@ -3,6 +3,7 @@ import functools
import io
import math
import werkzeug
from flask import request, jsonify, current_app, abort
from notifications_utils.pdf import pdf_page_count, PdfReadError
from notifications_utils.recipients import try_validate_and_format_phone_number
@@ -104,12 +105,17 @@ def post_precompiled_letter_notification():
@v2_notification_blueprint.route('/<notification_type>', methods=['POST'])
def post_notification(notification_type):
try:
request_json = request.get_json()
except werkzeug.exceptions.BadRequest as e:
raise BadRequestError(message=e.description, status_code=400)
if notification_type == EMAIL_TYPE:
form = validate(request.get_json(), post_email_request)
form = validate(request_json, post_email_request)
elif notification_type == SMS_TYPE:
form = validate(request.get_json(), post_sms_request)
form = validate(request_json, post_sms_request)
elif notification_type == LETTER_TYPE:
form = validate(request.get_json(), post_letter_request)
form = validate(request_json, post_letter_request)
else:
abort(404)