Skip to content

Commit 820a4e3

Browse files
authored
fix(core): better error message for failed cloudshell-gca auth (#26079)
1 parent 7d08f84 commit 820a4e3

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

packages/core/src/code_assist/server.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,28 @@ describe('CodeAssistServer', () => {
644644
);
645645
});
646646

647+
it('should throw friendly error for 403 on cloudshell-gca project', async () => {
648+
const { server } = createTestServer();
649+
const mock403Error = {
650+
response: {
651+
status: 403,
652+
data: {
653+
error: {
654+
message: 'Permission denied',
655+
},
656+
},
657+
},
658+
};
659+
vi.spyOn(server, 'requestPost').mockRejectedValue(mock403Error);
660+
661+
await expect(
662+
server.loadCodeAssist({
663+
cloudaicompanionProject: 'cloudshell-gca',
664+
metadata: {},
665+
}),
666+
).rejects.toThrow(/Access to the default Cloud Shell Gemini project/);
667+
});
668+
647669
it('should call the listExperiments endpoint with metadata', async () => {
648670
const { server } = createTestServer();
649671
const mockResponse = {

packages/core/src/code_assist/server.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,16 @@ export class CodeAssistServer implements ContentGenerator {
273273
return {
274274
currentTier: { id: UserTierId.STANDARD },
275275
};
276+
} else if (
277+
isPermissionDeniedError(e) &&
278+
req.cloudaicompanionProject === 'cloudshell-gca'
279+
) {
280+
throw new Error(
281+
'Access to the default Cloud Shell Gemini project was denied.\n' +
282+
'Please set your own Google Cloud project by running:\n' +
283+
'gcloud config set project [PROJECT_ID]\n' +
284+
'or setting export GOOGLE_CLOUD_PROJECT=...',
285+
);
276286
} else {
277287
throw e;
278288
}
@@ -572,3 +582,15 @@ function isVpcScAffectedUser(error: unknown): boolean {
572582
}
573583
return false;
574584
}
585+
586+
function isPermissionDeniedError(error: unknown): boolean {
587+
return (
588+
!!error &&
589+
typeof error === 'object' &&
590+
'response' in error &&
591+
!!error.response &&
592+
typeof error.response === 'object' &&
593+
'status' in error.response &&
594+
error.response.status === 403
595+
);
596+
}

0 commit comments

Comments
 (0)