Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
feat: handle JSONResponse errors like provisioned errors
Browse files Browse the repository at this point in the history
When handling provisioned errors, the same logic should be used as when
a JSONResponseError occurs.

Closes #744
  • Loading branch information
bbangert committed May 11, 2017
1 parent 2776922 commit 10287da
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 17 deletions.
7 changes: 4 additions & 3 deletions autopush/router/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ItemNotFound,
ProvisionedThroughputExceededException,
)
from boto.exception import JSONResponseError
from twisted.internet.threads import deferToThread
from twisted.internet.defer import (
inlineCallbacks,
Expand Down Expand Up @@ -107,8 +108,8 @@ def route_notification(self, notification, uaid_data):
if result is False:
self.metrics.increment("router.broadcast.miss")
returnValue(self.stored_response(notification))
except ProvisionedThroughputExceededException:
raise RouterException("Provisioned throughput error",
except (ProvisionedThroughputExceededException, JSONResponseError):
raise RouterException("Error saving to database",
status_code=503,
response_body="Retry Request",
errno=201)
Expand All @@ -124,7 +125,7 @@ def route_notification(self, notification, uaid_data):
# - Error (no client) : Done, return 404
try:
uaid_data = yield deferToThread(router.get_uaid, uaid)
except ProvisionedThroughputExceededException:
except (ProvisionedThroughputExceededException, JSONResponseError):
self.metrics.increment("router.broadcast.miss")
returnValue(self.stored_response(notification))
except ItemNotFound:
Expand Down
9 changes: 5 additions & 4 deletions autopush/tests/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -937,9 +937,12 @@ def check_result(msg):

return self._check_response(check_result)

def test_hello_jsonresponseerror_logged(self):
def test_hello_jsonresponseerror(self):
self._connect()

self.proto.randrange = Mock()
self.proto.randrange.return_value = 0.1

def throw_error(*args, **kwargs):
raise JSONResponseError(None, None)

Expand All @@ -950,9 +953,7 @@ def throw_error(*args, **kwargs):

def check_result(msg):
eq_(msg["status"], 503)
eq_(msg["reason"], "error")
self.proto.log.info.assert_called()
self.proto.log.failure.assert_not_called()
eq_(msg["reason"], "error - overloaded")
self.flushLoggedErrors()

return self._check_response(check_result)
Expand Down
4 changes: 2 additions & 2 deletions autopush/web/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from attr import attrs, attrib
from boto.dynamodb2.exceptions import ProvisionedThroughputExceededException
from boto.exception import BotoServerError
from boto.exception import BotoServerError, JSONResponseError
from marshmallow.schema import UnmarshalResult # noqa
from typing import Any, Callable # noqa
from twisted.internet.threads import deferToThread
Expand Down Expand Up @@ -236,7 +236,7 @@ def _overload_err(self, fail):

def _boto_err(self, fail):
"""errBack for random boto exceptions"""
fail.trap(BotoServerError)
fail.trap(BotoServerError, JSONResponseError)
self.log.info(format="BOTO Error: %s" % str(fail.value),
status_code=503, errno=202,
client_info=self._client_info)
Expand Down
12 changes: 4 additions & 8 deletions autopush/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,12 +445,8 @@ def base_tags(self):

def log_failure(self, failure, **kwargs):
"""Log a twisted failure out through twisted's log.failure"""
exc = failure.value
if isinstance(exc, JSONResponseError):
self.log.info("JSONResponseError: {exc}", exc=exc, **kwargs)
else:
self.log.failure(format="Unexpected error", failure=failure,
**kwargs)
self.log.failure(format="Unexpected error", failure=failure,
**kwargs)

@property
def paused(self):
Expand Down Expand Up @@ -724,7 +720,7 @@ def returnError(self, messageType, reason, statusCode, close=True,
self.sendClose()

def err_overload(self, failure, message_type, disconnect=True):
"""Handle database overloads
"""Handle database overloads and errors
If ``disconnect`` is False, the an overload error is returned and the
client is not disconnected.
Expand All @@ -738,7 +734,7 @@ def err_overload(self, failure, message_type, disconnect=True):
:param disconnect: Whether the client should be disconnected or not.
"""
failure.trap(ProvisionedThroughputExceededException)
failure.trap(ProvisionedThroughputExceededException, JSONResponseError)

if disconnect:
self.transport.pauseProducing()
Expand Down

0 comments on commit 10287da

Please sign in to comment.