{"id":1101,"hash":"c3ca3201f2485be4c678ed2a40397094514ac3cf218a2147d5e4bc0d04996f6f","pattern":"Properly catch boto3 Errors","full_message":"I am developing a django app which communicates with several Amazon Web Services.\n\nSo far I am having trouble dealing with and catching exceptions thrown by the boto3 client. What I am doing seems unnecessarily tedious:\n\nExample:\n\nclient = boto3.client('sns')\nclient.create_platform_endpoint(PlatformApplicationArn=SNS_APP_ARN, Token=token)\n\nthis might throw an botocore.errorfactory.InvalidParameterException if e.g. the token is bad.\n\nclient.get_endpoint_attributes(EndpointArn=endpoint_arn)\n\nmight throw an botocore.errorfactory.NotFoundException.\n\nFirst, I can't find these Errors anywhere in code, so they are probably generated somewhere. Bottom line: I can't import it and catch it as usual.\n\nSecond, I found one way to catch the error here using:\n\ntry:\n    # boto3 stuff\nexcept botocore.exceptions.ClientError as e:\n    if e.response['Error']['Code'] == 'NotFound':\n        # handle exception\n    else:\n        raise e\n\nBut I have to remove the Exception part of the error name. Seems very random and I have no clue whether I would remove the Error in botocore.exceptions.ParamValidationError if I wanted to catch that one. So it's hard to generalize.\n\nAnother way to catch the error is using the boto3 client object I got:\n\ntry:\n    # boto3 stuff\nexcept client.exceptions.NotFoundException as e:\n    # handle exception\n\nThis seems the cleanest way so far. But I don't always have the boto3 client object at hand where I want to catch the error. Also I am still only trying things out, so it's mostly guess work.\n\nDoes anybody know how boto3 errors are supposed to be handled?\n\nOr can point me towards some coherent documentation which mentions the errors above? Thanks","ecosystem":"pypi","package_name":"amazon-web-services","package_version":null,"solution":"You've summarized the situation well. The old boto had a simple hardcoded approach to supporting AWS APIs. boto3, in what appears to be an attempt to reduce the overhead of keeping Python client synced with evolving features on the various apis, has been more squishy around exceptions, so the ClientError approach you outlined above used to be the canonical way.\n\nIn 2017 they introduced the second mechanism you highlight: 'modeled' exceptions available on the client.\n\nI am not familiar with SNS but in my experience with other AWS products, the ClientError naming matches up with the HTTP apis, which tend to be well documented. So I would start here: https://docs.aws.amazon.com/sns/latest/api/Welcome.html\n\nIt looks like the new-style modeled exceptions are generated from service definition files that live in botocore module. I can't find any documentation about it, but go browse around the AWS service models in https://github.com/boto/botocore/tree/master/botocore/data.\n\nAlso, it's good to know that if you are not (in contrast to OP's code) dealing directly with the low-level client, but instead are using a high-level AWS ServiceResource object, a low-level client is still easily available at my_service_resource.meta.client so you can handle exceptions like this:\n\ntry:\n    my_service_resource.do_stuff()\nexcept my_service_resource.meta.client.exceptions.NotFoundException as e:\n    # handle exception","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/46174385/properly-catch-boto3-errors","votes":40,"created_at":"2026-04-19T04:52:20.955220+00:00","updated_at":"2026-04-19T04:52:20.955220+00:00"}