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