From 34772a6dcdfe741bb85287f91f31f675d1b0a65a Mon Sep 17 00:00:00 2001 From: Alexandr Garbuzov Date: Fri, 13 Oct 2023 12:16:14 +0300 Subject: [PATCH] feature: do not aks user's to open issues on upstream API errors (#3273) Co-authored-by: rickstaa --- src/common/utils.js | 81 +++++++++++++++++++++++++-------------------- tests/api.test.js | 4 +++ 2 files changed, 49 insertions(+), 36 deletions(-) diff --git a/src/common/utils.js b/src/common/utils.js index 1b38a9f28d339..93d60cd850db1 100644 --- a/src/common/utils.js +++ b/src/common/utils.js @@ -4,6 +4,41 @@ import toEmoji from "emoji-name-map"; import wrap from "word-wrap"; import { themes } from "../../themes/index.js"; +const TRY_AGAIN_LATER = "Please try again later"; + +const SECONDARY_ERROR_MESSAGES = { + MAX_RETRY: + "You can deploy own instance or wait until public will be no longer limited", + NO_TOKENS: + "Please add an env variable called PAT_1 with your GitHub API token in vercel", + USER_NOT_FOUND: "Make sure the provided username is not an organization", + GRAPHQL_ERROR: TRY_AGAIN_LATER, + GITHUB_REST_API_ERROR: TRY_AGAIN_LATER, + WAKATIME_USER_NOT_FOUND: "Make sure you have a public WakaTime profile", +}; + +/** + * Custom error class to handle custom GRS errors. + */ +class CustomError extends Error { + /** + * @param {string} message Error message. + * @param {string} type Error type. + */ + constructor(message, type) { + super(message); + this.type = type; + this.secondaryMessage = SECONDARY_ERROR_MESSAGES[type] || type; + } + + static MAX_RETRY = "MAX_RETRY"; + static NO_TOKENS = "NO_TOKENS"; + static USER_NOT_FOUND = "USER_NOT_FOUND"; + static GRAPHQL_ERROR = "GRAPHQL_ERROR"; + static GITHUB_REST_API_ERROR = "GITHUB_REST_API_ERROR"; + static WAKATIME_ERROR = "WAKATIME_ERROR"; +} + // Script parameters. const ERROR_CARD_LENGTH = 576.5; @@ -23,6 +58,11 @@ const encodeHTML = (str) => { .replace(/\u0008/gim, ""); }; +const UPSTREAM_API_ERRORS = [ + TRY_AGAIN_LATER, + SECONDARY_ERROR_MESSAGES.MAX_RETRY, +]; + /** * Renders error message on the card. * @@ -41,7 +81,11 @@ const renderError = (message, secondaryMessage = "") => { - Something went wrong! file an issue at https://tiny.one/readme-stats + Something went wrong!${ + UPSTREAM_API_ERRORS.includes(secondaryMessage) + ? "" + : " file an issue at https://tiny.one/readme-stats" + } ${encodeHTML(message)} ${secondaryMessage} @@ -399,41 +443,6 @@ const CONSTANTS = { ERROR_CACHE_SECONDS: TEN_MINUTES, }; -const TRY_AGAIN_LATER = "Please try again later"; - -const SECONDARY_ERROR_MESSAGES = { - MAX_RETRY: - "You can deploy own instance or wait until public will be no longer limited", - NO_TOKENS: - "Please add an env variable called PAT_1 with your GitHub API token in vercel", - USER_NOT_FOUND: "Make sure the provided username is not an organization", - GRAPHQL_ERROR: TRY_AGAIN_LATER, - GITHUB_REST_API_ERROR: TRY_AGAIN_LATER, - WAKATIME_USER_NOT_FOUND: "Make sure you have a public WakaTime profile", -}; - -/** - * Custom error class to handle custom GRS errors. - */ -class CustomError extends Error { - /** - * @param {string} message Error message. - * @param {string} type Error type. - */ - constructor(message, type) { - super(message); - this.type = type; - this.secondaryMessage = SECONDARY_ERROR_MESSAGES[type] || type; - } - - static MAX_RETRY = "MAX_RETRY"; - static NO_TOKENS = "NO_TOKENS"; - static USER_NOT_FOUND = "USER_NOT_FOUND"; - static GRAPHQL_ERROR = "GRAPHQL_ERROR"; - static GITHUB_REST_API_ERROR = "GITHUB_REST_API_ERROR"; - static WAKATIME_ERROR = "WAKATIME_ERROR"; -} - /** * Missing query parameter class. */ diff --git a/tests/api.test.js b/tests/api.test.js index 6af40f8818463..a6ed04b5c0260 100644 --- a/tests/api.test.js +++ b/tests/api.test.js @@ -321,5 +321,9 @@ describe("Test /api/", () => { expect(res.send).toBeCalledWith( renderError("Could not fetch total commits.", "Please try again later"), ); + // Received SVG output should not contain string "https://tiny.one/readme-stats" + expect(res.send.mock.calls[0][0]).not.toContain( + "https://tiny.one/readme-stats", + ); }); });