-
Notifications
You must be signed in to change notification settings - Fork 13k
fix(core): decode byte-array encoded API error messages #19855
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
4a373f5
0412d47
e789c4b
a37c6c9
d776f0b
3a5689d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,15 @@ import { DEFAULT_GEMINI_FLASH_MODEL } from '../config/models.js'; | |
| import type { UserTierId } from '../code_assist/types.js'; | ||
| import { AuthType } from '../core/contentGenerator.js'; | ||
|
|
||
| function tryDecodeByteArray(s: string): string | null { | ||
| if (!/^\d+(,\d+)+$/.test(s)) return null; | ||
| try { | ||
| return String.fromCharCode(...s.split(',').map(Number)); | ||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| const RATE_LIMIT_ERROR_MESSAGE_USE_GEMINI = | ||
| '\nPlease wait and try again later. To increase your limits, request a quota increase through AI Studio, or switch to another /auth method'; | ||
| const RATE_LIMIT_ERROR_MESSAGE_VERTEX = | ||
|
|
@@ -40,6 +49,15 @@ export function parseAndFormatApiError( | |
| fallbackModel?: string, | ||
| ): string { | ||
| if (isStructuredError(error)) { | ||
| const decoded = tryDecodeByteArray(error.message); | ||
| if (decoded) | ||
| return parseAndFormatApiError( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed an edge case here. When the code successfully decodes Timeline of what happens if the API returns a 429 Error:
Actual Output: Expected Output: We should clone the error object here so we can preserve the original HTTP if (decoded) {
return parseAndFormatApiError(
{ ...error, message: decoded },
authType,
userTier,
currentModel,
fallbackModel,
); |
||
| decoded, | ||
| authType, | ||
| userTier, | ||
| currentModel, | ||
| fallbackModel, | ||
| ); | ||
Rohan-Mohite14 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let text = `[API Error: ${error.message}]`; | ||
| if (error.status === 429) { | ||
| text += getRateLimitMessage(authType, fallbackModel); | ||
|
|
@@ -49,9 +67,18 @@ export function parseAndFormatApiError( | |
|
|
||
| // The error message might be a string containing a JSON object. | ||
| if (typeof error === 'string') { | ||
| const decoded = tryDecodeByteArray(error); | ||
| if (decoded) | ||
| return parseAndFormatApiError( | ||
| decoded, | ||
| authType, | ||
| userTier, | ||
| currentModel, | ||
| fallbackModel, | ||
| ); | ||
Rohan-Mohite14 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const jsonStart = error.indexOf('{'); | ||
| if (jsonStart === -1) { | ||
| return `[API Error: ${error}]`; // Not a JSON error, return as is. | ||
| return `[API Error: ${error}]`; | ||
| } | ||
|
|
||
| const jsonString = error.substring(jsonStart); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.