@@ -65,6 +65,42 @@ def __call__(self, **kwargs):
6565 raise RuntimeError ("call failed" )
6666
6767
68+ class _LongControlNameCallableModel :
69+ __name__ = " Model\t " + ("x" * 200 )
70+
71+ def __call__ (self , ** kwargs ):
72+ _ = kwargs
73+ raise RuntimeError ("call failed" )
74+
75+
76+ class _BrokenBlankKeyValueMapping (Mapping [str , object ]):
77+ def __iter__ (self ):
78+ return iter ([" " ])
79+
80+ def __len__ (self ) -> int :
81+ return 1
82+
83+ def __getitem__ (self , key : str ) -> object :
84+ if key == " " :
85+ raise RuntimeError ("cannot read blank key value" )
86+ raise KeyError (key )
87+
88+
89+ class _BrokenLongKeyValueMapping (Mapping [str , object ]):
90+ _KEY = "bad\t " + ("k" * 200 )
91+
92+ def __iter__ (self ):
93+ return iter ([self ._KEY ])
94+
95+ def __len__ (self ) -> int :
96+ return 1
97+
98+ def __getitem__ (self , key : str ) -> object :
99+ if key == self ._KEY :
100+ raise RuntimeError ("cannot read long key value" )
101+ raise KeyError (key )
102+
103+
68104def test_api_response_from_json_parses_model_data () -> None :
69105 response = APIResponse .from_json (
70106 {"name" : "job-1" , "retries" : 2 }, _SampleResponseModel
@@ -159,6 +195,39 @@ def test_api_response_from_json_uses_default_name_for_blank_model_name() -> None
159195 )
160196
161197
198+ def test_api_response_from_json_sanitizes_and_truncates_model_name_in_errors () -> None :
199+ with pytest .raises (
200+ HyperbrowserError ,
201+ match = r"Failed to parse response data for Model\?x+\.\.\. \(truncated\)" ,
202+ ):
203+ APIResponse .from_json (
204+ {"name" : "job-1" },
205+ cast ("type[_SampleResponseModel]" , _LongControlNameCallableModel ()),
206+ )
207+
208+
209+ def test_api_response_from_json_uses_placeholder_for_blank_mapping_key_in_errors () -> None :
210+ with pytest .raises (
211+ HyperbrowserError ,
212+ match = (
213+ "Failed to parse response data for _SampleResponseModel: "
214+ "unable to read value for key '<blank key>'"
215+ ),
216+ ):
217+ APIResponse .from_json (_BrokenBlankKeyValueMapping (), _SampleResponseModel )
218+
219+
220+ def test_api_response_from_json_sanitizes_and_truncates_mapping_keys_in_errors () -> None :
221+ with pytest .raises (
222+ HyperbrowserError ,
223+ match = (
224+ "Failed to parse response data for _SampleResponseModel: "
225+ r"unable to read value for key 'bad\?k+\.\.\. \(truncated\)'"
226+ ),
227+ ):
228+ APIResponse .from_json (_BrokenLongKeyValueMapping (), _SampleResponseModel )
229+
230+
162231def test_api_response_from_json_preserves_hyperbrowser_errors () -> None :
163232 with pytest .raises (HyperbrowserError , match = "model validation failed" ) as exc_info :
164233 APIResponse .from_json ({}, _RaisesHyperbrowserModel )
0 commit comments