From 65c330018974b42601b96f9ef1fb7515d522dd56 Mon Sep 17 00:00:00 2001 From: Alexandr Garbuzov Date: Thu, 21 Sep 2023 21:30:24 +0300 Subject: [PATCH] refactor(stats card fetcher): improve could not fetch total commits error message (#3255) --- src/fetchers/stats-fetcher.js | 5 ++++- tests/api.test.js | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/fetchers/stats-fetcher.js b/src/fetchers/stats-fetcher.js index f021a72..b4a8ca2 100644 --- a/src/fetchers/stats-fetcher.js +++ b/src/fetchers/stats-fetcher.js @@ -180,7 +180,10 @@ const totalCommitsFetcher = async (username) => { const totalCount = res.data.total_count; if (!totalCount || isNaN(totalCount)) { - throw new Error("Could not fetch total commits."); + throw new CustomError( + "Could not fetch total commits.", + CustomError.GRAPHQL_ERROR, + ); } return totalCount; }; diff --git a/tests/api.test.js b/tests/api.test.js index 8d2a1a0..6af40f8 100644 --- a/tests/api.test.js +++ b/tests/api.test.js @@ -304,4 +304,22 @@ describe("Test /api/", () => { renderError("Something went wrong", "Language not found"), ); }); + + it("should render error card when include_all_commits true and upstream API fails", async () => { + mock + .onGet("https://api.github.com/search/commits?q=author:anuraghazra") + .reply(200, { error: "Some test error message" }); + + const { req, res } = faker( + { username: "anuraghazra", include_all_commits: true }, + data_stats, + ); + + await api(req, res); + + expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml"); + expect(res.send).toBeCalledWith( + renderError("Could not fetch total commits.", "Please try again later"), + ); + }); });