topless
topless

Reputation: 8221

How can I handle a boto exception in python?

How can I wrap a boto.storage_uri() call in python so I can handle possible exceptions?

Upvotes: 8

Views: 14622

Answers (3)

Jeremy Brooks
Jeremy Brooks

Reputation: 589

Looking at the boto3 source, Boto3Error is declared as the base of all boto errors. So, you can probably do this:

try:
    boto.storage_uri()
except Boto3Error:
    # handle errors

Upvotes: 0

Nick Bastin
Nick Bastin

Reputation: 31299

The first question is what exceptions is this call likely to generate? You do not want to make a blanket exception handler in any language. You should first take a look at the Boto documentation to see if it documents the exceptions you might see from a given call, but if not I would first try:

try:
  uri = boto.storage_uri()
except Exception, e:
  print e

Or log the exception (with the logging package exception method), but either way you want to take note of what types of exceptions you see while you're testing and whether you should handle any of them specially. You also may want to review the Python Tutorial section on Exceptions and Errors.

Upvotes: 3

GaryWiz
GaryWiz

Reputation: 331

Your question about Boto is a good one, not not easy to answer. The Boto exception hierarchy is poorly designed, and ultimately the only way to determine what the exception you want to trap is requires looking at the boto source code.

For example if you look at (on Ubuntu) /usr/share/pyshared/boto/exception.py you will see that there are two broad classes: boto.exception.BotoClientError boto.exception.BotoServerError

Many of the exceptions are derived from these two, though the concept of "Client" and "Server" is not very well defined and you probably would want to check for both to be sure as many exceptions can happen unexpected (as usual). However, exceptions such as boto.exception.NoAuthHandlerFound is derived directly from Exception and therefore you would have to check for it separately.

Unfortunately from looking at the code, it appears that there is neither consistency nor much care in defining the exception hierarchy in Boto, which is a flaw in Boto's design which unfortunately requires that you rely on more broad exception checking than would normally be recommended.

Upvotes: 30

Related Questions