예제 #1
0
    def test_unit__exception_handled__ok__exception_error_dict(self):
        class MyException(Exception):
            def __init__(self, *args, **kwargs):
                super().__init__(*args, **kwargs)
                self.error_dict = {}

        context = AgnosticContext(app=None)
        error_builder = MarshmallowDefaultErrorBuilder()
        wrapper = ExceptionHandlerControllerWrapper(
            MyException,
            context,
            error_builder=error_builder,
            http_code=HTTPStatus.INTERNAL_SERVER_ERROR,
            processor_factory=lambda schema_: MarshmallowProcessor(error_builder.get_schema()),
        )

        @wrapper.get_wrapper
        def func(foo):
            exc = MyException("We are testing")
            exc.error_detail = {"foo": "bar"}
            raise exc

        response = func(42)
        assert response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR
        assert {
            "message": "We are testing",
            "details": {"error_detail": {"foo": "bar"}},
            "code": None,
        } == json.loads(response.body)
예제 #2
0
    def test_unit__exception_handled__ok__nominal_case(self):
        context = AgnosticContext(app=None)
        error_builder = MarshmallowDefaultErrorBuilder()
        wrapper = ExceptionHandlerControllerWrapper(
            ZeroDivisionError,
            context,
            error_builder=error_builder,
            http_code=HTTPStatus.INTERNAL_SERVER_ERROR,
            processor_factory=lambda schema_: MarshmallowProcessor(error_builder.get_schema()),
        )

        @wrapper.get_wrapper
        def func(foo):
            raise ZeroDivisionError("We are testing")

        response = func(42)
        assert HTTPStatus.INTERNAL_SERVER_ERROR == response.status_code
        assert {
            "details": {"error_detail": {}},
            "message": "We are testing",
            "code": None,
        } == json.loads(response.body)