从响应体里提取用户可读的错误摘要。
(payload: Any)
| 66 | |
| 67 | |
| 68 | def _extract_error_summary(payload: Any) -> str: |
| 69 | """从响应体里提取用户可读的错误摘要。""" |
| 70 | if payload is None: |
| 71 | return "" |
| 72 | |
| 73 | if isinstance(payload, str): |
| 74 | raw = payload.strip() |
| 75 | if not raw: |
| 76 | return "" |
| 77 | try: |
| 78 | return _extract_error_summary(json.loads(raw)) |
| 79 | except Exception: |
| 80 | return _truncate_text(raw) |
| 81 | |
| 82 | if isinstance(payload, dict): |
| 83 | for key in ("error_summary", "error_message", "detail", "message"): |
| 84 | value = payload.get(key) |
| 85 | if isinstance(value, str) and value.strip(): |
| 86 | return _truncate_text(value) |
| 87 | |
| 88 | error_value = payload.get("error") |
| 89 | if isinstance(error_value, dict): |
| 90 | for key in ("message", "detail", "reason", "code"): |
| 91 | value = error_value.get(key) |
| 92 | if isinstance(value, str) and value.strip(): |
| 93 | return _truncate_text(value) |
| 94 | elif isinstance(error_value, str) and error_value.strip(): |
| 95 | return _truncate_text(error_value) |
| 96 | |
| 97 | for nested_key in ("response", "data"): |
| 98 | nested = payload.get(nested_key) |
| 99 | if isinstance(nested, (dict, list, str)): |
| 100 | summary = _extract_error_summary(nested) |
| 101 | if summary: |
| 102 | return summary |
| 103 | |
| 104 | return "" |
| 105 | |
| 106 | if isinstance(payload, list): |
| 107 | for item in payload: |
| 108 | summary = _extract_error_summary(item) |
| 109 | if summary: |
| 110 | return summary |
| 111 | return "" |
| 112 | |
| 113 | return _truncate_text(payload) |
| 114 | |
| 115 | |
| 116 | def _guess_client_hints_from_user_agent(user_agent: str) -> Dict[str, str]: |
no test coverage detected