Skip to content

Commit

Permalink
Recursively walk subclass tree to build code->exception class mapping.
Browse files Browse the repository at this point in the history
  • Loading branch information
tseaver committed Nov 7, 2014
1 parent 89788fb commit e322609
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions gcloud/storage/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,16 @@ def make_exception(response, content):
return error


for name, value in globals().items():
code = getattr(value, 'code', None)
def _walk_subclasses(klass):
"""Recursively walk subclass tree."""
for sub in klass.__subclasses__():
yield sub
for subsub in _walk_subclasses(sub):
yield subsub


# Build the code->exception class mapping.
for eklass in _walk_subclasses(StorageError):
code = getattr(eklass, 'code', None)
if code is not None:
_HTTP_CODE_TO_EXCEPTION[code] = value
_HTTP_CODE_TO_EXCEPTION[code] = eklass

0 comments on commit e322609

Please sign in to comment.