boto3を使ってるときに、存在しないS3のオブジェクトを読み込んだ時のエラーハンドリングがしたかった時に調べたことをメモ。
NoSuchKeyのエラーが返るけど、動的に生成される例外クラスのようで、インポートしてexcept節に記述できない。
stackoverflowに対応方法が書いてた。
http://stackoverflow.com/questions/33068055/boto3-python-and-how-to-handle-errors
以下、抜粋。
import boto3
from botocore.exceptions import ClientError
try:
iam = boto3.client('iam')
user = iam.create_user(UserName='fred')
print "Created user: %s" % user
except ClientError as e:
if e.response['Error']['Code'] == 'EntityAlreadyExists':
print "User already exists"
else:
print "Unexpected error: %s" % e
botocore.exceptionsのClientErrorとして例外をキャッチして、response['Error']['Code']の値でエラー内容を確認する。