mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 17:31:14 -05:00
Create JobIncompleteError
This commit is contained in:
@@ -7,6 +7,22 @@ from app.authentication.auth import AuthError
|
|||||||
from app.errors import InvalidRequest
|
from app.errors import InvalidRequest
|
||||||
|
|
||||||
|
|
||||||
|
class JobIncompleteError(Exception):
|
||||||
|
def __init__(self, message):
|
||||||
|
self.message = message
|
||||||
|
|
||||||
|
def to_dict_v2(self):
|
||||||
|
return {
|
||||||
|
'status_code': 500,
|
||||||
|
"errors": [
|
||||||
|
{
|
||||||
|
"error": 'JobIncompleteError',
|
||||||
|
"message": self.message
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class TooManyRequestsError(InvalidRequest):
|
class TooManyRequestsError(InvalidRequest):
|
||||||
status_code = 429
|
status_code = 429
|
||||||
message_template = 'Exceeded send limits ({}) for today'
|
message_template = 'Exceeded send limits ({}) for today'
|
||||||
@@ -49,6 +65,10 @@ def register_errors(blueprint):
|
|||||||
current_app.logger.exception(error)
|
current_app.logger.exception(error)
|
||||||
return jsonify(json.loads(error.message)), 400
|
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(NoResultFound)
|
||||||
@blueprint.errorhandler(DataError)
|
@blueprint.errorhandler(DataError)
|
||||||
def no_result_found(e):
|
def no_result_found(e):
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ def app_for_test(mocker):
|
|||||||
import flask
|
import flask
|
||||||
from flask import Blueprint
|
from flask import Blueprint
|
||||||
from app.authentication.auth import AuthError
|
from app.authentication.auth import AuthError
|
||||||
from app.v2.errors import BadRequestError, TooManyRequestsError
|
from app.v2.errors import BadRequestError, TooManyRequestsError, JobIncompleteError
|
||||||
|
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
app.config['TESTING'] = True
|
app.config['TESTING'] = True
|
||||||
@@ -39,6 +39,10 @@ def app_for_test(mocker):
|
|||||||
def raising_data_error():
|
def raising_data_error():
|
||||||
raise DataError("There was a db problem", "params", "orig")
|
raise DataError("There was a db problem", "params", "orig")
|
||||||
|
|
||||||
|
@blue.route("raise_job_incomplete_error", methods=["GET"])
|
||||||
|
def raising_job_incomplete_error():
|
||||||
|
raise JobIncompleteError("Raising job incomplete error")
|
||||||
|
|
||||||
@blue.route("raise_exception", methods=["GET"])
|
@blue.route("raise_exception", methods=["GET"])
|
||||||
def raising_exception():
|
def raising_exception():
|
||||||
raise AssertionError("Raising any old exception")
|
raise AssertionError("Raising any old exception")
|
||||||
@@ -107,6 +111,16 @@ def test_data_errors(app_for_test):
|
|||||||
"errors": [{"error": "DataError", "message": "No result found"}]}
|
"errors": [{"error": "DataError", "message": "No result found"}]}
|
||||||
|
|
||||||
|
|
||||||
|
def test_job_incomplete_errors(app_for_test):
|
||||||
|
with app_for_test.test_request_context():
|
||||||
|
with app_for_test.test_client() as client:
|
||||||
|
response = client.get(url_for('v2_under_test.raising_job_incomplete_error'))
|
||||||
|
assert response.status_code == 500
|
||||||
|
error = json.loads(response.get_data(as_text=True))
|
||||||
|
assert error == {"status_code": 500,
|
||||||
|
"errors": [{"error": "JobIncompleteError", "message": "Raising job incomplete error"}]}
|
||||||
|
|
||||||
|
|
||||||
def test_internal_server_error_handler(app_for_test):
|
def test_internal_server_error_handler(app_for_test):
|
||||||
with app_for_test.test_request_context():
|
with app_for_test.test_request_context():
|
||||||
with app_for_test.test_client() as client:
|
with app_for_test.test_client() as client:
|
||||||
|
|||||||
Reference in New Issue
Block a user