From 01f4d360330c3f74182e0f61e1d4d2a356880059 Mon Sep 17 00:00:00 2001 From: RahulGautamSingh Date: Wed, 31 May 2023 07:33:23 +0530 Subject: [PATCH 1/3] handle unexpected github-graphql error --- lib/constants/error-messages.ts | 1 + lib/modules/platform/github/index.spec.ts | 18 ++++++++++++++++++ lib/modules/platform/github/index.ts | 7 +++++++ 3 files changed, 26 insertions(+) diff --git a/lib/constants/error-messages.ts b/lib/constants/error-messages.ts index 490a325f0e8e6a..2ef0f5be28414f 100644 --- a/lib/constants/error-messages.ts +++ b/lib/constants/error-messages.ts @@ -9,6 +9,7 @@ export const PLATFORM_GPG_FAILED = 'gpg-failed'; export const PLATFORM_INTEGRATION_UNAUTHORIZED = 'integration-unauthorized'; export const PLATFORM_NOT_FOUND = 'platform-not-found'; export const PLATFORM_RATE_LIMIT_EXCEEDED = 'rate-limit-exceeded'; +export const PLATFORM_UNKOWN_ERROR = 'rate-limit-exceeded'; // Config Error export const CONFIG_VALIDATION = 'config-validation'; diff --git a/lib/modules/platform/github/index.spec.ts b/lib/modules/platform/github/index.spec.ts index 31bec33b22c41e..e6b5245ec9eaa9 100644 --- a/lib/modules/platform/github/index.spec.ts +++ b/lib/modules/platform/github/index.spec.ts @@ -3,6 +3,7 @@ import * as httpMock from '../../../../test/http-mock'; import { logger, mocked, partial } from '../../../../test/util'; import { GlobalConfig } from '../../../config/global'; import { + PLATFORM_UNKOWN_ERROR, REPOSITORY_CANNOT_FORK, REPOSITORY_NOT_FOUND, REPOSITORY_RENAMED, @@ -542,6 +543,23 @@ describe('modules/platform/github/index', () => { ).rejects.toThrow(REPOSITORY_NOT_FOUND); }); + it('throws unexpected graphql errors', async () => { + httpMock + .scope(githubApiHost) + .post(`/graphql`) + .reply(200, { + errors: [ + { + type: 'RATE_LIMITED', + message: 'API rate limit exceeded for installation ID XXXXXXX.', + }, + ], + }); + await expect( + github.initRepo({ repository: 'some/repo' }) + ).rejects.toThrow(PLATFORM_UNKOWN_ERROR); + }); + it('should throw error if renamed', async () => { httpMock .scope(githubApiHost) diff --git a/lib/modules/platform/github/index.ts b/lib/modules/platform/github/index.ts index 6bd3ab403f5421..f562cdff5a26a3 100644 --- a/lib/modules/platform/github/index.ts +++ b/lib/modules/platform/github/index.ts @@ -9,6 +9,7 @@ import semver from 'semver'; import { GlobalConfig } from '../../../config/global'; import { PLATFORM_INTEGRATION_UNAUTHORIZED, + PLATFORM_UNKOWN_ERROR, REPOSITORY_ACCESS_FORBIDDEN, REPOSITORY_ARCHIVED, REPOSITORY_BLOCKED, @@ -394,6 +395,12 @@ export async function initRepo({ name: config.repositoryName, }, }); + + if (res?.errors) { + logger.debug({ res }, 'Unexpected Graph QL errors'); + throw new Error(PLATFORM_UNKOWN_ERROR); + } + repo = res?.data?.repository; // istanbul ignore if if (!repo) { From 6819c2298eced3ddeb83aa106b96fd77fb4633c5 Mon Sep 17 00:00:00 2001 From: RahulGautamSingh Date: Wed, 31 May 2023 10:24:06 +0530 Subject: [PATCH 2/3] Update lib/constants/error-messages.ts Co-authored-by: Rhys Arkins --- lib/constants/error-messages.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/constants/error-messages.ts b/lib/constants/error-messages.ts index 2ef0f5be28414f..95daa453ee8ecb 100644 --- a/lib/constants/error-messages.ts +++ b/lib/constants/error-messages.ts @@ -9,7 +9,7 @@ export const PLATFORM_GPG_FAILED = 'gpg-failed'; export const PLATFORM_INTEGRATION_UNAUTHORIZED = 'integration-unauthorized'; export const PLATFORM_NOT_FOUND = 'platform-not-found'; export const PLATFORM_RATE_LIMIT_EXCEEDED = 'rate-limit-exceeded'; -export const PLATFORM_UNKOWN_ERROR = 'rate-limit-exceeded'; +export const PLATFORM_UNKNOWN_ERROR = 'platform-unknown-error'; // Config Error export const CONFIG_VALIDATION = 'config-validation'; From da48ad51fbba0668736a3e77827ccecbddf036fd Mon Sep 17 00:00:00 2001 From: RahulGautamSingh Date: Wed, 31 May 2023 12:26:10 +0530 Subject: [PATCH 3/3] applu suggestions --- lib/constants/error-messages.ts | 2 +- lib/modules/platform/github/index.spec.ts | 22 ++++++++++++++++++++-- lib/modules/platform/github/index.ts | 9 +++++++-- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/constants/error-messages.ts b/lib/constants/error-messages.ts index 2ef0f5be28414f..95daa453ee8ecb 100644 --- a/lib/constants/error-messages.ts +++ b/lib/constants/error-messages.ts @@ -9,7 +9,7 @@ export const PLATFORM_GPG_FAILED = 'gpg-failed'; export const PLATFORM_INTEGRATION_UNAUTHORIZED = 'integration-unauthorized'; export const PLATFORM_NOT_FOUND = 'platform-not-found'; export const PLATFORM_RATE_LIMIT_EXCEEDED = 'rate-limit-exceeded'; -export const PLATFORM_UNKOWN_ERROR = 'rate-limit-exceeded'; +export const PLATFORM_UNKNOWN_ERROR = 'platform-unknown-error'; // Config Error export const CONFIG_VALIDATION = 'config-validation'; diff --git a/lib/modules/platform/github/index.spec.ts b/lib/modules/platform/github/index.spec.ts index e6b5245ec9eaa9..ba40d4a3aa12fb 100644 --- a/lib/modules/platform/github/index.spec.ts +++ b/lib/modules/platform/github/index.spec.ts @@ -3,7 +3,8 @@ import * as httpMock from '../../../../test/http-mock'; import { logger, mocked, partial } from '../../../../test/util'; import { GlobalConfig } from '../../../config/global'; import { - PLATFORM_UNKOWN_ERROR, + PLATFORM_RATE_LIMIT_EXCEEDED, + PLATFORM_UNKNOWN_ERROR, REPOSITORY_CANNOT_FORK, REPOSITORY_NOT_FOUND, REPOSITORY_RENAMED, @@ -544,6 +545,23 @@ describe('modules/platform/github/index', () => { }); it('throws unexpected graphql errors', async () => { + httpMock + .scope(githubApiHost) + .post(`/graphql`) + .reply(200, { + errors: [ + { + type: 'SOME_ERROR_TYPE', + message: 'Some error message', + }, + ], + }); + await expect( + github.initRepo({ repository: 'some/repo' }) + ).rejects.toThrow(PLATFORM_UNKNOWN_ERROR); + }); + + it('throws graphql rate limit error', async () => { httpMock .scope(githubApiHost) .post(`/graphql`) @@ -557,7 +575,7 @@ describe('modules/platform/github/index', () => { }); await expect( github.initRepo({ repository: 'some/repo' }) - ).rejects.toThrow(PLATFORM_UNKOWN_ERROR); + ).rejects.toThrow(PLATFORM_RATE_LIMIT_EXCEEDED); }); it('should throw error if renamed', async () => { diff --git a/lib/modules/platform/github/index.ts b/lib/modules/platform/github/index.ts index f562cdff5a26a3..7f03bff81fc0c2 100644 --- a/lib/modules/platform/github/index.ts +++ b/lib/modules/platform/github/index.ts @@ -9,7 +9,8 @@ import semver from 'semver'; import { GlobalConfig } from '../../../config/global'; import { PLATFORM_INTEGRATION_UNAUTHORIZED, - PLATFORM_UNKOWN_ERROR, + PLATFORM_RATE_LIMIT_EXCEEDED, + PLATFORM_UNKNOWN_ERROR, REPOSITORY_ACCESS_FORBIDDEN, REPOSITORY_ARCHIVED, REPOSITORY_BLOCKED, @@ -397,8 +398,12 @@ export async function initRepo({ }); if (res?.errors) { + if (res.errors.find((err) => err.type === 'RATE_LIMITED')) { + logger.debug({ res }, 'Graph QL rate limit exceeded.'); + throw new Error(PLATFORM_RATE_LIMIT_EXCEEDED); + } logger.debug({ res }, 'Unexpected Graph QL errors'); - throw new Error(PLATFORM_UNKOWN_ERROR); + throw new Error(PLATFORM_UNKNOWN_ERROR); } repo = res?.data?.repository;