create_error_response( 'CHATGPT_QUOTA_EXCEEDED', $http_code ); case 401: return $this->create_error_response( 'CHATGPT_INVALID_API_KEY', $http_code ); case 400: return $this->create_error_response( 'Bad request - check your input parameters', $http_code ); case 500: case 502: case 503: return $this->create_error_response( 'OpenAI server error - please try again later', $http_code ); default: return array( 'success' => false, 'error' => 'HTTP Error: ' . $http_code, 'http_code' => $http_code, 'response' => $response_body, ); } } /** * Handle API errors in JSON response. * * @param array $error_data Error data from API response. * @return array Error response. */ public function handle_api_error( array $error_data ): array { $error_msg = $error_data['message'] ?? 'Unknown API error'; if ( function_exists( 'accessibility_audio_tts_debug_log' ) ) { accessibility_audio_tts_debug_log( '[ChatGPT_Error_Handler] API error: ' . $error_msg, true ); } // Check for quota issues in error message. if ( strpos( $error_msg, 'quota' ) !== false || strpos( $error_msg, 'exceeded' ) !== false ) { return $this->create_error_response( 'CHATGPT_QUOTA_EXCEEDED' ); } // Check for rate limiting. if ( strpos( $error_msg, 'rate limit' ) !== false ) { return $this->create_error_response( 'CHATGPT_RATE_LIMITED' ); } // Check for invalid model. if ( strpos( $error_msg, 'model' ) !== false && strpos( $error_msg, 'does not exist' ) !== false ) { return $this->create_error_response( 'CHATGPT_INVALID_MODEL' ); } return $this->create_error_response( $error_msg ); } /** * Handle cURL errors. * * @param string $curl_error cURL error message. * @return array Error response. */ public function handle_curl_error( string $curl_error ): array { if ( function_exists( 'accessibility_audio_tts_debug_log' ) ) { accessibility_audio_tts_debug_log( '[ChatGPT_Error_Handler] cURL error: ' . $curl_error, true ); } return $this->create_error_response( 'cURL Error: ' . $curl_error ); } /** * Handle JSON parsing errors. * * @param string $json_error JSON error message. * @return array Error response. */ public function handle_json_error( string $json_error ): array { if ( function_exists( 'accessibility_audio_tts_debug_log' ) ) { accessibility_audio_tts_debug_log( '[ChatGPT_Error_Handler] JSON parse error: ' . $json_error, true ); } return $this->create_error_response( 'Invalid JSON response: ' . $json_error ); } /** * Create standardized error response. * * @param string $error_message Error message. * @param int|null $http_code HTTP status code if applicable. * @return array Standardized error response. */ private function create_error_response( string $error_message, ?int $http_code = null ): array { $response = array( 'success' => false, 'error' => $error_message, ); if ( null !== $http_code ) { $response['http_code'] = $http_code; } return $response; } /** * Check if error is recoverable. * * @param string $error_code Error code. * @return bool True if error might be recoverable with retry. */ public function is_recoverable_error( string $error_code ): bool { $recoverable_errors = array( 'CHATGPT_RATE_LIMITED', 'OpenAI server error - please try again later', ); return in_array( $error_code, $recoverable_errors, true ); } /** * Get user-friendly error message. * * @param string $error_code Error code. * @return string User-friendly error message. */ public function get_user_friendly_message( string $error_code ): string { $messages = array( 'CHATGPT_QUOTA_EXCEEDED' => __( 'ChatGPT API quota exceeded. Please check your OpenAI billing.', 'accessibility-audio-tts-text-to-speech-for-articles' ), 'CHATGPT_INVALID_API_KEY' => __( 'Invalid ChatGPT API key. Please check your settings.', 'accessibility-audio-tts-text-to-speech-for-articles' ), 'CHATGPT_RATE_LIMITED' => __( 'ChatGPT API rate limit reached. Please try again later.', 'accessibility-audio-tts-text-to-speech-for-articles' ), 'CHATGPT_INVALID_MODEL' => __( 'Selected ChatGPT model is not available. Please choose a different model.', 'accessibility-audio-tts-text-to-speech-for-articles' ), ); return $messages[ $error_code ] ?? $error_code; } }