create a base RESTError exception

This commit is contained in:
Konstantinos Koukopoulos 2015-02-10 15:31:28 +02:00
parent 8a8aba3395
commit 7c719269be
2 changed files with 31 additions and 21 deletions

28
moto/core/exceptions.py Normal file
View File

@ -0,0 +1,28 @@
from werkzeug.exceptions import HTTPException
from jinja2 import DictLoader, Environment
ERROR_RESPONSE = u"""<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Errors>
<Error>
<Code>{{code}}</Code>
<Message>{{message}}</Message>
{% block extra %}{% endblock %}
</Error>
</Errors>
<RequestID>7a62c49f-347e-4fc4-9331-6e8eEXAMPLE</RequestID>
</Response>
"""
class RESTError(HTTPException):
templates = {
'error': ERROR_RESPONSE
}
def __init__(self, code, message, template='error', **kwargs):
super(RESTError, self).__init__()
env = Environment(loader=DictLoader(self.templates))
self.description = env.get_template(template).render(
code=code, message=message, **kwargs)

View File

@ -1,13 +1,9 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from werkzeug.exceptions import BadRequest from moto.core.exceptions import RESTError
from jinja2 import Template
class EC2ClientError(BadRequest): class EC2ClientError(RESTError):
def __init__(self, code, message): code = 400
super(EC2ClientError, self).__init__()
self.description = ERROR_RESPONSE_TEMPLATE.render(
code=code, message=message)
class DependencyViolationError(EC2ClientError): class DependencyViolationError(EC2ClientError):
@ -306,17 +302,3 @@ class InvalidCIDRSubnetError(EC2ClientError):
"InvalidParameterValue", "InvalidParameterValue",
"invalid CIDR subnet specification: {0}" "invalid CIDR subnet specification: {0}"
.format(cidr)) .format(cidr))
ERROR_RESPONSE = u"""<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Errors>
<Error>
<Code>{{code}}</Code>
<Message>{{message}}</Message>
</Error>
</Errors>
<RequestID>7a62c49f-347e-4fc4-9331-6e8eEXAMPLE</RequestID>
</Response>
"""
ERROR_RESPONSE_TEMPLATE = Template(ERROR_RESPONSE)