Fix cross-transport error handler

zaqar.common.api.utils.raises_conn_error decorator is intended to catch
all of the uncatched exceptions to return 500 error response.

But now it can't return normal error response, because it passes None
instead of Request object to the Response constructor.

Eventually AttributeError is raised:
AttributeError: 'NoneType' object has no attribute 'get_request'

Because of unexpected AttributeError, no response is sent to the client.

This patch makes zaqar.common.api.utils.raises_conn_error pass Request
object to the Response constructor.

Also it renames this decorator to on_exception_sends_500, because that's
what this decorator actually is: it catches exceptions and returns
response with code 500. It doesn't raise connection error exceptions
like these similarly named decorators in the storage drivers:
zaqar.storage.mongodb.utils.raises_conn_error
and
zaqar.storage.redis.utils.raises_conn_error

Closes-Bug: 1538794
Change-Id: I751d46383e8d434f65fea1694bcb8c76923d3aca
This commit is contained in:
Eva Balycheva 2016-02-19 16:17:52 +03:00
parent b24be72fde
commit 7de2edd159
2 changed files with 25 additions and 24 deletions

View File

@ -42,7 +42,7 @@ class Endpoints(object):
self._subscription_url = None self._subscription_url = None
# Queues # Queues
@api_utils.raises_conn_error @api_utils.on_exception_sends_500
def queue_list(self, req): def queue_list(self, req):
"""Gets a list of queues """Gets a list of queues
@ -81,7 +81,7 @@ class Endpoints(object):
return response.Response(req, body, headers) return response.Response(req, body, headers)
@api_utils.raises_conn_error @api_utils.on_exception_sends_500
def queue_create(self, req): def queue_create(self, req):
"""Creates a queue """Creates a queue
@ -118,7 +118,7 @@ class Endpoints(object):
headers = {'status': 201} if created else {'status': 204} headers = {'status': 201} if created else {'status': 204}
return response.Response(req, body, headers) return response.Response(req, body, headers)
@api_utils.raises_conn_error @api_utils.on_exception_sends_500
def queue_delete(self, req): def queue_delete(self, req):
"""Deletes a queue """Deletes a queue
@ -144,7 +144,7 @@ class Endpoints(object):
headers = {'status': 204} headers = {'status': 204}
return response.Response(req, body, headers) return response.Response(req, body, headers)
@api_utils.raises_conn_error @api_utils.on_exception_sends_500
def queue_get(self, req): def queue_get(self, req):
"""Gets a queue """Gets a queue
@ -178,7 +178,7 @@ class Endpoints(object):
headers = {'status': 200} headers = {'status': 200}
return response.Response(req, body, headers) return response.Response(req, body, headers)
@api_utils.raises_conn_error @api_utils.on_exception_sends_500
def queue_get_stats(self, req): def queue_get_stats(self, req):
"""Gets queue stats """Gets queue stats
@ -220,7 +220,7 @@ class Endpoints(object):
return response.Response(req, body, headers) return response.Response(req, body, headers)
# Messages # Messages
@api_utils.raises_conn_error @api_utils.on_exception_sends_500
def message_list(self, req): def message_list(self, req):
"""Gets a list of messages on a queue """Gets a list of messages on a queue
@ -272,7 +272,7 @@ class Endpoints(object):
return response.Response(req, body, headers) return response.Response(req, body, headers)
@api_utils.raises_conn_error @api_utils.on_exception_sends_500
def message_get(self, req): def message_get(self, req):
"""Gets a message from a queue """Gets a message from a queue
@ -309,7 +309,7 @@ class Endpoints(object):
return response.Response(req, body, headers) return response.Response(req, body, headers)
@api_utils.raises_conn_error @api_utils.on_exception_sends_500
def message_get_many(self, req): def message_get_many(self, req):
"""Gets a set of messages from a queue """Gets a set of messages from a queue
@ -347,7 +347,7 @@ class Endpoints(object):
return response.Response(req, body, headers) return response.Response(req, body, headers)
@api_utils.raises_conn_error @api_utils.on_exception_sends_500
def message_post(self, req): def message_post(self, req):
"""Post a set of messages to a queue """Post a set of messages to a queue
@ -427,7 +427,7 @@ class Endpoints(object):
return response.Response(req, body, headers) return response.Response(req, body, headers)
@api_utils.raises_conn_error @api_utils.on_exception_sends_500
def message_delete(self, req): def message_delete(self, req):
"""Delete a message from a queue """Delete a message from a queue
@ -478,7 +478,7 @@ class Endpoints(object):
return response.Response(req, body, headers) return response.Response(req, body, headers)
@api_utils.raises_conn_error @api_utils.on_exception_sends_500
def message_delete_many(self, req): def message_delete_many(self, req):
"""Deletes a set of messages from a queue """Deletes a set of messages from a queue
@ -511,7 +511,7 @@ class Endpoints(object):
elif pop_limit: elif pop_limit:
return self._pop_messages(req, queue_name, project_id, pop_limit) return self._pop_messages(req, queue_name, project_id, pop_limit)
@api_utils.raises_conn_error @api_utils.on_exception_sends_500
def _delete_messages_by_id(self, req, queue_name, ids, project_id): def _delete_messages_by_id(self, req, queue_name, ids, project_id):
self._message_controller.bulk_delete(queue_name, message_ids=ids, self._message_controller.bulk_delete(queue_name, message_ids=ids,
project=project_id) project=project_id)
@ -521,7 +521,7 @@ class Endpoints(object):
return response.Response(req, body, headers) return response.Response(req, body, headers)
@api_utils.raises_conn_error @api_utils.on_exception_sends_500
def _pop_messages(self, req, queue_name, project_id, pop_limit): def _pop_messages(self, req, queue_name, project_id, pop_limit):
LOG.debug(u'Pop messages - queue: %(queue)s, project: %(project)s', LOG.debug(u'Pop messages - queue: %(queue)s, project: %(project)s',
@ -542,7 +542,7 @@ class Endpoints(object):
return response.Response(req, body, headers) return response.Response(req, body, headers)
# Claims # Claims
@api_utils.raises_conn_error @api_utils.on_exception_sends_500
def claim_create(self, req): def claim_create(self, req):
"""Creates a claim """Creates a claim
@ -608,7 +608,7 @@ class Endpoints(object):
return response.Response(req, body, headers) return response.Response(req, body, headers)
@api_utils.raises_conn_error @api_utils.on_exception_sends_500
def claim_get(self, req): def claim_get(self, req):
"""Gets a claim """Gets a claim
@ -653,7 +653,7 @@ class Endpoints(object):
return response.Response(req, body, headers) return response.Response(req, body, headers)
@api_utils.raises_conn_error @api_utils.on_exception_sends_500
def claim_update(self, req): def claim_update(self, req):
"""Updates a claim """Updates a claim
@ -700,7 +700,7 @@ class Endpoints(object):
headers = {'status': 404} headers = {'status': 404}
return api_utils.error_response(req, ex, headers, error) return api_utils.error_response(req, ex, headers, error)
@api_utils.raises_conn_error @api_utils.on_exception_sends_500
def claim_delete(self, req): def claim_delete(self, req):
"""Deletes a claim """Deletes a claim
@ -729,7 +729,7 @@ class Endpoints(object):
return response.Response(req, body, headers) return response.Response(req, body, headers)
# Subscriptions # Subscriptions
@api_utils.raises_conn_error @api_utils.on_exception_sends_500
def subscription_list(self, req): def subscription_list(self, req):
"""List all subscriptions for a queue. """List all subscriptions for a queue.
@ -769,7 +769,7 @@ class Endpoints(object):
return response.Response(req, body, headers) return response.Response(req, body, headers)
@api_utils.raises_conn_error @api_utils.on_exception_sends_500
def subscription_create(self, req, subscriber): def subscription_create(self, req, subscriber):
"""Create a subscription for a queue. """Create a subscription for a queue.
@ -820,7 +820,7 @@ class Endpoints(object):
headers = {'status': 409} headers = {'status': 409}
return response.Response(req, body, headers) return response.Response(req, body, headers)
@api_utils.raises_conn_error @api_utils.on_exception_sends_500
def subscription_delete(self, req): def subscription_delete(self, req):
"""Delete a specific subscription by ID. """Delete a specific subscription by ID.
@ -852,7 +852,7 @@ class Endpoints(object):
headers = {'status': 204} headers = {'status': 204}
return response.Response(req, body, headers) return response.Response(req, body, headers)
@api_utils.raises_conn_error @api_utils.on_exception_sends_500
def subscription_get(self, req): def subscription_get(self, req):
"""Retrieve details about an existing subscription. """Retrieve details about an existing subscription.

View File

@ -179,8 +179,8 @@ def get_headers(req):
return kwargs return kwargs
def raises_conn_error(func): def on_exception_sends_500(func):
"""Handles generic Exceptions """Handles generic Exceptions in API endpoints
This decorator catches generic Exceptions and returns a generic This decorator catches generic Exceptions and returns a generic
Response. Response.
@ -194,7 +194,8 @@ def raises_conn_error(func):
LOG.exception(ex) LOG.exception(ex)
error = _("Unexpected error.") error = _("Unexpected error.")
headers = {'status': 500} headers = {'status': 500}
req = kwargs.get('req') # args[0] - Endpoints object, args[1] - Request object.
req = args[1]
return error_response(req, ex, headers, error) return error_response(req, ex, headers, error)
return wrapper return wrapper