Skip to content

Commit

Permalink
Report endpoint errors without shutting down Tribler
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
ichorid committed Dec 13, 2021
1 parent 0e7fba3 commit 95be163
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ def unhandled_error_observer(self, _, context):
try:
SentryReporter.ignore_logger(self.logger.name)

should_stop = True
context = context.copy()
should_stop = context.pop('should_stop', True)
message = context.pop('message', 'no message')
exception = context.pop('exception', None) or self._create_exception_from(message)
# Exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ async def test_unhandled_error_observer_store_unreported_error(exception_handler
assert exception_handler.unreported_error


async def test_unhandled_error_observer_false_should_stop(exception_handler):
# Test passing negative value for should_stop flag through the context dict
context = {'message': 'Any', 'should_stop': False}
exception_handler.unhandled_error_observer(None, context)
assert exception_handler.unreported_error.should_stop is False


async def test_unhandled_error_observer_ignored(exception_handler):
# test that exception from list IGNORED_ERRORS_BY_CODE never sends to the GUI
context = {'exception': OSError(113, '')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from apispec.core import VALID_METHODS_OPENAPI_V2

from tribler_core.components.reporter.exception_handler import default_core_exception_handler
from tribler_core.components.restapi.rest.rest_endpoint import (
HTTP_INTERNAL_SERVER_ERROR,
HTTP_NOT_FOUND,
Expand Down Expand Up @@ -55,6 +56,9 @@ async def error_middleware(request, handler):
except Exception as e:
logger.exception(e)
full_exception = traceback.format_exc()

default_core_exception_handler.unhandled_error_observer(None, {'exception': e, 'should_stop': False})

return RESTResponse({"error": {
"handled": False,
"code": e.__class__.__name__,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import shutil
from unittest.mock import patch

import pytest

Expand Down Expand Up @@ -81,11 +82,17 @@ async def test_api_key_fail(rest_manager, api_port):
@pytest.mark.asyncio
async def test_unhandled_exception(rest_manager, api_port):
"""
Testing whether the API returns a formatted 500 error if an unhandled Exception is raised
Testing whether the API returns a formatted 500 error and
calls exception handler if an unhandled Exception is raised
"""
response_dict = await do_real_request(api_port, 'settings', expected_code=500,
post_data={'general': 'invalid schema'},
request_type='POST')
with patch('tribler_core.components.restapi.rest.rest_manager.default_core_exception_handler') as handler:
response_dict = await do_real_request(api_port, 'settings', expected_code=500,
post_data={'general': 'invalid schema'},
request_type='POST')
handler.unhandled_error_observer.assert_called_once()
exception_dict = handler.unhandled_error_observer.call_args.args[1]
assert exception_dict['should_stop'] is False
assert isinstance(exception_dict['exception'], TypeError)
assert response_dict
assert not response_dict['error']['handled']
assert response_dict['error']['code'] == "TypeError"

0 comments on commit 95be163

Please sign in to comment.