Skip to content

Commit 9b18091

Browse files
committed
Improve error handling
1 parent 0b9d890 commit 9b18091

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

index.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,20 @@ const githubToken = process.env.GH_TOKEN;
1010
const countAllCommits = process.env.ALL_COMMITS.toString() === 'true';
1111

1212
async function main() {
13-
const stats = await getStats();
14-
await updateGist(stats);
13+
if (!githubToken) {
14+
throw new Error('GH_TOKEN is not defined');
15+
}
16+
let stats;
17+
try {
18+
stats = await getStats();
19+
} catch (e) {
20+
throw new Error(`cannot retrieve statistics: ${e.message}`);
21+
}
22+
try {
23+
await updateGist(stats);
24+
} catch (e) {
25+
throw new Error(`cannot update gist: ${e.message}`);
26+
}
1527
}
1628

1729
async function getStats() {
@@ -86,8 +98,7 @@ async function updateGist(stats) {
8698
});
8799
}
88100

89-
(async () => {
90-
await main().catch((err) => {
91-
console.error(err);
92-
});
93-
})();
101+
main().catch((err) => {
102+
console.error(err.message);
103+
process.exit(1);
104+
});

0 commit comments

Comments
 (0)