diff --git a/lib/constants/error-messages.ts b/lib/constants/error-messages.ts index 490a325f0e8e6a..95daa453ee8ecb 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_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 31bec33b22c41e..ba40d4a3aa12fb 100644 --- a/lib/modules/platform/github/index.spec.ts +++ b/lib/modules/platform/github/index.spec.ts @@ -3,6 +3,8 @@ import * as httpMock from '../../../../test/http-mock'; import { logger, mocked, partial } from '../../../../test/util'; import { GlobalConfig } from '../../../config/global'; import { + PLATFORM_RATE_LIMIT_EXCEEDED, + PLATFORM_UNKNOWN_ERROR, REPOSITORY_CANNOT_FORK, REPOSITORY_NOT_FOUND, REPOSITORY_RENAMED, @@ -542,6 +544,40 @@ 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: '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`) + .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_RATE_LIMIT_EXCEEDED); + }); + 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..7f03bff81fc0c2 100644 --- a/lib/modules/platform/github/index.ts +++ b/lib/modules/platform/github/index.ts @@ -9,6 +9,8 @@ import semver from 'semver'; import { GlobalConfig } from '../../../config/global'; import { PLATFORM_INTEGRATION_UNAUTHORIZED, + PLATFORM_RATE_LIMIT_EXCEEDED, + PLATFORM_UNKNOWN_ERROR, REPOSITORY_ACCESS_FORBIDDEN, REPOSITORY_ARCHIVED, REPOSITORY_BLOCKED, @@ -394,6 +396,16 @@ export async function initRepo({ name: config.repositoryName, }, }); + + 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_UNKNOWN_ERROR); + } + repo = res?.data?.repository; // istanbul ignore if if (!repo) {