Skip to content
This repository has been archived by the owner on Jun 11, 2018. It is now read-only.

Commit

Permalink
Make sure exception handler can handle stdlib exceptions (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
ticosax authored and beniwohli committed Jun 30, 2017
1 parent d027e6e commit e2456f6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
14 changes: 9 additions & 5 deletions opbeat/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import opbeat
from opbeat.conf import defaults
from opbeat.traces import RequestsStore
from opbeat.transport.base import TransportException
from opbeat.utils import opbeat_json as json
from opbeat.utils import is_master_process, six, stacks, varmap
from opbeat.utils.compat import atexit_register, urlparse
Expand Down Expand Up @@ -417,7 +418,7 @@ def _get_log_message(self, data):
# decode message so we can show the actual event
try:
data = self.decode(data)
except:
except Exception:
message = '<failed decoding data>'
else:
message = data.pop('message', '<no message value>')
Expand Down Expand Up @@ -603,13 +604,16 @@ def handle_transport_success(self, **kwargs):
self.logger.info('Logged error at ' + kwargs['url'])
self.state.set_success()

def handle_transport_fail(self, **kwargs):
def handle_transport_fail(self, exception=None, **kwargs):
"""
Failure handler called by the transport
"""
exception = kwargs.get('exception')
message = self._get_log_message(exception.data)
self.error_logger.error(exception.args[0])
if isinstance(exception, TransportException):
message = self._get_log_message(exception.data)
self.error_logger.error(exception.args[0])
else:
# stdlib exception
message = str(exception)
self.error_logger.error(
'Failed to submit message: %r',
message,
Expand Down
28 changes: 28 additions & 0 deletions tests/client/client_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,34 @@ def test_send_remote_failover_sync(self, should_try, http_send):
client.send_remote('http://example.com/api/store', 'foo')
assert client.state.status == client.state.ONLINE

@mock.patch('opbeat.transport.http_urllib3.Urllib3Transport.send')
@mock.patch('opbeat.base.ClientState.should_try')
def test_send_remote_failover_sync_stdlib(self, should_try, http_send):
should_try.return_value = True

client = Client(
servers=['http://example.com'],
organization_id='organization_id',
app_id='app_id',
secret_token='secret',
async_mode=False,
)
logger = mock.Mock()
client.error_logger.error = logger

# test error
encoded_data = client.encode({'message': 'oh no'})
http_send.side_effect = ValueError('oopsie')
client.send_remote('http://example.com/api/store', data=encoded_data)
assert client.state.status == client.state.ERROR
assert len(logger.call_args_list) == 1
assert 'oopsie' in logger.call_args_list[0][0][1]

# test recovery
http_send.side_effect = None
client.send_remote('http://example.com/api/store', 'foo')
assert client.state.status == client.state.ONLINE

@mock.patch('opbeat.transport.http_urllib3.Urllib3Transport.send')
@mock.patch('opbeat.base.ClientState.should_try')
def test_send_remote_failover_async(self, should_try, http_send):
Expand Down

0 comments on commit e2456f6

Please sign in to comment.