Skip to content

Improve error parsing for billing errors#10639

Draft
falahat wants to merge 5 commits into
mainfrom
improve_error_parsing
Draft

Improve error parsing for billing errors#10639
falahat wants to merge 5 commits into
mainfrom
improve_error_parsing

Conversation

@falahat

@falahat falahat commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Description

Scenarios Tested

Sample Commands

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the API client to gracefully handle non-JSON error responses (status >= 400) by falling back to the raw response text instead of throwing a JSON parsing error, and adds a corresponding unit test. The reviewer suggested wrapping the raw text in a standard error object structure ({ error: { message: text } }) to prevent potential runtime TypeErrors for callers expecting an object structure.

Comment thread src/apiv2.ts
Comment on lines +480 to +484
if (res.status >= 400) {
body = text as unknown as ResT;
} else {
throw new FirebaseError(`Unable to parse JSON: ${err}`);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When resolveOnHTTPError is true, the caller expects the returned body to be an object (matching the generic ResT type). If we assign the raw non-JSON text string directly to body, any caller attempting to access properties on it (e.g., body.error.message) will encounter a runtime TypeError (e.g., Cannot read properties of undefined).

To prevent this and maintain type safety/compatibility, we can wrap the raw text in a standard error object structure: { error: { message: text } }. This structure is fully compatible with responseToError (which expects this shape or a string) and ensures that callers using resolveOnHTTPError: true can safely inspect the error message without throwing runtime exceptions.

Suggested change
if (res.status >= 400) {
body = text as unknown as ResT;
} else {
throw new FirebaseError(`Unable to parse JSON: ${err}`);
}
if (res.status >= 400) {
body = { error: { message: text } } as unknown as ResT;
} else {
throw new FirebaseError(`Unable to parse JSON: ${err}`);
}

@falahat falahat changed the title Improve error parsing when the response is not in the requested JSON format Improve error parsing for billing errors Jun 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants