{"record":{"id":"51d31fb18236a12d","repo":"amruthpillai/reactive-resume","slug":"bad-gateway-51d31f","errorCode":"BAD_GATEWAY","errorMessage":"Could not reach the AI provider.","messagePattern":"Could not reach the AI provider\\.","errorType":"http","errorClass":"ORPCError","httpStatus":502,"severity":"error","filePath":"packages/api/src/features/ai/router.ts","lineNumber":28,"sourceCode":"import { aiProvidersService } from \"../ai-providers/service\";\nimport { resumeService } from \"../resume/service\";\nimport { aiService, fileInputSchema } from \"./service\";\n\nfunction isInvalidAiBaseUrlError(error: unknown): boolean {\n\treturn error instanceof Error && error.message === \"INVALID_AI_BASE_URL\";\n}\n\nfunction isAiProviderGatewayError(error: unknown): boolean {\n\treturn error instanceof AISDKError;\n}\n\nfunction isCredentialEncryptionUnavailable(error: unknown): boolean {\n\treturn error instanceof Error && error.message === \"AI_CREDENTIAL_ENCRYPTION_UNAVAILABLE\";\n}\n\n/** Throws a BAD_GATEWAY ORPCError, preserving the original cause for upstream error reporters. */\nfunction throwAiProviderGatewayError(cause?: unknown): never {\n\tthrow new ORPCError(\"BAD_GATEWAY\", { message: \"Could not reach the AI provider.\", cause });\n}\n\nfunction throwAiProviderConfigError(): never {\n\tthrow new ORPCError(\"BAD_REQUEST\", { message: \"Invalid AI provider configuration.\" });\n}\n\nfunction throwCredentialEncryptionUnavailable(): never {\n\tthrow new ORPCError(\"PRECONDITION_FAILED\", {\n\t\tmessage: \"AI providers are unavailable because ENCRYPTION_SECRET is not configured.\",\n\t});\n}\n\nfunction throwResumeStructureError(error: ZodError): never {\n\tthrow new ORPCError(\"BAD_REQUEST\", {\n\t\tmessage: \"Invalid resume data structure\",\n\t\tcause: flattenError(error),\n\t});\n}","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/amruthpillai/reactive-resume/blob/3a5b12e2a40374a9571988701fcb75c5a1831c42/packages/api/src/features/ai/router.ts#L10-L46","documentation":"BAD_GATEWAY thrown by the throwAiProviderGatewayError helper in the AI feature router. It is invoked from each AI handler's catch block when the error is recognized as an AISDKError (isAiProviderGatewayError), i.e. the AI SDK itself surfaced a provider/network/runtime error during generateText. The original error is preserved as cause for server-side reporters.","triggerScenarios":"Any AI feature (parsePdf, analyze, etc.) where the underlying model call throws an instance of AISDKError — e.g. authentication error, rate limit, model not found, upstream timeout, or invalid response shape from the provider.","commonSituations":"Provider returns 401/403 (bad key), 429 (rate limit), 404 (wrong model id), 5xx; transient network blips; provider deprecating a model name; region restrictions on the API key.","solutions":["Check server logs for the preserved cause (AISDKError) to identify the exact upstream status.","Validate the API key and model id against the provider's current API.","Retry on transient 429/5xx with backoff; for 401/403 rotate the key and retest the provider.","Confirm the chosen model is still available and not deprecated by the provider."],"exampleFix":"// before\nprovider: { provider: 'openai', model: 'gpt-4-deprecated', apiKey: 'sk-...expired' }\n\n// after\nprovider: { provider: 'openai', model: 'gpt-4o', apiKey: 'sk-...valid' }","handlingStrategy":"try-catch","validationCode":"async function verifyProviderBeforeCall(provider) {\n  const reachable = await canReachProvider(provider.baseURL, provider.apiKey);\n  if (!reachable) throw new Error('provider unreachable');\n}","typeGuard":"function isAiSdkGatewayError(e) {\n  return e?.code === 'BAD_GATEWAY' && /Could not reach the AI provider/i.test(e.message);\n}","tryCatchPattern":"try {\n  await ai.parsePdf(input);\n} catch (e) {\n  if (e?.code === 'BAD_GATEWAY') {\n    // inspect server logs for cause; retry with backoff for transient upstream errors\n    await backoffRetry(() => ai.parsePdf(input));\n  } else throw e;\n}","preventionTips":["Keep provider test status green; re-test after key rotation.","Use models the provider currently supports.","Implement exponential backoff for 429/5xx upstream errors."],"tags":["ai","upstream","network","ai-sdk","bad-gateway"],"backgroundTag":null,"analyzedSha":"3a5b12e2a40374a9571988701fcb75c5a1831c42","analyzedAt":"2026-08-12T22:31:22.666Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}