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

Commit 1d6a4ab

Browse files
committed
🐛 Error Handling (throw error when not 200)
1 parent 8e8aec8 commit 1d6a4ab

File tree

2 files changed

+46
-23
lines changed

2 files changed

+46
-23
lines changed

package-lock.json

Lines changed: 10 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/api.ts

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { encode } from 'base-64';
2-
import fetch from 'node-fetch';
2+
import fetch, { Response } from 'node-fetch';
33

44
class Traffic {
55
/**
@@ -14,8 +14,10 @@ class Traffic {
1414
method: 'GET',
1515
headers: { Authorization: 'Basic ' + encode(`${username}:${password}`) }
1616
})
17-
.then((value) => value.json())
18-
.catch((err) => Error(err));
17+
.then((value) => validateResponseAndJSON(value))
18+
.catch((err) => {
19+
throw new Error(err);
20+
});
1921
}
2022
/**
2123
* @param user - The User of "repo"
@@ -34,8 +36,10 @@ class Traffic {
3436
method: 'GET',
3537
headers: { Authorization: 'Basic ' + encode(`${username}:${password}`) }
3638
})
37-
.then((value) => value.json())
38-
.catch((err) => Error(err));
39+
.then((value) => validateResponseAndJSON(value))
40+
.catch((err) => {
41+
throw new Error(err);
42+
});
3943
}
4044
/**
4145
* @param user - The User of "repo"
@@ -54,8 +58,10 @@ class Traffic {
5458
method: 'GET',
5559
headers: { Authorization: 'Basic ' + encode(`${username}:${password}`) }
5660
})
57-
.then((value) => value.json())
58-
.catch((err) => Error(err));
61+
.then((value) => validateResponseAndJSON(value))
62+
.catch((err) => {
63+
throw new Error(err);
64+
});
5965
}
6066
/**
6167
* @param user - The User of "repo"
@@ -69,8 +75,10 @@ class Traffic {
6975
method: 'GET',
7076
headers: { Authorization: 'Basic ' + encode(`${username}:${password}`) }
7177
})
72-
.then((value) => value.json())
73-
.catch((err) => Error(err));
78+
.then((value) => validateResponseAndJSON(value))
79+
.catch((err) => {
80+
throw new Error(err);
81+
});
7482
}
7583
}
7684
/**
@@ -79,8 +87,10 @@ class Traffic {
7987
*/
8088
async function Repos(user: string): Promise<ReposType[]> {
8189
return fetch(`https://api.github.com/users/${user}/repos?page=1&type=all&per_page=100`)
82-
.then((value) => value.json())
83-
.catch((err) => Error(err));
90+
.then((value) => validateResponseAndJSON(value))
91+
.catch((err) => {
92+
throw new Error(err);
93+
});
8494
}
8595

8696
/**
@@ -90,8 +100,10 @@ async function Repos(user: string): Promise<ReposType[]> {
90100
*/
91101
async function CommitActivity(user: string, repo: string): Promise<CommitActivityType[]> {
92102
return fetch(`https://api.github.com/repos/${user}/${repo}/stats/commit_activity`)
93-
.then((value) => value.json())
94-
.catch((err) => Error(err));
103+
.then((value) => validateResponseAndJSON(value))
104+
.catch((err) => {
105+
throw new Error(err);
106+
});
95107
}
96108

97109
/**
@@ -101,8 +113,17 @@ async function CommitActivity(user: string, repo: string): Promise<CommitActivit
101113
*/
102114
async function PunchCard(user: string, repo: string): Promise<PunchCardType[]> {
103115
return fetch(`https://api.github.com/repos/${user}/${repo}/stats/punch_card`)
104-
.then((value) => value.json())
105-
.catch((err) => Error(err));
116+
.then((value) => validateResponseAndJSON(value))
117+
.catch((err) => {
118+
throw new Error(err);
119+
});
120+
}
121+
122+
function validateResponseAndJSON(value: Response) {
123+
if (!value.ok) {
124+
throw new Error('Not a valid response');
125+
}
126+
return value.json();
106127
}
107128

108129
export { Traffic, Repos, CommitActivity, PunchCard };

0 commit comments

Comments
 (0)