@@ -821,10 +821,10 @@ async def test_handle_deserialize_raises_exception(
821
821
with pytest .raises (HTTPException ):
822
822
await handler .handle (get_test_request ())
823
823
824
+ get_content_type_from_headers_mock .assert_called_once_with (get_test_headers ())
824
825
deserialize_exception_mock .assert_called_once_with (
825
826
_TEST_INPUT , _APPLICATION_JSON
826
827
)
827
- get_content_type_from_headers_mock .assert_called_once_with (get_test_headers ())
828
828
assert not predictor_mock ().preprocess .called
829
829
assert not predictor_mock ().predict .called
830
830
assert not predictor_mock ().postprocess .called
@@ -845,25 +845,66 @@ async def test_handle_predictor_raises_exception(
845
845
handler = PredictionHandler (
846
846
_TEST_GCS_ARTIFACTS_URI , predictor = get_test_predictor ()
847
847
)
848
+ expected_message = (
849
+ "The following exception has occurred: Exception. Arguments: ()."
850
+ )
848
851
849
852
with mock .patch .multiple (
850
853
handler ._predictor ,
851
854
preprocess = preprocess_mock ,
852
855
predict = predict_mock ,
853
856
postprocess = postprocess_mock ,
854
857
):
855
- with pytest .raises (Exception ) :
858
+ with pytest .raises (HTTPException ) as exception :
856
859
await handler .handle (get_test_request ())
857
860
858
- deserialize_mock .assert_called_once_with (_TEST_INPUT , _APPLICATION_JSON )
859
- get_content_type_from_headers_mock .assert_called_once_with (
860
- get_test_headers ()
861
- )
862
- preprocess_mock .assert_called_once_with (_TEST_DESERIALIZED_INPUT )
863
- predict_mock .assert_called_once_with (_TEST_DESERIALIZED_INPUT )
864
- assert not postprocess_mock .called
865
- assert not get_accept_from_headers_mock .called
866
- assert not serialize_mock .called
861
+ assert exception .value .status_code == 500
862
+ assert exception .value .detail == expected_message
863
+ get_content_type_from_headers_mock .assert_called_once_with (get_test_headers ())
864
+ deserialize_mock .assert_called_once_with (_TEST_INPUT , _APPLICATION_JSON )
865
+ preprocess_mock .assert_called_once_with (_TEST_DESERIALIZED_INPUT )
866
+ predict_mock .assert_called_once_with (_TEST_DESERIALIZED_INPUT )
867
+ assert not postprocess_mock .called
868
+ assert not get_accept_from_headers_mock .called
869
+ assert not serialize_mock .called
870
+
871
+ @pytest .mark .asyncio
872
+ async def test_handle_predictor_raises_http_exception (
873
+ self ,
874
+ get_content_type_from_headers_mock ,
875
+ deserialize_mock ,
876
+ get_accept_from_headers_mock ,
877
+ serialize_mock ,
878
+ ):
879
+ status_code = 400
880
+ expected_message = "This is an user error."
881
+ preprocess_mock = mock .MagicMock (return_value = _TEST_DESERIALIZED_INPUT )
882
+ predict_mock = mock .MagicMock (
883
+ side_effect = HTTPException (status_code = status_code , detail = expected_message )
884
+ )
885
+ postprocess_mock = mock .MagicMock (return_value = _TEST_SERIALIZED_OUTPUT )
886
+ handler = PredictionHandler (
887
+ _TEST_GCS_ARTIFACTS_URI , predictor = get_test_predictor ()
888
+ )
889
+
890
+ with mock .patch .multiple (
891
+ handler ._predictor ,
892
+ preprocess = preprocess_mock ,
893
+ predict = predict_mock ,
894
+ postprocess = postprocess_mock ,
895
+ ):
896
+ with pytest .raises (HTTPException ) as exception :
897
+ await handler .handle (get_test_request ())
898
+
899
+ assert exception .value .status_code == status_code
900
+ assert exception .value .detail == expected_message
901
+ get_content_type_from_headers_mock .assert_called_once_with (get_test_headers ())
902
+ deserialize_mock .assert_called_once_with (_TEST_INPUT , _APPLICATION_JSON )
903
+ preprocess_mock .assert_called_once_with (_TEST_DESERIALIZED_INPUT )
904
+ predict_mock .assert_called_once_with (_TEST_DESERIALIZED_INPUT )
905
+ assert not postprocess_mock .called
906
+ assert not get_accept_from_headers_mock .called
907
+ assert not serialize_mock .called
867
908
868
909
@pytest .mark .asyncio
869
910
async def test_handle_serialize_raises_exception (
@@ -3173,20 +3214,46 @@ def test_predict_thorws_http_exception(
3173
3214
assert response .status_code == 400
3174
3215
assert json .loads (response .content )["detail" ] == expected_message
3175
3216
3176
- def test_predict_thorws_exceptions_not_http_exception (
3217
+ def test_predict_thorws_exceptions_not_http_exception_default_handler (
3177
3218
self , model_server_env_mock , importlib_import_module_mock_twice
3178
3219
):
3179
3220
expected_message = (
3180
3221
"An exception ValueError occurred. Arguments: ('Not a correct value.',)."
3181
3222
)
3182
3223
model_server = CprModelServer ()
3224
+ model_server .is_default_handler = True
3183
3225
client = TestClient (model_server .app )
3184
3226
3185
3227
with mock .patch .object (model_server .handler , "handle" ) as handle_mock :
3186
3228
handle_mock .side_effect = ValueError ("Not a correct value." )
3187
3229
3188
3230
response = client .post (_TEST_AIP_PREDICT_ROUTE , json = {"x" : [1 ]})
3189
3231
3232
+ assert (
3233
+ prediction .CUSTOM_PREDICTION_ROUTINES_SERVER_ERROR_HEADER_KEY
3234
+ in response .headers
3235
+ )
3236
+ assert response .status_code == 500
3237
+ assert json .loads (response .content )["detail" ] == expected_message
3238
+
3239
+ def test_predict_thorws_exceptions_not_http_exception_not_default_handler (
3240
+ self , model_server_env_mock , importlib_import_module_mock_twice
3241
+ ):
3242
+ expected_message = (
3243
+ "An exception ValueError occurred. Arguments: ('Not a correct value.',)."
3244
+ )
3245
+ model_server = CprModelServer ()
3246
+ client = TestClient (model_server .app )
3247
+
3248
+ with mock .patch .object (model_server .handler , "handle" ) as handle_mock :
3249
+ handle_mock .side_effect = ValueError ("Not a correct value." )
3250
+
3251
+ response = client .post (_TEST_AIP_PREDICT_ROUTE , json = {"x" : [1 ]})
3252
+
3253
+ assert (
3254
+ prediction .CUSTOM_PREDICTION_ROUTINES_SERVER_ERROR_HEADER_KEY
3255
+ not in response .headers
3256
+ )
3190
3257
assert response .status_code == 500
3191
3258
assert json .loads (response .content )["detail" ] == expected_message
3192
3259
0 commit comments