mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-18 03:41:43 -05:00
The admin app validation did not catch this problem. But the API did. This PR is a small fix to catch the erorr thrown by the notifications-utils/recipient validation methods and return a 400 status rather than a 500. This only solves the issue of the user seeing "We are experiencing technical difficulties" rather than "invalid email address" The bug can be replicated if you enter use quotes when entering the email address. More work needs to be done so that the admin app does the same validation as the api so the user sees a nice form validtion error rather than a 400 after clicking send. See: https://www.pivotaltracker.com/story/show/154472625
118 lines
3.8 KiB
Python
118 lines
3.8 KiB
Python
import json
|
|
|
|
from flask import jsonify, current_app, request
|
|
from jsonschema import ValidationError
|
|
from notifications_utils.recipients import InvalidEmailError
|
|
from sqlalchemy.exc import DataError
|
|
from sqlalchemy.orm.exc import NoResultFound
|
|
|
|
from app.authentication.auth import AuthError
|
|
from app.errors import InvalidRequest
|
|
|
|
|
|
class JobIncompleteError(Exception):
|
|
def __init__(self, message):
|
|
self.message = message
|
|
self.status_code = 500
|
|
|
|
def to_dict_v2(self):
|
|
return {
|
|
'status_code': self.status_code,
|
|
"errors": [
|
|
{
|
|
"error": 'JobIncompleteError',
|
|
"message": self.message
|
|
}
|
|
]
|
|
}
|
|
|
|
|
|
class NoAckFileReceived(Exception):
|
|
def __init__(self, message):
|
|
self.message = message
|
|
self.status_code = 500
|
|
|
|
def to_dict_v2(self):
|
|
return {
|
|
'status_code': self.status_code,
|
|
"errors": [
|
|
{
|
|
"error": 'NoAckFileReceived',
|
|
"message": str(self.message)
|
|
}
|
|
]
|
|
}
|
|
|
|
|
|
class TooManyRequestsError(InvalidRequest):
|
|
status_code = 429
|
|
message_template = 'Exceeded send limits ({}) for today'
|
|
|
|
def __init__(self, sending_limit):
|
|
self.message = self.message_template.format(sending_limit)
|
|
|
|
|
|
class RateLimitError(InvalidRequest):
|
|
status_code = 429
|
|
message_template = 'Exceeded rate limit for key type {} of {} requests per {} seconds'
|
|
|
|
def __init__(self, sending_limit, interval, key_type):
|
|
# normal keys are spoken of as "live" in the documentation
|
|
# so using this in the error messaging
|
|
if key_type == 'normal':
|
|
key_type = 'live'
|
|
|
|
self.message = self.message_template.format(key_type.upper(), sending_limit, interval)
|
|
|
|
|
|
class BadRequestError(InvalidRequest):
|
|
message = "An error occurred"
|
|
|
|
def __init__(self, fields=[], message=None, status_code=400):
|
|
self.status_code = status_code
|
|
self.fields = fields
|
|
self.message = message if message else self.message
|
|
|
|
|
|
def register_errors(blueprint):
|
|
@blueprint.errorhandler(InvalidEmailError)
|
|
def invalid_format(error):
|
|
# Please not that InvalidEmailError is re-raised for InvalidEmail or InvalidPhone,
|
|
# work should be done in the utils app to tidy up these errors.
|
|
current_app.logger.exception(error)
|
|
return jsonify(status_code=400,
|
|
errors=[{"error": error.__class__.__name__, "message": str(error)}]), 400
|
|
|
|
@blueprint.errorhandler(InvalidRequest)
|
|
def invalid_data(error):
|
|
current_app.logger.error(error)
|
|
response = jsonify(error.to_dict_v2()), error.status_code
|
|
return response
|
|
|
|
@blueprint.errorhandler(ValidationError)
|
|
def validation_error(error):
|
|
current_app.logger.exception(error)
|
|
return jsonify(json.loads(error.message)), 400
|
|
|
|
@blueprint.errorhandler(JobIncompleteError)
|
|
def job_incomplete_error(error):
|
|
return jsonify(error.to_dict_v2()), 500
|
|
|
|
@blueprint.errorhandler(NoResultFound)
|
|
@blueprint.errorhandler(DataError)
|
|
def no_result_found(e):
|
|
current_app.logger.exception(e)
|
|
return jsonify(status_code=404,
|
|
errors=[{"error": e.__class__.__name__, "message": "No result found"}]), 404
|
|
|
|
@blueprint.errorhandler(AuthError)
|
|
def auth_error(error):
|
|
current_app.logger.info('API AuthError, client: {} error: {}'.format(request.headers.get('User-Agent'), error))
|
|
return jsonify(error.to_dict_v2()), error.code
|
|
|
|
@blueprint.errorhandler(Exception)
|
|
def internal_server_error(error):
|
|
current_app.logger.exception(error)
|
|
return jsonify(status_code=500,
|
|
errors=[{"error": error.__class__.__name__, "message": 'Internal server error'}]), 500
|