Skip to content

Commit

Permalink
fix(platform): handle unexpected github-graphql error (#22512)
Browse files Browse the repository at this point in the history
Co-authored-by: Rhys Arkins <rhys@arkins.net>
  • Loading branch information
RahulGautamSingh and rarkins committed May 31, 2023
1 parent 71d082d commit 04f4875
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/constants/error-messages.ts
Expand Up @@ -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';
Expand Down
36 changes: 36 additions & 0 deletions lib/modules/platform/github/index.spec.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions lib/modules/platform/github/index.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 04f4875

Please sign in to comment.