Skip to content
This repository was archived by the owner on Jan 24, 2023. It is now read-only.

Commit 1b19b84

Browse files
authored
Catch failed API fetch (#32)
If `fetch()` fails, fallback to getting PR metadata from document. Fixes #31
1 parent bba6abe commit 1b19b84

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

content.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,22 @@ async function getPrMetadataFromDocument() {
8989
}
9090

9191
async function getPrMetadataFromApi(repo, prNumber) {
92-
const response = await fetch(`https://api.github.com/repos/${repo}/pulls/${prNumber}`);
93-
if (!response.ok) {
92+
try {
93+
const response = await fetch(`https://api.github.com/repos/${repo}/pulls/${prNumber}`);
94+
if (!response.ok) {
95+
return null;
96+
}
97+
98+
const prMetadata = await response.json();
99+
return {
100+
// These could be null in some cases, so replace nulls with empty strings.
101+
title: prMetadata.title || '',
102+
body: prMetadata.body || '',
103+
};
104+
} catch (error) {
105+
warn('failed to fetch PR metadata from API: ' + error);
94106
return null;
95107
}
96-
97-
const prMetadata = await response.json();
98-
return {
99-
// These could be null in some cases, so replace nulls with empty strings.
100-
title: prMetadata.title || '',
101-
body: prMetadata.body || '',
102-
};
103108
}
104109

105110
async function makePrDescriptionAppear() {

0 commit comments

Comments
 (0)