Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exceptions can't be caught with try/except #1195

Closed
matt-telstra opened this issue Jul 21, 2017 · 7 comments
Closed

Exceptions can't be caught with try/except #1195

matt-telstra opened this issue Jul 21, 2017 · 7 comments
Labels
documentation This is a problem with documentation. enhancement This issue requests an improvement to a current feature. question

Comments

@matt-telstra
Copy link

Whenever I get an exception in boto which I want to catch, I copy and paste the exception code into the except block, and it normally fails because python doesn't know which exception I'm trying to catch.

Steps to reproduce

Run the following code:

import botocore
import boto3


client = boto3.client('s3')

b = 'somebucketnamewhichdoesntexist'

response = client.get_bucket_location(
    Bucket=b
)

Because that bucket doesn't exist, boto raises a botocore.errorfactory.NoSuchBucket exception.

So copy and paste botocore.errorfactory.NoSuchBucket into an except block.

import botocore
import boto3


client = boto3.client('s3')

b = 'somebucketnamewhichdoesntexist'

try:
    response = client.get_bucket_location(
        Bucket=b
    )
except botocore.errorfactory.NoSuchBucket as e:
    print('bucket %s doesnt exist')

Run this new code

Expected behaviour

  • The except block catches the exception
  • The script prints bucket somebucketnamewhichdoesntexist doesnt exist
  • The script exits successfully

Actual Behaviour

The interpreter doesn't know what botocore.errorfactory.NoSuchBucket is

Traceback (most recent call last):
File "botofail.py", line 13, in
except botocore.errorfactory.NoSuchBucket as e:
AttributeError: 'module' object has no attribute 'NoSuchBucket'

@joguSD joguSD added documentation This is a problem with documentation. question enhancement This issue requests an improvement to a current feature. labels Jul 21, 2017
@joguSD
Copy link
Contributor

joguSD commented Jul 21, 2017

Modeled exceptions needs to be accessed through the client.
So rather than having botocore.errorfactory.NoSuchBucket you need client.exceptions.NoSuchBucket. I agree that this is confusing and the current documentation is insufficient.

More information on this can be found at this pr: boto/botocore#1113 and this issue: #167

@joguSD joguSD closed this as completed Jul 21, 2017
@teledyn
Copy link

teledyn commented Jan 12, 2018

using build to create a webmasters resource, I'm told the client has no 'exceptions' -- has this changed since July?

@michael-robbins
Copy link

I think it needs to be a 'client' as a 'resource' doesn't have 'exceptions.

I'm trying to capture a 'botocore.errorfactory.AlreadyExistsException' but this is raised by a 'resource', documentation here is very lacking...

@afrittoli
Copy link

I have code that may use local storage or s3 - when using local storage I don't create an s3 client, so my try/except code fails since it tries to get the exception from None.
It would be nice to be able to get the exceptions from a class rather than from an object.

@dazza-codes
Copy link

To access the client exceptions from a resource, it might be possible using:

>>> type(s3)
<class 'boto3.resources.factory.s3.ServiceResource'>
>>> s3.meta.client.exceptions
<botocore.errorfactory.S3Exceptions object at 0x7f2c97eb8128>
>>> s3.meta.client.exceptions.NoSuchBucket
<class 'botocore.errorfactory.NoSuchBucket'>
>>> s3.meta.client.exceptions.NoSuchKey
<class 'botocore.errorfactory.NoSuchKey'>

But these don't actually catch a

botocore.exceptions.ClientError: An error occurred (404) when calling the HeadObject operation: Not Found

In general, the docs are confusing with regard to the preferred idioms for using a client or a resource to download an s3 file and handle exceptions gracefully.

@brianbruggeman
Copy link

I ran into the same issue, and what I want to do is debug why something isn't working, but none of the standard python methods for try/except seem to work (still: may 2019).

botocore should remove the factories and modify them to drop static files into botocore so there was both traceability and ease of understanding. As it is, there are quite a few really poorly contrived python structures. The factories in use here are an anti-pattern for the user.

@dazza-codes
Copy link

Current code to make sense of some exceptions has to inspect the response content of the client exception, e.g.

def stat(bucket, key):
    try:
        client = boto3.client("s3")
        return client.head_object(Bucket=bucket, Key=key)

    except botocore.exceptions.ClientError as err:
        status = err.response["ResponseMetadata"]["HTTPStatusCode"]
        errcode = err.response["Error"]["Code"]
        if status == 404:
            logging.warning("Missing object, %s", errcode)
        elif status == 403:
            logging.error("Access denied, %s", errcode)
        else:
            logging.exception("Error in request, %s", errcode)
    return {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation This is a problem with documentation. enhancement This issue requests an improvement to a current feature. question
Projects
None yet
Development

No branches or pull requests

7 participants