Skip to content

Commit f8812e4

Browse files
committed
refactor: simplify error handling in AwsBedrockHandler and update tests for throttling errors
1 parent 5d6c74d commit f8812e4

File tree

3 files changed

+25
-26
lines changed

3 files changed

+25
-26
lines changed

packages/types/src/provider-settings.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ const bedrockSchema = apiModelIdProviderModelSchema.extend({
102102
awsModelContextWindow: z.number().optional(),
103103
awsBedrockEndpointEnabled: z.boolean().optional(),
104104
awsBedrockEndpoint: z.string().optional(),
105-
awsBedrockVerboseErrors: z.boolean().optional(),
106105
})
107106

108107
const vertexSchema = apiModelIdProviderModelSchema.extend({

src/api/providers/__tests__/bedrock-error-handling.spec.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ describe("AwsBedrockHandler Error Handling", () => {
163163
}
164164
})
165165

166-
it("should display verbose error information including codes and metadata", async () => {
167-
const verboseError = createMockError({
166+
it("should display appropriate error information for throttling errors", async () => {
167+
const throttlingError = createMockError({
168168
message: "Bedrock is unable to process your request",
169169
name: "ThrottlingException",
170170
status: 429,
@@ -176,21 +176,14 @@ describe("AwsBedrockHandler Error Handling", () => {
176176
},
177177
})
178178

179-
mockSend.mockRejectedValueOnce(verboseError)
179+
mockSend.mockRejectedValueOnce(throttlingError)
180180

181181
try {
182182
await handler.completePrompt("test")
183183
throw new Error("Expected error to be thrown")
184184
} catch (error) {
185-
// Should contain error codes
186-
expect(error.message).toMatch(/\[.*HTTP 429.*AWS ThrottlingException.*\]/)
187185
// Should contain the main error message
188186
expect(error.message).toContain("throttled or rate limited")
189-
// Should contain debug information
190-
expect(error.message).toContain("Debug Info:")
191-
expect(error.message).toContain("Request ID: 12345-abcde-67890")
192-
expect(error.message).toContain("Extended Request ID: extended-12345")
193-
expect(error.message).toContain("CloudFront ID: cf-12345")
194187
}
195188
})
196189
})
@@ -406,12 +399,8 @@ describe("AwsBedrockHandler Error Handling", () => {
406399
await handler.completePrompt("test")
407400
throw new Error("Expected error to be thrown")
408401
} catch (error) {
409-
// Should contain error codes (note: completePrompt adds "Bedrock completion error: " prefix)
410-
expect(error.message).toContain("[HTTP 429")
411402
// Should contain the verbose message template
412403
expect(error.message).toContain("Request was throttled or rate limited")
413-
// Should contain debug information
414-
expect(error.message).toContain("Request ID: test-request-id-12345")
415404
// Should preserve original error properties
416405
expect((error as any).status).toBe(429)
417406
expect((error as any).$metadata.requestId).toBe("test-request-id-12345")
@@ -450,9 +439,6 @@ describe("AwsBedrockHandler Error Handling", () => {
450439
} catch (error) {
451440
// Should contain error codes (note: this will be caught by the non-throttling error path)
452441
expect(error.message).toContain("Too many tokens")
453-
// Should contain debug information
454-
expect(error.message).toContain("Request ID: token-error-id-67890")
455-
expect(error.message).toContain("Extended Request ID: extended-12345")
456442
// Should preserve original error properties
457443
expect(error.name).toBe("ValidationException")
458444
expect((error as any).$metadata.requestId).toBe("token-error-id-67890")

src/api/providers/bedrock.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
583583
yield chunk as any // Cast to any to bypass type checking since we know the structure is correct
584584
}
585585

586-
// Re-throw with enhanced error message so retry system shows verbose details
586+
// Re-throw with enhanced error message for retry system
587587
const enhancedErrorMessage = this.formatErrorMessage(error, this.getErrorType(error), true)
588588
if (error instanceof Error) {
589589
const enhancedError = new Error(enhancedErrorMessage)
@@ -670,7 +670,7 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
670670
// Since we're in a non-streaming context, we know the result is a string
671671
const errorMessage = errorResult as string
672672

673-
// Create enhanced error with verbose details for retry system
673+
// Create enhanced error for retry system
674674
const enhancedError = new Error(errorMessage)
675675
if (error instanceof Error) {
676676
// Preserve important properties from the original error
@@ -1089,7 +1089,7 @@ Please verify:
10891089
"throttl",
10901090
"rate",
10911091
"limit",
1092-
"bedrock is unable to process your request", // AWS Bedrock specific message
1092+
"bedrock is unable to process your request", // AWS Bedrock specific throttling message
10931093
"please wait",
10941094
"quota exceeded",
10951095
"service unavailable",
@@ -1123,11 +1123,13 @@ Suggestions:
11231123
2. Split your request into smaller chunks
11241124
3. Use a model with a larger context window
11251125
4. If rate limited, reduce request frequency
1126-
5. Check your Amazon Bedrock quotas and limits`,
1126+
5. Check your Amazon Bedrock quotas and limits
1127+
1128+
{formattedErrorDetails}`,
11271129
logLevel: "error",
11281130
},
11291131
SERVICE_QUOTA_EXCEEDED: {
1130-
patterns: ["service quota exceeded", "quota exceeded", "exceeded quota", "limit exceeded"],
1132+
patterns: ["service quota exceeded", "service quota", "quota exceeded for model"],
11311133
messageTemplate: `Service quota exceeded. This error indicates you've reached AWS service limits.
11321134
11331135
Please try:
@@ -1235,9 +1237,21 @@ Please check:
12351237
const errorMessage = error.message.toLowerCase()
12361238
const errorName = error.name.toLowerCase()
12371239

1238-
// Check each error type's patterns
1239-
for (const [errorType, definition] of Object.entries(AwsBedrockHandler.ERROR_TYPES)) {
1240-
if (errorType === "GENERIC") continue // Skip the generic type
1240+
// Check each error type's patterns in order of specificity (most specific first)
1241+
const errorTypeOrder = [
1242+
"SERVICE_QUOTA_EXCEEDED", // Most specific - check before THROTTLING
1243+
"MODEL_NOT_READY",
1244+
"TOO_MANY_TOKENS",
1245+
"INTERNAL_SERVER_ERROR",
1246+
"ON_DEMAND_NOT_SUPPORTED",
1247+
"NOT_FOUND",
1248+
"ACCESS_DENIED",
1249+
"THROTTLING", // Less specific - check after more specific patterns
1250+
]
1251+
1252+
for (const errorType of errorTypeOrder) {
1253+
const definition = AwsBedrockHandler.ERROR_TYPES[errorType]
1254+
if (!definition) continue
12411255

12421256
// If any pattern matches in either message or name, return this error type
12431257
if (definition.patterns.some((pattern) => errorMessage.includes(pattern) || errorName.includes(pattern))) {

0 commit comments

Comments
 (0)