( error: unknown, modelName: string, )
| 82 | } |
| 83 | |
| 84 | function handleValidationError( |
| 85 | error: unknown, |
| 86 | modelName: string, |
| 87 | ): { valid: boolean; error: string } { |
| 88 | // NotFoundError (404) means the model doesn't exist |
| 89 | if (error instanceof NotFoundError) { |
| 90 | const fallback = get3PFallbackSuggestion(modelName) |
| 91 | const suggestion = fallback ? `. Try '${fallback}' instead` : '' |
| 92 | return { |
| 93 | valid: false, |
| 94 | error: `Model '${modelName}' not found${suggestion}`, |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // For other API errors, provide context-specific messages |
| 99 | if (error instanceof APIError) { |
| 100 | if (error instanceof AuthenticationError) { |
| 101 | return { |
| 102 | valid: false, |
| 103 | error: 'Authentication failed. Please check your API credentials.', |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if (error instanceof APIConnectionError) { |
| 108 | return { |
| 109 | valid: false, |
| 110 | error: 'Network error. Please check your internet connection.', |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // Check error body for model-specific errors |
| 115 | const errorBody = error.error as unknown |
| 116 | if ( |
| 117 | errorBody && |
| 118 | typeof errorBody === 'object' && |
| 119 | 'type' in errorBody && |
| 120 | errorBody.type === 'not_found_error' && |
| 121 | 'message' in errorBody && |
| 122 | typeof errorBody.message === 'string' && |
| 123 | errorBody.message.includes('model:') |
| 124 | ) { |
| 125 | return { valid: false, error: `Model '${modelName}' not found` } |
| 126 | } |
| 127 | |
| 128 | // Generic API error |
| 129 | return { valid: false, error: `API error: ${error.message}` } |
| 130 | } |
| 131 | |
| 132 | // For unknown errors, be safe and reject |
| 133 | const errorMessage = error instanceof Error ? error.message : String(error) |
| 134 | return { |
| 135 | valid: false, |
| 136 | error: `Unable to validate model: ${errorMessage}`, |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // @[MODEL LAUNCH]: Add a fallback suggestion chain for the new model → previous version |
| 141 | /** |
no test coverage detected