From a3572731a25b6de9cbc1d78dfa5715bf82fcce74 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Sat, 10 Sep 2022 21:58:26 +0100 Subject: [PATCH] Improve error messages when S3 returns a 403 error during an `ls` operation Include the bucket name and URI in the logged error message (via the exception object). --- S3/Exceptions.py | 14 ++++++++++++++ S3/S3.py | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/S3/Exceptions.py b/S3/Exceptions.py index 99f5358d..3116ca87 100644 --- a/S3/Exceptions.py +++ b/S3/Exceptions.py @@ -136,6 +136,20 @@ def parse_error_xml(tree): raise S3ResponseError("Malformed error XML returned from remote server.") return info +class S3AccessDenied(S3Error): + def __init__(self, resource, response): + super(S3AccessDenied, self).__init__(response) + self.resource = resource + debug("S3AccessDenied: %s (%s): %s" % (self.status, self.reason, self.resource)) + + def __unicode__(self): + retval = u"%d " % (self.status) + retval += (u"(%s)" % (self.code or self.reason)) + error_msg = self.message + if error_msg: + retval += (u": %s" % error_msg) + retval += (u": %s" % (self.resource)) + return retval class CloudFrontError(S3Error): pass diff --git a/S3/S3.py b/S3/S3.py index 4439893e..74cdf516 100644 --- a/S3/S3.py +++ b/S3/S3.py @@ -1469,7 +1469,7 @@ def _http_403_handler(self, request, response, fn, *args, **kwargs): self.fallback_to_signature_v2 = True return fn(*args, **kwargs) - raise S3Error(response) + raise S3AccessDenied(request.resource, response) def update_region_inner_request(self, request): """Get and update region for the request if needed.