Skip to content

Commit 454d17c

Browse files
committed
refactor(backend-common/GithubUrlReader): only call fetch inside fetchResponse
Signed-off-by: secustor <sebastian@poxhofer.at>
1 parent b9ed162 commit 454d17c

File tree

2 files changed

+28
-32
lines changed

2 files changed

+28
-32
lines changed

.changeset/eight-rice-cross.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@backstage/backend-common': patch
3+
---
4+
5+
Do not call fetch directly but rather use fetchResponse facility

packages/backend-common/src/reading/GithubUrlReader.ts

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class GithubUrlReader implements UrlReader {
107107

108108
let response: Response;
109109
try {
110-
response = await fetch(ghUrl, {
110+
response = await this.fetchResponse(ghUrl, {
111111
headers: {
112112
...credentials?.headers,
113113
...(options?.etag && { 'If-None-Match': options.etag }),
@@ -125,38 +125,13 @@ export class GithubUrlReader implements UrlReader {
125125
signal: options?.signal as any,
126126
});
127127
} catch (e) {
128-
throw new Error(`Unable to read ${url}, ${e}`);
128+
throw e;
129129
}
130130

131-
if (response.status === 304) {
132-
throw new NotModifiedError();
133-
}
134-
135-
if (response.ok) {
136-
return ReadUrlResponseFactory.fromNodeJSReadable(response.body, {
137-
etag: response.headers.get('ETag') ?? undefined,
138-
lastModifiedAt: parseLastModified(
139-
response.headers.get('Last-Modified'),
140-
),
141-
});
142-
}
143-
144-
let message = `${url} could not be read as ${ghUrl}, ${response.status} ${response.statusText}`;
145-
if (response.status === 404) {
146-
throw new NotFoundError(message);
147-
}
148-
149-
// GitHub returns a 403 response with a couple of headers indicating rate
150-
// limit status. See more in the GitHub docs:
151-
// https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting
152-
if (
153-
response.status === 403 &&
154-
response.headers.get('X-RateLimit-Remaining') === '0'
155-
) {
156-
message += ' (rate limit exceeded)';
157-
}
158-
159-
throw new Error(message);
131+
return ReadUrlResponseFactory.fromNodeJSReadable(response.body, {
132+
etag: response.headers.get('ETag') ?? undefined,
133+
lastModifiedAt: parseLastModified(response.headers.get('Last-Modified')),
134+
});
160135
}
161136

162137
async readTree(
@@ -350,10 +325,26 @@ export class GithubUrlReader implements UrlReader {
350325
const response = await fetch(urlAsString, init);
351326

352327
if (!response.ok) {
353-
const message = `Request failed for ${urlAsString}, ${response.status} ${response.statusText}`;
328+
let message = `Request failed for ${urlAsString}, ${response.status} ${response.statusText}`;
329+
330+
if (response.status === 304) {
331+
throw new NotModifiedError();
332+
}
333+
354334
if (response.status === 404) {
355335
throw new NotFoundError(message);
356336
}
337+
338+
// GitHub returns a 403 response with a couple of headers indicating rate
339+
// limit status. See more in the GitHub docs:
340+
// https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting
341+
if (
342+
response.status === 403 &&
343+
response.headers.get('X-RateLimit-Remaining') === '0'
344+
) {
345+
message += ' (rate limit exceeded)';
346+
}
347+
357348
throw new Error(message);
358349
}
359350

0 commit comments

Comments
 (0)