Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a mechanism to automatically retry API requests without the x-goog-user-project header when a 403 USER_PROJECT_DENIED or serviceusage.services.use permission error is encountered, and subsequently omits the header for the rest of the session. It also adds comprehensive unit tests to verify this fallback behavior. The review feedback suggests extracting the inline magic strings "USER_PROJECT_DENIED" and "serviceusage.services.use" in isUserProjectDeniedError into top-level constants to align with the repository's style guide.
| /** | ||
| * Returns true if an HTTP 403 response indicates the caller lacks | ||
| * `serviceusage.services.use` on the `x-goog-user-project` quota project. | ||
| */ | ||
| export function isUserProjectDeniedError(status: number, body: unknown): boolean { | ||
| if (status !== 403 || !body) { | ||
| return false; | ||
| } | ||
| if (typeof body === "string") { | ||
| return body.includes("serviceusage.services.use") || body.includes("USER_PROJECT_DENIED"); | ||
| } | ||
| if (typeof body !== "object") { | ||
| return false; | ||
| } | ||
| const record = body as Record<string, unknown>; | ||
| const errObj = | ||
| record.error && typeof record.error === "object" | ||
| ? (record.error as Record<string, unknown>) | ||
| : record; | ||
| if (Array.isArray(errObj.details)) { | ||
| for (const detail of errObj.details) { | ||
| if ( | ||
| detail && | ||
| typeof detail === "object" && | ||
| (detail as Record<string, unknown>).reason === "USER_PROJECT_DENIED" | ||
| ) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| if (typeof errObj.message === "string") { | ||
| return ( | ||
| errObj.message.includes("serviceusage.services.use") || | ||
| errObj.message.includes("USER_PROJECT_DENIED") | ||
| ); | ||
| } | ||
| return false; | ||
| } |
There was a problem hiding this comment.
According to the repository style guide (GEMINI.md), static IDs, default timeouts, millisecond offsets, and repeated strings should be defined as top-level constants (UPPER_SNAKE_CASE) instead of inlining magic values inside functions.
Extracting "USER_PROJECT_DENIED" and "serviceusage.services.use" as top-level constants will improve maintainability and prevent duplication.
const USER_PROJECT_DENIED_REASON = "USER_PROJECT_DENIED";
const SERVICE_USAGE_PERMISSION = "serviceusage.services.use";
/**
* Returns true if an HTTP 403 response indicates the caller lacks
* `serviceusage.services.use` on the `x-goog-user-project` quota project.
*/
export function isUserProjectDeniedError(status: number, body: unknown): boolean {
if (status !== 403 || !body) {
return false;
}
if (typeof body === "string") {
return body.includes(SERVICE_USAGE_PERMISSION) || body.includes(USER_PROJECT_DENIED_REASON);
}
if (typeof body !== "object") {
return false;
}
const record = body as Record<string, unknown>;
const errObj =
record.error && typeof record.error === "object"
? (record.error as Record<string, unknown>)
: record;
if (Array.isArray(errObj.details)) {
for (const detail of errObj.details) {
if (
detail &&
typeof detail === "object" &&
(detail as Record<string, unknown>).reason === USER_PROJECT_DENIED_REASON
) {
return true;
}
}
}
if (typeof errObj.message === "string") {
return (
errObj.message.includes(SERVICE_USAGE_PERMISSION) ||
errObj.message.includes(USER_PROJECT_DENIED_REASON)
);
}
return false;
}References
- Define static IDs, default timeouts, millisecond offsets, and repeated strings as top-level constants (UPPER_SNAKE_CASE) instead of inlining magic values inside functions. (link)
Description
Follow-up to #11646.
When
apiv2automatically attachesx-goog-user-projectfromdefaultQuotaProject, callers who lackserviceusage.services.useon that project receive a403 PERMISSION_DENIED(USER_PROJECT_DENIED) from Service Infrastructure even when they would previously have succeeded using the CLI's default OAuth project quota.This PR adds a targeted fallback in
src/apiv2.ts:x-goog-user-projectheader came fromdefaultQuotaProject(leaving explicitGOOGLE_CLOUD_QUOTA_PROJECTenv overrides and explicit caller headers untouched).403whose response indicatesUSER_PROJECT_DENIED(via structuredErrorInfo.reason === "USER_PROJECT_DENIED"or a message mentioningserviceusage.services.use/USER_PROJECT_DENIED), records the project in a session-scopeddeniedQuotaProjectsset and immediately retries replayable requests once withoutx-goog-user-project.x-goog-user-projectupfront without incurring another403round-trip.403permission errors untouched.Scenarios Tested
eslint --quietandprettier --checkonsrc/apiv2.tsandsrc/apiv2.spec.ts: passednpx mocha src/apiv2.spec.ts --grep "defaultQuotaProject|quota|x-goog-user-project|USER_PROJECT_DENIED|serviceusage|unrelated 403": 15 passingSample Commands
N/A