11from abc import ABC , abstractmethod
22from typing import Generic , Mapping , Optional , Type , TypeVar , Union
33
4+ from hyperbrowser .display_utils import normalize_display_text
45from hyperbrowser .exceptions import HyperbrowserError
56from hyperbrowser .mapping_utils import read_string_key_mapping
67
78T = TypeVar ("T" )
8- _TRUNCATED_DISPLAY_SUFFIX = "... (truncated)"
99_MAX_MODEL_NAME_DISPLAY_LENGTH = 120
1010_MAX_MAPPING_KEY_DISPLAY_LENGTH = 120
1111_MIN_HTTP_STATUS_CODE = 100
1212_MAX_HTTP_STATUS_CODE = 599
1313
1414
15- def _sanitize_display_text (value : str , * , max_length : int ) -> str :
16- try :
17- sanitized_value = "" .join (
18- "?" if ord (character ) < 32 or ord (character ) == 127 else character
19- for character in value
20- ).strip ()
21- if type (sanitized_value ) is not str :
22- return ""
23- if not sanitized_value :
24- return ""
25- if len (sanitized_value ) <= max_length :
26- return sanitized_value
27- available_length = max_length - len (_TRUNCATED_DISPLAY_SUFFIX )
28- if available_length <= 0 :
29- return _TRUNCATED_DISPLAY_SUFFIX
30- return f"{ sanitized_value [:available_length ]} { _TRUNCATED_DISPLAY_SUFFIX } "
31- except Exception :
32- return ""
33-
34-
3515def _safe_model_name (model : object ) -> str :
3616 try :
3717 model_name = getattr (model , "__name__" , "response model" )
@@ -40,7 +20,7 @@ def _safe_model_name(model: object) -> str:
4020 if type (model_name ) is not str :
4121 return "response model"
4222 try :
43- normalized_model_name = _sanitize_display_text (
23+ normalized_model_name = normalize_display_text (
4424 model_name , max_length = _MAX_MODEL_NAME_DISPLAY_LENGTH
4525 )
4626 except Exception :
@@ -51,7 +31,7 @@ def _safe_model_name(model: object) -> str:
5131
5232
5333def _format_mapping_key_for_error (key : str ) -> str :
54- normalized_key = _sanitize_display_text (
34+ normalized_key = normalize_display_text (
5535 key , max_length = _MAX_MAPPING_KEY_DISPLAY_LENGTH
5636 )
5737 if normalized_key :
0 commit comments