Change endpoint responses where there are marshalling, unmarshalling

or param errors to raise invalid data exception. That will cause
those responses to be handled in by errors.py, which will log
the errors.

Set most of schemas to strict mode so that marshmallow will raise
exception rather than checking for errors in return tuple from load.

Added handler to errors.py for marshmallow validation errors.
This commit is contained in:
Adam Shimali
2016-06-14 15:07:23 +01:00
parent 5a66884f0e
commit b33312b855
16 changed files with 240 additions and 267 deletions

View File

@@ -19,7 +19,10 @@ from app.schemas import (template_schema, template_history_schema)
template = Blueprint('template', __name__, url_prefix='/service/<uuid:service_id>/template')
from app.errors import register_errors
from app.errors import (
register_errors,
InvalidRequest
)
register_errors(template)
@@ -28,27 +31,23 @@ def _content_count_greater_than_limit(content, template_type):
template = Template({'content': content, 'template_type': template_type})
if template_type == 'sms' and \
template.content_count > current_app.config.get('SMS_CHAR_COUNT_LIMIT'):
return True, jsonify(
result="error",
message={'content': ['Content has a character count greater than the limit of {}'.format(
current_app.config.get('SMS_CHAR_COUNT_LIMIT'))]}
)
return False, ''
return True
return False
@template.route('', methods=['POST'])
def create_template(service_id):
fetched_service = dao_fetch_service_by_id(service_id=service_id)
new_template, errors = template_schema.load(request.get_json())
if errors:
return jsonify(result="error", message=errors), 400
new_template = template_schema.load(request.get_json()).data
new_template.service = fetched_service
new_template.content = _strip_html(new_template.content)
over_limit, json_resp = _content_count_greater_than_limit(
new_template.content,
new_template.template_type)
over_limit = _content_count_greater_than_limit(new_template.content, new_template.template_type)
if over_limit:
return json_resp, 400
char_count_limit = current_app.config.get('SMS_CHAR_COUNT_LIMIT')
message = 'Content has a character count greater than the limit of {}'.format(char_count_limit)
errors = {'content': [message]}
raise InvalidRequest(errors, status_code=400)
dao_create_template(new_template)
return jsonify(data=template_schema.dump(new_template).data), 201
@@ -65,14 +64,13 @@ def update_template(service_id, template_id):
if _template_has_not_changed(current_data, updated_template):
return jsonify(data=updated_template), 200
update_dict, errors = template_schema.load(updated_template)
if errors:
return jsonify(result="error", message=errors), 400
over_limit, json_resp = _content_count_greater_than_limit(
updated_template['content'],
fetched_template.template_type)
update_dict = template_schema.load(updated_template).data
over_limit = _content_count_greater_than_limit(updated_template['content'], fetched_template.template_type)
if over_limit:
return json_resp, 400
char_count_limit = current_app.config.get('SMS_CHAR_COUNT_LIMIT')
message = 'Content has a character count greater than the limit of {}'.format(char_count_limit)
errors = {'content': [message]}
raise InvalidRequest(errors, status_code=400)
dao_update_template(update_dict)
return jsonify(data=template_schema.dump(update_dict).data), 200
@@ -80,39 +78,35 @@ def update_template(service_id, template_id):
@template.route('', methods=['GET'])
def get_all_templates_for_service(service_id):
templates = dao_get_all_templates_for_service(service_id=service_id)
data, errors = template_schema.dump(templates, many=True)
data = template_schema.dump(templates, many=True).data
return jsonify(data=data)
@template.route('/<uuid:template_id>', methods=['GET'])
def get_template_by_id_and_service_id(service_id, template_id):
fetched_template = dao_get_template_by_id_and_service_id(template_id=template_id, service_id=service_id)
data, errors = template_schema.dump(fetched_template)
data = template_schema.dump(fetched_template).data
return jsonify(data=data)
@template.route('/<uuid:template_id>/version/<int:version>')
def get_template_version(service_id, template_id, version):
data, errors = template_history_schema.dump(
data = template_history_schema.dump(
dao_get_template_by_id_and_service_id(
template_id=template_id,
service_id=service_id,
version=version
)
)
if errors:
return jsonify(result='error', message=errors), 400
).data
return jsonify(data=data)
@template.route('/<uuid:template_id>/versions')
def get_template_versions(service_id, template_id):
data, errors = template_history_schema.dump(
data = template_history_schema.dump(
dao_get_template_versions(service_id=service_id, template_id=template_id),
many=True
)
if errors:
return jsonify(result='error', message=errors), 400
).data
return jsonify(data=data)