| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| """ |
| Kiro API error enhancement and user-friendly message formatting. |
| |
| This module provides a centralized system for enhancing cryptic Kiro API errors |
| with clear, actionable, user-friendly messages. |
| |
| Architecture: |
| - KiroErrorReason: Enum of known error reasons from Kiro API |
| - KiroErrorInfo: Structured information about an enhanced error |
| - enhance_kiro_error(): Analyzes error JSON and returns enhanced message |
| |
| Example: |
| >>> error_json = {"message": "Input is too long.", "reason": "CONTENT_LENGTH_EXCEEDS_THRESHOLD"} |
| >>> error_info = enhance_kiro_error(error_json) |
| >>> print(error_info.user_message) |
| "Model context limit reached. Conversation size exceeds model capacity." |
| """ |
|
|
| from dataclasses import dataclass |
| from enum import Enum |
| from typing import Dict, Any |
|
|
| from loguru import logger |
|
|
|
|
| @dataclass |
| class KiroErrorInfo: |
| """ |
| Structured information about a Kiro API error. |
| |
| Contains both the enhanced user-friendly message and the original |
| error details for logging and debugging. |
| |
| Attributes: |
| reason: Error reason code from Kiro API (as string, e.g. "CONTENT_LENGTH_EXCEEDS_THRESHOLD") |
| user_message: Enhanced, user-friendly message for end users |
| original_message: Original message from Kiro API (for logging) |
| """ |
| reason: str |
| user_message: str |
| original_message: str |
|
|
|
|
| def enhance_kiro_error(error_json: Dict[str, Any]) -> KiroErrorInfo: |
| """ |
| Enhances Kiro API error with user-friendly message. |
| |
| Takes raw error JSON from Kiro API and returns structured information |
| with enhanced, user-friendly messages that help users understand what |
| went wrong without technical jargon. |
| |
| Args: |
| error_json: Parsed JSON from Kiro API error response |
| Expected format: {"message": "...", "reason": "..."} |
| The "reason" field is optional. |
| |
| Returns: |
| KiroErrorInfo with enhanced message and original details |
| |
| Example: |
| >>> error_json = {"message": "Input is too long.", "reason": "CONTENT_LENGTH_EXCEEDS_THRESHOLD"} |
| >>> error_info = enhance_kiro_error(error_json) |
| >>> print(error_info.user_message) |
| "Model context limit reached. Conversation size exceeds model capacity." |
| >>> print(error_info.original_message) |
| "Input is too long." |
| |
| Example (unknown error): |
| >>> error_json = {"message": "Something went wrong.", "reason": "UNKNOWN_REASON"} |
| >>> error_info = enhance_kiro_error(error_json) |
| >>> print(error_info.user_message) |
| "Something went wrong. (reason: UNKNOWN_REASON)" |
| """ |
| |
| |
| original_message = error_json.get("message") |
| if original_message is None: |
| original_message = "Unknown error" |
| |
| reason = error_json.get("reason") |
| if reason is None: |
| reason = "UNKNOWN" |
| |
| |
| if reason == "CONTENT_LENGTH_EXCEEDS_THRESHOLD": |
| |
| user_message = "Model context limit reached. Conversation size exceeds model capacity." |
| |
| elif reason == "MONTHLY_REQUEST_COUNT": |
| |
| user_message = "Monthly request limit exceeded. Account has reached its monthly quota." |
| |
| |
| |
| |
| |
| |
| |
| else: |
| |
| |
| if "reason" in error_json and reason != "UNKNOWN": |
| user_message = f"{original_message} (reason: {reason})" |
| else: |
| user_message = original_message |
| |
| return KiroErrorInfo( |
| reason=reason, |
| user_message=user_message, |
| original_message=original_message |
| ) |
|
|