diff --git a/moto/core/exceptions.py b/moto/core/exceptions.py index fcb6ec96e..c66b8f257 100644 --- a/moto/core/exceptions.py +++ b/moto/core/exceptions.py @@ -3,6 +3,15 @@ from jinja2 import DictLoader, Environment from six import text_type +SINGLE_ERROR_RESPONSE = u""" + + {{error_type}} + {{message}} + {% block extra %}{% endblock %} + 7a62c49f-347e-4fc4-9331-6e8eEXAMPLE + +""" + ERROR_RESPONSE = u""" @@ -24,6 +33,7 @@ ERROR_JSON_RESPONSE = u"""{ class RESTError(HTTPException): templates = { + 'single_error': SINGLE_ERROR_RESPONSE, 'error': ERROR_RESPONSE, 'error_json': ERROR_JSON_RESPONSE, } diff --git a/moto/s3/exceptions.py b/moto/s3/exceptions.py index f359e5e96..2f444e2dd 100644 --- a/moto/s3/exceptions.py +++ b/moto/s3/exceptions.py @@ -2,17 +2,20 @@ from __future__ import unicode_literals from moto.core.exceptions import RESTError -ERROR_WITH_BUCKET_NAME = """{% extends 'error' %} +ERROR_WITH_BUCKET_NAME = """{% extends 'single_error' %} {% block extra %}{{ bucket }}{% endblock %} """ -ERROR_WITH_KEY_NAME = """{% extends 'error' %} +ERROR_WITH_KEY_NAME = """{% extends 'single_error' %} {% block extra %}{{ key_name }}{% endblock %} """ class S3ClientError(RESTError): - pass + def __init__(self, *args, **kwargs): + kwargs.setdefault('template', 'single_error') + self.templates['bucket_error'] = ERROR_WITH_BUCKET_NAME + super(S3ClientError, self).__init__(*args, **kwargs) class BucketError(S3ClientError): diff --git a/tests/test_s3/test_s3.py b/tests/test_s3/test_s3.py index eb68c9ed4..95a755ab1 100644 --- a/tests/test_s3/test_s3.py +++ b/tests/test_s3/test_s3.py @@ -1013,6 +1013,21 @@ def test_boto3_head_object(): s3.Object('blah', 'hello2.txt').meta.client.head_object(Bucket='blah', Key='hello_bad.txt') +@mock_s3 +def test_boto3_get_object(): + s3 = boto3.resource('s3', region_name='us-east-1') + s3.create_bucket(Bucket="blah") + + s3.Object('blah', 'hello.txt').put(Body="some text") + + s3.Object('blah', 'hello.txt').meta.client.head_object(Bucket='blah', Key='hello.txt') + + with assert_raises(ClientError) as e: + s3.Object('blah', 'hello2.txt').get() + + e.exception.response['Error']['Code'].should.equal('NoSuchKey') + + @mock_s3 def test_boto3_head_object_with_versioning(): s3 = boto3.resource('s3', region_name='us-east-1')