def test_map_exceptions(): with pytest.raises(APIException) as api_exception_1: with map_exceptions({TemporaryException: 'ERROR_TEMPORARY'}): raise TemporaryException assert api_exception_1.value.detail['error'] == 'ERROR_TEMPORARY' assert api_exception_1.value.detail['detail'] == '' assert api_exception_1.value.status_code == status.HTTP_400_BAD_REQUEST with pytest.raises(APIException) as api_exception_2: with map_exceptions({ TemporaryException: ('ERROR_TEMPORARY_2', HTTP_404_NOT_FOUND, 'Another message') }): raise TemporaryException assert api_exception_2.value.detail['error'] == 'ERROR_TEMPORARY_2' assert api_exception_2.value.detail['detail'] == 'Another message' assert api_exception_2.value.status_code == status.HTTP_404_NOT_FOUND with pytest.raises(TemporaryException2): with map_exceptions({TemporaryException: 'ERROR_TEMPORARY_3'}): raise TemporaryException2 with map_exceptions({TemporaryException: 'ERROR_TEMPORARY_4'}): pass
def test_map_exceptions(): with pytest.raises(APIException) as api_exception_1: with map_exceptions({TemporaryException: "ERROR_TEMPORARY"}): raise TemporaryException assert api_exception_1.value.detail["error"] == "ERROR_TEMPORARY" assert api_exception_1.value.detail["detail"] == "" assert api_exception_1.value.status_code == status.HTTP_400_BAD_REQUEST with pytest.raises(APIException) as api_exception_2: with map_exceptions({ TemporaryException: ( "ERROR_TEMPORARY_2", HTTP_404_NOT_FOUND, "Another message {e.message}", ) }): e = TemporaryException() e.message = "test" raise e assert api_exception_2.value.detail["error"] == "ERROR_TEMPORARY_2" assert api_exception_2.value.detail["detail"] == "Another message test" assert api_exception_2.value.status_code == status.HTTP_404_NOT_FOUND with pytest.raises(TemporaryException2): with map_exceptions({TemporaryException: "ERROR_TEMPORARY_3"}): raise TemporaryException2 with map_exceptions({TemporaryException: "ERROR_TEMPORARY_4"}): pass
def map_api_exceptions(self): """ The map_api_exceptions method can be used to map uncaught exceptions to certain api error responses. These API exceptions should be defined in the api_exceptions_map. """ with map_exceptions(self.api_exceptions_map): yield