Skip to content

Commit

Permalink
fix(github): Error messages for empty repository result (#17645)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
zharinov and viceice committed Sep 6, 2022
1 parent 56c61c9 commit 3f30119
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 9 deletions.
46 changes: 45 additions & 1 deletion lib/modules/datasource/github-releases/cache/cache-base.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ describe('modules/datasource/github-releases/cache/cache-base', () => {
{
statusCode: 200,
headers: {},
body: { errors: [{ message: 'Ooops' }] },
body: { errors: [{} as never, { message: 'Ooops' }] },
},
];
const cache = new TestCache(http, { resetDeltaMinutes: 0 });
Expand All @@ -345,6 +345,50 @@ describe('modules/datasource/github-releases/cache/cache-base', () => {
expect(packageCache.set).not.toHaveBeenCalled();
});

it('throws for unknown graphql errors', async () => {
packageCache.get.mockResolvedValueOnce({
items: {},
createdAt: t3,
updatedAt: t3,
});
responses = [
{
statusCode: 200,
headers: {},
body: { errors: [] },
},
];
const cache = new TestCache(http, { resetDeltaMinutes: 0 });

await expect(cache.getItems({ packageName: 'foo/bar' })).rejects.toThrow(
'GitHub datasource cache: unknown GraphQL error'
);
expect(packageCache.get).toHaveBeenCalled();
expect(packageCache.set).not.toHaveBeenCalled();
});

it('throws for empty payload', async () => {
packageCache.get.mockResolvedValueOnce({
items: {},
createdAt: t3,
updatedAt: t3,
});
responses = [
{
statusCode: 200,
headers: {},
body: { data: { repository: { payload: null as never } } },
},
];
const cache = new TestCache(http, { resetDeltaMinutes: 0 });

await expect(cache.getItems({ packageName: 'foo/bar' })).rejects.toThrow(
'GitHub datasource cache: failed to obtain payload data'
);
expect(packageCache.get).toHaveBeenCalled();
expect(packageCache.set).not.toHaveBeenCalled();
});

it('shrinks for some of graphql errors', async () => {
packageCache.get.mockResolvedValueOnce({
items: {},
Expand Down
32 changes: 24 additions & 8 deletions lib/modules/datasource/github-releases/cache/cache-base.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import is from '@sindresorhus/is';
import { DateTime, DurationLikeObject } from 'luxon';
import { logger } from '../../../../logger';
import * as memCache from '../../../../util/cache/memory';
Expand Down Expand Up @@ -164,11 +165,11 @@ export abstract class AbstractGithubDatasourceCache<
*/
abstract coerceFetched(fetchedItem: FetchedItem): StoredItem | null;

private async query(
private async queryPayload(
baseUrl: string,
variables: GithubQueryParams,
options: GithubHttpOptions
): Promise<QueryResponse<FetchedItem> | Error> {
): Promise<QueryResponse<FetchedItem>['repository']['payload'] | Error> {
try {
const graphqlRes = await this.http.postJson<
GithubGraphqlResponse<QueryResponse<FetchedItem>>
Expand All @@ -179,7 +180,22 @@ export abstract class AbstractGithubDatasourceCache<
});
const { body } = graphqlRes;
const { data, errors } = body;
return data ?? new Error(errors?.[0]?.message);

if (errors) {
let [errorMessage] = errors
.map(({ message }) => message)
.filter(is.string);
errorMessage ??= 'GitHub datasource cache: unknown GraphQL error';
return new Error(errorMessage);
}

if (!data?.repository?.payload) {
return new Error(
'GitHub datasource cache: failed to obtain payload data'
);
}

return data.repository.payload;
} catch (err) {
return err;
}
Expand Down Expand Up @@ -282,12 +298,12 @@ export abstract class AbstractGithubDatasourceCache<
: this.maxPrefetchPages;
let stopIteration = false;
while (pagesRemained > 0 && !stopIteration) {
const res = await this.query(baseUrl, variables, {
const queryResult = await this.queryPayload(baseUrl, variables, {
repository: packageName,
});
if (res instanceof Error) {
if (queryResult instanceof Error) {
if (
res.message.startsWith(
queryResult.message.startsWith(
'Something went wrong while executing your query.' // #16343
) &&
variables.count > 30
Expand All @@ -299,15 +315,15 @@ export abstract class AbstractGithubDatasourceCache<
variables.count = Math.floor(variables.count / 2);
continue;
}
throw res;
throw queryResult;
}

pagesRemained -= 1;

const {
nodes: fetchedItems,
pageInfo: { hasNextPage, endCursor },
} = res.repository.payload;
} = queryResult;

if (hasNextPage) {
variables.cursor = endCursor;
Expand Down

0 comments on commit 3f30119

Please sign in to comment.