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

Commit

Permalink
🐛 Better Error Handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kaaax0815 committed May 5, 2021
1 parent 8ca9cdd commit a78ae77
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Traffic {
})
.then((value) => validateResponseAndJSON(value))
.catch((err) => {
throw new Error(err);
throw new Error(err.message ? err.message : err);
});
}
/**
Expand All @@ -38,7 +38,7 @@ class Traffic {
})
.then((value) => validateResponseAndJSON(value))
.catch((err) => {
throw new Error(err);
throw new Error(err.message ? err.message : err);
});
}
/**
Expand All @@ -60,7 +60,7 @@ class Traffic {
})
.then((value) => validateResponseAndJSON(value))
.catch((err) => {
throw new Error(err);
throw new Error(err.message ? err.message : err);
});
}
/**
Expand All @@ -77,7 +77,7 @@ class Traffic {
})
.then((value) => validateResponseAndJSON(value))
.catch((err) => {
throw new Error(err);
throw new Error(err.message ? err.message : err);
});
}
}
Expand All @@ -89,7 +89,7 @@ async function Repos(user: string): Promise<ReposType[]> {
return fetch(`https://api.github.com/users/${user}/repos?page=1&type=all&per_page=100`)
.then((value) => validateResponseAndJSON(value))
.catch((err) => {
throw new Error(err);
throw new Error(err.message ? err.message : err);
});
}

Expand All @@ -102,7 +102,7 @@ async function CommitActivity(user: string, repo: string): Promise<CommitActivit
return fetch(`https://api.github.com/repos/${user}/${repo}/stats/commit_activity`)
.then((value) => validateResponseAndJSON(value))
.catch((err) => {
throw new Error(err);
throw new Error(err.message ? err.message : err);
});
}

Expand All @@ -115,13 +115,15 @@ async function PunchCard(user: string, repo: string): Promise<PunchCardType[]> {
return fetch(`https://api.github.com/repos/${user}/${repo}/stats/punch_card`)
.then((value) => validateResponseAndJSON(value))
.catch((err) => {
throw new Error(err);
throw new Error(err.message ? err.message : err);
});
}

function validateResponseAndJSON(value: Response) {
async function validateResponseAndJSON(value: Response) {
if (!value.ok) {
throw new Error('Not a valid response');
const json = await value.json();
// Check if Github Error Message exists
throw new Error(json.message ? json.message : "Could'nt get a valid response");
}
return value.json();
}
Expand Down Expand Up @@ -274,7 +276,7 @@ export interface CommitActivityType {
days: number[];
}

interface PunchCardType {
export interface PunchCardType {
/** 0-6: Sunday - Saturday */
0: number;
/** 0-23: Hour of day */
Expand Down

0 comments on commit a78ae77

Please sign in to comment.