(error: unknown)
| 138 | * Returns safe, generic messages to prevent leaking internal details. |
| 139 | */ |
| 140 | export function categorizeError(error: unknown): { message: string; status: number } { |
| 141 | if (!(error instanceof Error)) { |
| 142 | return { message: 'Unknown error occurred', status: 500 } |
| 143 | } |
| 144 | |
| 145 | // Typed dispatch first — our own classes carry definitive intent. |
| 146 | if (error instanceof McpOauthAuthorizationRequiredError || error instanceof UnauthorizedError) { |
| 147 | return { message: 'Authentication required', status: 401 } |
| 148 | } |
| 149 | if (error instanceof McpConnectionError) { |
| 150 | if (error.message.toLowerCase().includes('cooldown')) { |
| 151 | return { message: 'Server temporarily unavailable', status: 503 } |
| 152 | } |
| 153 | return { message: 'Connection failed', status: 502 } |
| 154 | } |
| 155 | |
| 156 | // Fall back to substring matching for SDK / third-party errors we don't |
| 157 | // own a typed class for. |
| 158 | const msg = error.message.toLowerCase() |
| 159 | |
| 160 | if (msg.includes('timeout')) { |
| 161 | return { message: 'Request timed out', status: 408 } |
| 162 | } |
| 163 | if (msg.includes('cooldown')) { |
| 164 | return { message: 'Server temporarily unavailable', status: 503 } |
| 165 | } |
| 166 | if (msg.includes('not found') || msg.includes('not accessible')) { |
| 167 | return { message: 'Resource not found', status: 404 } |
| 168 | } |
| 169 | if (msg.includes('authentication') || msg.includes('unauthorized')) { |
| 170 | return { message: 'Authentication required', status: 401 } |
| 171 | } |
| 172 | if (msg.includes('invalid') || msg.includes('missing required') || msg.includes('validation')) { |
| 173 | return { message: 'Invalid request parameters', status: 400 } |
| 174 | } |
| 175 | return { message: 'Internal server error', status: 500 } |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Create standardized MCP tool ID from server ID and tool name |
no outgoing calls
no test coverage detected