Repository: anuraghazra/github-readme-stats
Pull request #3126 (2023-08-22 06:53:39)

Top langs card: Remove unreachable code from fetcher and increase tests coverage
There is two error handlers inside `fetchTopLanguages` function. Having error handler on strings 78-81 makes unreachable the next more advanced error handler on strings 84-102.

vercel[bot]: [vc]: #s+lvg052FlZmLOqYIKn7UGpa3OLnMPEH7bh+qzo6zvs=:eyJpc01vbm9yZXBvIjp0cnVlLCJ0eXBlIjoiZ2l0aHViIiwicHJvamVjdHMiOlt7Im5hbWUiOiJnaXRodWItcmVhZG1lLXN0YXRzIiwiaW5zcGVjdG9yVXJsIjoiaHR0cHM6Ly92ZXJjZWwuY29tL2dpdGh1Yi1yZWFkbWUtc3RhdHMtdGVhbS9naXRodWItcmVhZG1lLXN0YXRzLzRuWjZNYWFZRWRiS1J3dWZBTnVobXgyQnNXYUIiLCJwcmV2aWV3VXJsIjoiZ2l0aHViLXJlYWRtZS1zdGF0cy1naXQtdG9wbGFuZy03NTBmYzEtZ2l0aHViLXJlYWRtZS1zdGF0cy10ZWFtLnZlcmNlbC5hcHAiLCJuZXh0Q29tbWl0U3RhdHVzIjoiREVQTE9ZRUQifV19
**The latest updates on your projects**. Learn more about [Vercel for Git ↗︎](https://vercel.link/github-learn-more)

| Name | Status | Preview | Updated (UTC) |
| :--- | :----- | :------ | :------ |
| **github-readme-stats** | ✅ Ready ([Inspect](https://vercel.com/github-readme-stats-team/github-readme-stats/4nZ6MaaYEdbKRwufANuhmx2BsWaB)) | [Visit Preview](https://github-readme-stats-git-toplang-750fc1-github-readme-stats-team.vercel.app) | Aug 22, 2023 6:53am |



Patch:
diff --git a/src/fetchers/top-languages-fetcher.js b/src/fetchers/top-languages-fetcher.js
index 1f386c1..26e99e6 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") {


Test patch:
diff --git a/tests/fetchTopLanguages.test.js b/tests/fetchTopLanguages.test.js
index 9a48a16..90648c3 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 f0ce0b5..3f4e594 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 () => {