Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(platform): handle unexpected github-graphql error #22512

Merged
merged 5 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/constants/error-messages.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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