Files
notifications-api/app/v2/errors.py
Rebecca Law fc298367c5 Updated test_validators to test the contents of the error messages.
Added some tests to the test_post_notifications.
Added a errorhandler for AuthErrors.

This endpoint is not being used anywhere, however there is some common code being used in the v1 post endpoint. The only thing that may be affected is the error response, hopefully they are the same.
2016-10-31 12:22:26 +00:00

55 lines
1.7 KiB
Python

from flask import jsonify, current_app
from sqlalchemy.exc import SQLAlchemyError, DataError
from sqlalchemy.orm.exc import NoResultFound
from app.authentication.auth import AuthError
from app.errors import InvalidRequest
class TooManyRequestsError(InvalidRequest):
status_code = 429
# code and link will be in a static file
code = "10429"
link = "link to docs"
message_template = 'Exceeded send limits ({}) for today'
def __init__(self, sending_limit):
self.message = self.message_template.format(sending_limit)
class BadRequestError(InvalidRequest):
status_code = 400
code = 10400
link = "link to documentation"
message = "An error occurred"
def __init__(self, fields=[], message=None):
self.fields = fields
self.message = message if message else self.message
def register_errors(blueprint):
@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(NoResultFound)
@blueprint.errorhandler(DataError)
def no_result_found(e):
current_app.logger.exception(e)
return jsonify(message="No result found"), 404
@blueprint.errorhandler(AuthError)
def auth_error(error):
return jsonify(status_code=error.code,
message=error.message,
code=error.code,
link='link to docs'), error.code
@blueprint.errorhandler(Exception)
def internal_server_error(error):
current_app.logger.exception(error)
return jsonify(message='Internal server error'), 500