An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
fix(errors): classify a malformed provider request instead of a generic 400 (#953)
Yoshiki Miurada5427ff75cb
diff --git a/lib/errors/__tests__/public-error.test.ts b/lib/errors/__tests__/public-error.test.tsindex bf6c628c..ae1f5026 100644--- a/lib/errors/__tests__/public-error.test.ts+++ b/lib/errors/__tests__/public-error.test.ts@@ -159,6 +159,98 @@ describe('public error mapping', () => {expect(payload.error).not.toContain('144000')})+ it('maps malformed 400 request messages to actionable copy', () => {+ const error = Object.assign(+ new Error(+ 'Invalid body: failed to parse JSON value. Please check the value to ensure it is valid JSON. (Common errors include trailing commas, missing closing brackets, missing quotation marks, etc.)'+ ),+ { statusCode: 400 }+ )+ const payload = toPublicErrorPayload(error)++ expect(payload.code).toBe('malformed_request')+ expect(payload.error).toBe(+ 'This conversation could not be sent to the model. Please start a new chat.'+ )+ expect(payload.details).toBe(+ 'Retrying this message will fail the same way.'+ )+ expect(payload.retryable).toBe(false)+ })++ it('maps malformed 400 provider codes to actionable copy', () => {+ const error = Object.assign(new Error('Request failed'), {+ statusCode: 400,+ code: 'invalid_json'+ })+ const payload = toPublicErrorPayload(error)++ expect(payload.code).toBe('malformed_request')+ expect(payload.error).toBe(+ 'This conversation could not be sent to the model. Please start a new chat.'+ )+ expect(payload.details).toBe(+ 'Retrying this message will fail the same way.'+ )+ expect(payload.retryable).toBe(false)+ })++ it('keeps unrelated 400 errors on the bad request path', () => {+ const error = Object.assign(new Error('Request failed validation'), {+ statusCode: 400+ })+ const payload = toPublicErrorPayload(error)++ expect(payload.code).toBe('bad_request')+ })++ it('survives the serialize and reparse round trip the client performs', () => {+ const serialized = serializePublicError(+ Object.assign(new Error('Invalid body: failed to parse JSON value.'), {+ statusCode: 400+ })+ )+ const payload = toPublicErrorPayload(new Error(serialized))++ expect(payload.code).toBe('malformed_request')+ expect(payload.error).toBe(+ 'This conversation could not be sent to the model. Please start a new chat.'+ )+ expect(payload.details).toBe(+ 'Retrying this message will fail the same way.'+ )+ expect(payload.retryable).toBe(false)+ })++ it('does not honour a malformed_request code carrying foreign copy', () => {+ const payload = toPublicErrorPayload({+ error: 'Upstream said something we should not show verbatim.',+ code: 'malformed_request'+ })++ expect(payload.error).not.toBe(+ 'Upstream said something we should not show verbatim.'+ )+ })++ it('keeps upstream "Invalid JSON response" fetch failures on the bad request path', () => {+ const payload = toPublicErrorPayload({+ error: 'Invalid JSON response',+ status: 400+ })++ expect(payload.code).toBe('bad_request')+ })++ it('does not map malformed request messages for non-400 errors', () => {+ const error = Object.assign(new Error('failed to parse JSON value'), {+ statusCode: 500+ })+ const payload = toPublicErrorPayload(error)++ expect(payload.code).not.toBe('malformed_request')+ })+it('serializes public payloads for stream error chunks', () => {const serialized = serializePublicError(new Error('Redis timeout while saving stream results')diff --git a/lib/errors/public-error.ts b/lib/errors/public-error.tsindex 1f2ed3a9..45ea1969 100644--- a/lib/errors/public-error.ts+++ b/lib/errors/public-error.ts@@ -7,6 +7,7 @@ export type PublicErrorCode =| 'bad_request'| 'context_length'| 'forbidden'+ | 'malformed_request'| 'model_unavailable'| 'provider_auth'| 'provider_billing'@@ -45,11 +46,18 @@ type ErrorSnapshot = {const DEFAULT_PUBLIC_ERROR_MESSAGE ='We could not generate a response. Please try again.'+const MALFORMED_REQUEST_MESSAGE =+ 'This conversation could not be sent to the model. Please start a new chat.'++const MALFORMED_REQUEST_DETAILS =+ 'Retrying this message will fail the same way.'+const PUBLIC_ERROR_CODES: ReadonlySet<string> = new Set(['auth_required','bad_request','context_length','forbidden',+ 'malformed_request','model_unavailable','provider_auth','provider_billing',@@ -114,6 +122,13 @@ const CONTEXT_LENGTH_PATTERNS = [/too many tokens/i]+// Matched against the provider's own signature only: the prose form+// "Invalid JSON response" is an upstream-fetch message, not this failure.+const MALFORMED_REQUEST_PATTERNS = [+ /failed to parse JSON/i,+ /\binvalid_json\b/i+]+const MODEL_UNAVAILABLE_PATTERNS = [/model .*not found/i,/no such model/i,@@ -344,6 +359,16 @@ function classifyError(snapshot: ErrorSnapshot, fallbackMessage?: string) {}}+ if (status === 400 && matchesAny(combined, MALFORMED_REQUEST_PATTERNS)) {+ return {+ code: 'malformed_request' as const,+ type: 'general' as const,+ error: MALFORMED_REQUEST_MESSAGE,+ details: MALFORMED_REQUEST_DETAILS,+ retryable: false+ }+ }+if (status === 400) {return {code: 'bad_request' as const,@@ -385,6 +410,20 @@ function isToolFailurePayload(message: string, code: PublicErrorCode): boolean {return code === 'tool_failed' && isToolFailureMessage(message)}+/**+ * The client re-parses the serialized payload, and that second pass carries+ * neither the provider status nor its signature, so the copy would fall back to+ * the generic retry message while the details still said retrying is futile.+ * Only this module's own copy is honoured, for the same provenance reason as+ * `isToolFailurePayload`.+ */+function isMalformedRequestPayload(+ message: string,+ code: PublicErrorCode+): boolean {+ return code === 'malformed_request' && message === MALFORMED_REQUEST_MESSAGE+}+function isIntentionalPublicPayload(value: Record<string, unknown>,code: PublicErrorCode@@ -418,7 +457,8 @@ function fromParsedPayload(const type = asPublicErrorType(value.type) ?? typeForCode(code)const preserveMessage =isIntentionalPublicPayload(value, code) ||- isToolFailurePayload(message, code)+ isToolFailurePayload(message, code) ||+ isMalformedRequestPayload(message, code)return {error: preserveMessage ? message : classification.error,