From a78ae77f8afe5a0438c6222e31a539000be61da2 Mon Sep 17 00:00:00 2001 From: kaaax0815 <999999bst@gmail.com> Date: Wed, 5 May 2021 23:46:46 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Better=20Error=20Handling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api.ts | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/api.ts b/src/api.ts index c303ad8..c1a8775 100644 --- a/src/api.ts +++ b/src/api.ts @@ -16,7 +16,7 @@ class Traffic { }) .then((value) => validateResponseAndJSON(value)) .catch((err) => { - throw new Error(err); + throw new Error(err.message ? err.message : err); }); } /** @@ -38,7 +38,7 @@ class Traffic { }) .then((value) => validateResponseAndJSON(value)) .catch((err) => { - throw new Error(err); + throw new Error(err.message ? err.message : err); }); } /** @@ -60,7 +60,7 @@ class Traffic { }) .then((value) => validateResponseAndJSON(value)) .catch((err) => { - throw new Error(err); + throw new Error(err.message ? err.message : err); }); } /** @@ -77,7 +77,7 @@ class Traffic { }) .then((value) => validateResponseAndJSON(value)) .catch((err) => { - throw new Error(err); + throw new Error(err.message ? err.message : err); }); } } @@ -89,7 +89,7 @@ async function Repos(user: string): Promise { 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); }); } @@ -102,7 +102,7 @@ async function CommitActivity(user: string, repo: string): Promise validateResponseAndJSON(value)) .catch((err) => { - throw new Error(err); + throw new Error(err.message ? err.message : err); }); } @@ -115,13 +115,15 @@ async function PunchCard(user: string, repo: string): Promise { 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(); } @@ -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 */