Skip to content

Commit d4b8ad8

Browse files
bluwyAndarist
andauthored
Improve error messages when fetching from GitHub api (#1781)
* Improve error messages when fetching github api * Fix lint * tweak --------- Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
1 parent ece0376 commit d4b8ad8

2 files changed

Lines changed: 28 additions & 11 deletions

File tree

.changeset/heavy-lions-stick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@changesets/get-github-info": patch
3+
---
4+
5+
Improve error messages when fail to fetch data from GitHub

packages/get-github-info/src/index.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,31 @@ const GHDataLoader = new DataLoader(async (requests: RequestData[]) => {
101101
repos[repo].push(data);
102102
});
103103

104-
const data = await fetch(GITHUB_GRAPHQL_URL, {
105-
method: "POST",
106-
headers: {
107-
Authorization: `Token ${GITHUB_TOKEN}`,
108-
},
109-
body: JSON.stringify({ query: makeQuery(repos) }),
110-
}).then((x: any) => x.json());
104+
let fetchResponse;
105+
try {
106+
fetchResponse = await fetch(GITHUB_GRAPHQL_URL, {
107+
method: "POST",
108+
headers: {
109+
Authorization: `Token ${GITHUB_TOKEN}`,
110+
},
111+
body: JSON.stringify({ query: makeQuery(repos) }),
112+
});
113+
} catch (e: any) {
114+
throw new Error(
115+
`An error occurred when fetching data from GitHub\n${e.message}`
116+
);
117+
}
118+
119+
let data;
120+
try {
121+
data = await fetchResponse.json();
122+
} catch (e: any) {
123+
throw new Error(`Failed to parse data from GitHub\n${e.message}`);
124+
}
111125

112126
if (data.errors) {
113127
throw new Error(
114-
`An error occurred when fetching data from GitHub\n${JSON.stringify(
128+
`Fetched data from GitHub returned errors\n${JSON.stringify(
115129
data.errors,
116130
null,
117131
2
@@ -122,9 +136,7 @@ const GHDataLoader = new DataLoader(async (requests: RequestData[]) => {
122136
// this is mainly for the case where there's an authentication problem
123137
if (!data.data) {
124138
throw new Error(
125-
`An error occurred when fetching data from GitHub\n${JSON.stringify(
126-
data
127-
)}`
139+
`Fetched data from GitHub has missing data\n${JSON.stringify(data)}`
128140
);
129141
}
130142

0 commit comments

Comments
 (0)