Botocore: How to use generated exceptions

Created on 23 Aug 2019  路  6Comments  路  Source: boto/botocore

So I'm trying to use SSM and catch scenario where value doesn't exist. The call generates botocore.errorfactory.ParameterNotFound: An error occurred (ParameterNotFound) when calling the GetParameter operation

The thing is that ParameterNotFound does not exist the class with the error is dynamically generated and I can't reference it in the code (or at least not from botocore.errorfactory.ParameterNotFound). What's the point of having exceptions like this when they can't be caught? Why bothering with dynamically generated class, when you could just have a generic error and inside of its properties I could extract the actual cause. Or is there a way to use it that I'm not aware of?

closed-for-staleness documentation

Most helpful comment

Yes, this is what I ended up doing, my point is that those dynamically generated classes for exceptions are pointless. You don't have a static class that you can use to catch them, thankfully they all inherit from ClientError so one can catch them the way you showed the problem is that when you see botocore.errorfactory.ParameterNotFound it's not obvious how to handle it. You don't know that it inherits from ClientError and you don't know that exception name could be the error code. I would like to propose abolish the dynamic classes and simply raise ClientError so it shows ClientError('ParameterNotFound', ...) or similar that makes it clear what exception happened and how to catch it.

All 6 comments

@takeda - Thank you for your post. Basically this error comes from the service when the requested parameter does not exist. You can see in the documentation that what does the error means and what is the cause of this. You can see in the debug log that this error comes from service. You can find the root causes in the code for client errors only.

Hope it helps and please let me know if you have any questions.

The problem I'm having is that I can't use such exception in try-except clause, so it has a little of use as an actual exception.

You can use try except block to see if parameter exists or not. Here is the sample code:

import boto3
from botocore.exceptions import ClientError

client = boto3.client('ssm')

try:
   response = client.get_parameter(Name='test')
except ClientError as e:
        if e.response['Error']['Code'] == 'ParameterNotFound':
            print('Parameter does not exist')
        else:
            print('parameter exists')

For more information see botocore Error Handling:
https://botocore.amazonaws.com/v1/documentation/api/latest/client_upgrades.html#error-handling

Hope it helps and please let me know if you have any questions.

Yes, this is what I ended up doing, my point is that those dynamically generated classes for exceptions are pointless. You don't have a static class that you can use to catch them, thankfully they all inherit from ClientError so one can catch them the way you showed the problem is that when you see botocore.errorfactory.ParameterNotFound it's not obvious how to handle it. You don't know that it inherits from ClientError and you don't know that exception name could be the error code. I would like to propose abolish the dynamic classes and simply raise ClientError so it shows ClientError('ParameterNotFound', ...) or similar that makes it clear what exception happened and how to catch it.

@takeda - Modeled exceptions can be accessed through the client. Instead of botocore.errorfactory.ParameterNotFound you can do client.exceptions.ParameterNotFound.

client = boto3.client('ssm')

try:
   response = client.get_parameter(Name='sweta')
except client.exceptions.ParameterNotFound as e:
    print(e)

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 .

More information on how to catch exceptions with boto3/botocore
https://boto3.amazonaws.com/v1/documentation/api/latest/guide/error-handling.html

Closing this issue as it has been fixed. Please reopen if you have any more concerns.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kapilt picture kapilt  路  5Comments

thehesiod picture thehesiod  路  6Comments

eode picture eode  路  5Comments

sean-zou picture sean-zou  路  4Comments

AlexDiede picture AlexDiede  路  3Comments