Use status_code in error response.

Remove code.
This commit is contained in:
Rebecca Law
2016-11-02 14:58:39 +00:00
parent db91a87fb2
commit 4cb38e2d12
7 changed files with 13 additions and 20 deletions

View File

@@ -16,10 +16,11 @@ class AuthError(Exception):
self.code = code
def to_dict_v2(self):
return {'code': self.code,
return {
'status_code': self.code,
'message': self.short_message,
'fields': self.message,
'link': 'link to docs'}
'fields': self.message
}
def get_auth_token(req):

View File

@@ -25,7 +25,7 @@ class InvalidRequest(Exception):
Version 2 of the public api error response.
'''
return {
"code": self.code,
"status_code": self.code,
"message": self.message,
"fields": self.fields
}

View File

@@ -19,7 +19,7 @@ def build_error_message(errors, schema):
field = {s[1]: s[2].strip()}
fields.append(field)
message = {
"code": "1001",
"status_code": 400,
"message": "Validation error occurred - {}".format(schema['title']),
"fields": fields
}

View File

@@ -11,7 +11,6 @@ from app.errors import InvalidRequest
class TooManyRequestsError(InvalidRequest):
status_code = 429
code = "10429"
message_template = 'Exceeded send limits ({}) for today'
def __init__(self, sending_limit):
@@ -20,7 +19,6 @@ class TooManyRequestsError(InvalidRequest):
class BadRequestError(InvalidRequest):
status_code = 400
code = 10400
message = "An error occurred"
def __init__(self, fields=[], message=None):

View File

@@ -28,7 +28,6 @@ def test_check_service_message_limit_over_message_limit_fails(key_type, notify_d
with pytest.raises(TooManyRequestsError) as e:
check_service_message_limit(key_type, service)
assert e.value.status_code == 429
assert e.value.code == '10429'
assert e.value.message == 'Exceeded send limits (4) for today'
assert e.value.fields == []
@@ -49,7 +48,7 @@ def test_check_template_is_for_notification_type_fails_when_template_type_does_n
with pytest.raises(BadRequestError) as e:
check_template_is_for_notification_type(notification_type=notification_type,
template_type=template_type)
assert e.value.code == 10400
e.value.status_code == 400
error_message = '{0} template is not suitable for {1} notification'.format(template_type, notification_type)
assert e.value.message == error_message
assert e.value.fields == [{'template': error_message}]
@@ -66,7 +65,6 @@ def test_check_template_is_active_fails(sample_template):
with pytest.raises(BadRequestError) as e:
check_template_is_active(sample_template)
assert e.value.status_code == 400
assert e.value.code == 10400
assert e.value.message == 'Template has been deleted'
assert e.value.fields == [{'template': 'Template has been deleted'}]
@@ -120,7 +118,6 @@ def test_service_can_send_to_recipient_fails_when_recipient_is_not_on_team(recip
key_type,
trial_mode_service)
assert exec_info.value.status_code == 400
assert exec_info.value.code == 10400
assert exec_info.value.message == error_message
assert exec_info.value.fields == []
@@ -132,7 +129,6 @@ def test_service_can_send_to_recipient_fails_when_mobile_number_is_not_on_team(n
'team',
live_service)
assert e.value.status_code == 400
assert e.value.code == 10400
assert e.value.message == 'Cant send to this recipient using a team-only API key'
assert e.value.fields == []
@@ -147,7 +143,6 @@ def test_check_sms_content_char_count_fails(char_count, notify_api):
with pytest.raises(BadRequestError) as e:
check_sms_content_char_count(char_count)
assert e.value.status_code == 400
assert e.value.code == 10400
assert e.value.message == 'Content for template has a character count greater than the limit of {}'.format(
notify_api.config['SMS_CHAR_COUNT_LIMIT'])
assert e.value.fields == []

View File

@@ -32,7 +32,7 @@ def test_post_sms_json_schema_bad_uuid_and_missing_phone_number():
assert len(error.get('fields')) == 2
assert {"phone_number": "is a required property"} in error['fields']
assert {"template_id": "not a valid UUID"} in error['fields']
assert error.get('code') == '1001'
assert error.get('status_code') == 400
def test_post_sms_schema_with_personalisation_that_is_not_a_dict():
@@ -48,7 +48,7 @@ def test_post_sms_schema_with_personalisation_that_is_not_a_dict():
assert "POST v2/notifications/sms" in error['message']
assert len(error.get('fields')) == 1
assert error['fields'][0] == {"personalisation": "should contain key value pairs"}
assert error.get('code') == '1001'
assert error.get('status_code') == 400
valid_response = {
@@ -84,6 +84,6 @@ def test_post_sms_response_schema_missing_uri():
with pytest.raises(ValidationError) as e:
validate(j, post_sms_response)
error = json.loads(e.value.message)
assert '1001' == error['code']
assert error['status_code'] == 400
assert 'Validation error occurred - response v2/notifications/sms' == error['message']
assert [{"uri": "is a required property"}] == error['fields']

View File

@@ -55,7 +55,6 @@ def test_post_sms_notification_returns_404_and_missing_template(notify_api, samp
assert response.headers['Content-type'] == 'application/json'
error_json = json.loads(response.get_data(as_text=True))
assert error_json['code'] == 10400
assert error_json['message'] == 'Template not found'
assert error_json['fields'] == [{'template': 'Template not found'}]
@@ -76,7 +75,7 @@ def test_post_sms_notification_returns_403_and_well_formed_auth_error(notify_api
assert response.status_code == 401
assert response.headers['Content-type'] == 'application/json'
error_resp = json.loads(response.get_data(as_text=True))
assert error_resp['code'] == 401
assert error_resp['status_code'] == 401
assert error_resp['message'] == 'Unauthorized, authentication token must be provided'
assert error_resp['fields'] == {'token': ['Unauthorized, authentication token must be provided']}
@@ -98,6 +97,6 @@ def test_post_sms_notification_returns_400_and_for_schema_problems(notify_api, s
assert response.status_code == 400
assert response.headers['Content-type'] == 'application/json'
error_resp = json.loads(response.get_data(as_text=True))
assert error_resp['code'] == '1001'
assert error_resp['status_code'] == 400
assert error_resp['message'] == 'Validation error occurred - POST v2/notifications/sms'
assert error_resp['fields'] == [{"template_id": "is a required property"}]