diff --git a/src/fetchers/top-languages-fetcher.js b/src/fetchers/top-languages-fetcher.js index 1f386c1df682a..26e99e619ddb3 100644 --- a/src/fetchers/top-languages-fetcher.js +++ b/src/fetchers/top-languages-fetcher.js @@ -75,12 +75,6 @@ const fetchTopLanguages = async ( const res = await retryer(fetcher, { login: username }); - if (res.data.errors) { - logger.error(res.data.errors); - throw Error(res.data.errors[0].message || "Could not fetch user"); - } - - // Catch GraphQL errors. if (res.data.errors) { logger.error(res.data.errors); if (res.data.errors[0].type === "NOT_FOUND") { diff --git a/tests/fetchTopLanguages.test.js b/tests/fetchTopLanguages.test.js index 9a48a1620f8dc..90648c3198dd9 100644 --- a/tests/fetchTopLanguages.test.js +++ b/tests/fetchTopLanguages.test.js @@ -141,11 +141,31 @@ describe("FetchTopLanguages", () => { }); }); - it("should throw error", async () => { + it("should throw specific error when user not found", async () => { mock.onPost("https://api.github.com/graphql").reply(200, error); await expect(fetchTopLanguages("anuraghazra")).rejects.toThrow( "Could not resolve to a User with the login of 'noname'.", ); }); + + it("should throw other errors with their message", async () => { + mock.onPost("https://api.github.com/graphql").reply(200, { + errors: [{ message: "Some test GraphQL error" }], + }); + + await expect(fetchTopLanguages("anuraghazra")).rejects.toThrow( + "Some test GraphQL error", + ); + }); + + it("should throw error with specific message when error does not contain message property", async () => { + mock.onPost("https://api.github.com/graphql").reply(200, { + errors: [{ type: "TEST" }], + }); + + await expect(fetchTopLanguages("anuraghazra")).rejects.toThrow( + "Something went while trying to retrieve the language data using the GraphQL API.", + ); + }); }); diff --git a/tests/top-langs.test.js b/tests/top-langs.test.js index f0ce0b55bb51b..3f4e594aa7295 100644 --- a/tests/top-langs.test.js +++ b/tests/top-langs.test.js @@ -139,7 +139,12 @@ describe("Test /api/top-langs", () => { await topLangs(req, res); expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml"); - expect(res.send).toBeCalledWith(renderError(error.errors[0].message)); + expect(res.send).toBeCalledWith( + renderError( + error.errors[0].message, + "Make sure the provided username is not an organization", + ), + ); }); it("should render error card on incorrect layout input", async () => {