Catch AttributeError less often

I always get tripped up when I'm editing code that catches
AttributeError and does something with it. I'll type "req.emthod" or
something, and next thing I know I'm getting 405s in all my unit
tests. This diff removes some places where we catch AttributeError
(sometimes, having deliberately thrown it only one line before) so
that typos can crash the way Guido intended.

Change-Id: I2f7586f96b41a97e6ae254efc83218b3b5c6cc9e
This commit is contained in:
Samuel Merritt 2016-06-14 16:10:16 -07:00
parent 6c9a1899a1
commit 3944d82038
5 changed files with 10 additions and 23 deletions

View File

@ -262,14 +262,10 @@ class AccountController(BaseStorageServer):
else: else:
try: try:
# disallow methods which are not publicly accessible # disallow methods which are not publicly accessible
try: if req.method not in self.allowed_methods:
if req.method not in self.allowed_methods:
raise AttributeError('Not allowed method.')
except AttributeError:
res = HTTPMethodNotAllowed() res = HTTPMethodNotAllowed()
else: else:
method = getattr(self, req.method) res = getattr(self, req.method)(req)
res = method(req)
except HTTPException as error_response: except HTTPException as error_response:
res = error_response res = error_response
except (Exception, Timeout): except (Exception, Timeout):

View File

@ -378,9 +378,8 @@ class Manager(object):
""" """
cmd = cmd.lower().replace('-', '_') cmd = cmd.lower().replace('-', '_')
try: f = getattr(self, cmd, None)
f = getattr(self, cmd) if f is None:
except AttributeError:
raise UnknownCommandError(cmd) raise UnknownCommandError(cmd)
if not hasattr(f, 'publicly_accessible'): if not hasattr(f, 'publicly_accessible'):
raise UnknownCommandError(cmd) raise UnknownCommandError(cmd)

View File

@ -594,14 +594,10 @@ class ContainerController(BaseStorageServer):
else: else:
try: try:
# disallow methods which have not been marked 'public' # disallow methods which have not been marked 'public'
try: if req.method not in self.allowed_methods:
if req.method not in self.allowed_methods:
raise AttributeError('Not allowed method.')
except AttributeError:
res = HTTPMethodNotAllowed() res = HTTPMethodNotAllowed()
else: else:
method = getattr(self, req.method) res = getattr(self, req.method)(req)
res = method(req)
except HTTPException as error_response: except HTTPException as error_response:
res = error_response res = error_response
except (Exception, Timeout): except (Exception, Timeout):

View File

@ -1031,10 +1031,7 @@ class ObjectController(BaseStorageServer):
else: else:
try: try:
# disallow methods which have not been marked 'public' # disallow methods which have not been marked 'public'
try: if req.method not in self.allowed_methods:
if req.method not in self.allowed_methods:
raise AttributeError('Not allowed method.')
except AttributeError:
res = HTTPMethodNotAllowed() res = HTTPMethodNotAllowed()
else: else:
method = getattr(self, req.method) method = getattr(self, req.method)

View File

@ -382,10 +382,9 @@ class Application(object):
req.headers['x-trans-id'] = req.environ['swift.trans_id'] req.headers['x-trans-id'] = req.environ['swift.trans_id']
controller.trans_id = req.environ['swift.trans_id'] controller.trans_id = req.environ['swift.trans_id']
self.logger.client_ip = get_remote_client(req) self.logger.client_ip = get_remote_client(req)
try:
handler = getattr(controller, req.method) handler = getattr(controller, req.method, None)
getattr(handler, 'publicly_accessible') if not getattr(handler, 'publicly_accessible', False):
except AttributeError:
allowed_methods = getattr(controller, 'allowed_methods', set()) allowed_methods = getattr(controller, 'allowed_methods', set())
return HTTPMethodNotAllowed( return HTTPMethodNotAllowed(
request=req, headers={'Allow': ', '.join(allowed_methods)}) request=req, headers={'Allow': ', '.join(allowed_methods)})