예제 #1
0
    def test_plugin_error_handler_context_manager(self,
                                                  mock_iter_entry_points):

        mock_error_handler_instance = Mock()

        class MockErrorHandlerClass(IndexError):
            def __new__(cls):
                return mock_error_handler_instance

        mock_entry_point_error_handler = Mock()
        mock_entry_point_error_handler.configure_mock(
            **{"load.return_value": MockErrorHandlerClass})

        mock_iter_entry_points.configure_mock(
            **{"return_value": [mock_entry_point_error_handler]})

        error = IndexError()

        with GlobalErrorHandler():
            raise error

        with GlobalErrorHandler():
            pass

        # pylint: disable=protected-access
        mock_error_handler_instance._handle.assert_called_once_with(error)
예제 #2
0
    def test_plugin_error_handler(self, mock_iter_entry_points):
        class ZeroDivisionErrorHandler(ErrorHandler, ZeroDivisionError):
            # pylint: disable=arguments-differ

            _handle = Mock()

        class AssertionErrorHandler(ErrorHandler, AssertionError):
            # pylint: disable=arguments-differ

            _handle = Mock()

        mock_entry_point_zero_division_error_handler = Mock()
        mock_entry_point_zero_division_error_handler.configure_mock(
            **{"load.return_value": ZeroDivisionErrorHandler})
        mock_entry_point_assertion_error_handler = Mock()
        mock_entry_point_assertion_error_handler.configure_mock(
            **{"load.return_value": AssertionErrorHandler})

        mock_iter_entry_points.configure_mock(
            **{
                "return_value": [
                    mock_entry_point_zero_division_error_handler,
                    mock_entry_point_assertion_error_handler,
                ]
            })

        error = ZeroDivisionError()

        with GlobalErrorHandler():
            raise error

        # pylint: disable=protected-access
        ZeroDivisionErrorHandler._handle.assert_called_with(error)

        error = AssertionError()

        with GlobalErrorHandler():
            raise error

        AssertionErrorHandler._handle.assert_called_with(error)
예제 #3
0
    def test_error_in_handler(self, mock_iter_entry_points):
        class ErrorErrorHandler(ErrorHandler, ZeroDivisionError):
            # pylint: disable=arguments-differ

            def _handle(self, error: Exception):
                assert False

        mock_entry_point_error_error_handler = Mock()
        mock_entry_point_error_error_handler.configure_mock(
            **{"load.return_value": ErrorErrorHandler})

        mock_iter_entry_points.configure_mock(
            **{"return_value": [mock_entry_point_error_error_handler]})

        error = ZeroDivisionError()

        with self.assertLogs(logger, ERROR):
            with GlobalErrorHandler():
                raise error
예제 #4
0
    def test_default_error_handler(self):

        with self.assertLogs(logger, ERROR):
            with GlobalErrorHandler():
                raise Exception("some exception")
예제 #5
0
from opentelemetry.sdk.error_handler import GlobalErrorHandler

# ZeroDivisionError to be handled by ErrorHandler0
with GlobalErrorHandler():
    1 / 0

print()

# IndexError to be handled by ErrorHandler1
with GlobalErrorHandler():
    [1][2]

print()

# KeyError to be handled by ErrorHandler1
with GlobalErrorHandler():
    {1: 2}[2]

print()

# AssertionError to be handled by DefaultErrorHandler
with GlobalErrorHandler():
    assert False

print()

# No error raised
with GlobalErrorHandler():
    print("No error raised")
    def test_default_error_handler(self, mock_iter_entry_points):

        with self.assertLogs(logger, ERROR):
            with GlobalErrorHandler():
                raise Exception("some exception")