Cleanup SWF to use HTTP exceptions so that the standalone server will work. Closes #495.

This commit is contained in:
Steve Pulec 2016-01-17 18:00:57 -05:00
commit a53a97d136
12 changed files with 136 additions and 142 deletions

View file

@ -1,12 +1,13 @@
from werkzeug.exceptions import HTTPException
from jinja2 import DictLoader, Environment
from six import text_type
ERROR_RESPONSE = u"""<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Errors>
<Error>
<Code>{{code}}</Code>
<Code>{{error_type}}</Code>
<Message>{{message}}</Message>
{% block extra %}{% endblock %}
</Error>
@ -15,14 +16,33 @@ ERROR_RESPONSE = u"""<?xml version="1.0" encoding="UTF-8"?>
</Response>
"""
ERROR_JSON_RESPONSE = u"""{
"message": "{{message}}",
"__type": "{{error_type}}"
}
"""
class RESTError(HTTPException):
templates = {
'error': ERROR_RESPONSE
'error': ERROR_RESPONSE,
'error_json': ERROR_JSON_RESPONSE,
}
def __init__(self, code, message, template='error', **kwargs):
def __init__(self, error_type, message, template='error', **kwargs):
super(RESTError, self).__init__()
env = Environment(loader=DictLoader(self.templates))
self.error_type = error_type
self.message = message
self.description = env.get_template(template).render(
code=code, message=message, **kwargs)
error_type=error_type, message=message, **kwargs)
class JsonRESTError(RESTError):
def __init__(self, error_type, message, template='error_json', **kwargs):
super(JsonRESTError, self).__init__(error_type, message, template, **kwargs)
def get_headers(self, *args, **kwargs):
return [('Content-Type', 'application/json')]
def get_body(self, *args, **kwargs):
return self.description