diff --git a/moto/core/exceptions.py b/moto/core/exceptions.py new file mode 100644 index 000000000..d5a754e78 --- /dev/null +++ b/moto/core/exceptions.py @@ -0,0 +1,28 @@ +from werkzeug.exceptions import HTTPException +from jinja2 import DictLoader, Environment + + +ERROR_RESPONSE = u""" + + + + {{code}} + {{message}} + {% block extra %}{% endblock %} + + + 7a62c49f-347e-4fc4-9331-6e8eEXAMPLE + +""" + + +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) diff --git a/moto/ec2/exceptions.py b/moto/ec2/exceptions.py index 599f0d00d..3c181e045 100644 --- a/moto/ec2/exceptions.py +++ b/moto/ec2/exceptions.py @@ -1,13 +1,9 @@ from __future__ import unicode_literals -from werkzeug.exceptions import BadRequest -from jinja2 import Template +from moto.core.exceptions import RESTError -class EC2ClientError(BadRequest): - def __init__(self, code, message): - super(EC2ClientError, self).__init__() - self.description = ERROR_RESPONSE_TEMPLATE.render( - code=code, message=message) +class EC2ClientError(RESTError): + code = 400 class DependencyViolationError(EC2ClientError): @@ -306,17 +302,3 @@ class InvalidCIDRSubnetError(EC2ClientError): "InvalidParameterValue", "invalid CIDR subnet specification: {0}" .format(cidr)) - - -ERROR_RESPONSE = u""" - - - - {{code}} - {{message}} - - - 7a62c49f-347e-4fc4-9331-6e8eEXAMPLE - -""" -ERROR_RESPONSE_TEMPLATE = Template(ERROR_RESPONSE)