diff --git a/src/common/utils.js b/src/common/utils.js index baa93c69..92d8f45f 100644 --- a/src/common/utils.js +++ b/src/common/utils.js @@ -273,13 +273,28 @@ class CustomError extends Error { constructor(message, type) { super(message); this.type = type; - this.secondaryMessage = SECONDARY_ERROR_MESSAGES[type] || "adsad"; + this.secondaryMessage = SECONDARY_ERROR_MESSAGES[type] || type; } static MAX_RETRY = "MAX_RETRY"; static USER_NOT_FOUND = "USER_NOT_FOUND"; } +class MissingParamError extends Error { + /** + * @param {string[]} missedParams + * @param {string?=} secondaryMessage + */ + constructor(missedParams, secondaryMessage) { + const msg = `Missing params ${missedParams + .map((p) => `"${p}"`) + .join(", ")} make sure you pass the parameters in URL`; + super(msg); + this.missedParams = missedParams; + this.secondaryMessage = secondaryMessage; + } +} + /** * @see https://stackoverflow.com/a/48172630/10629172 * @param {string} str @@ -372,6 +387,7 @@ module.exports = { logger, CONSTANTS, CustomError, + MissingParamError, lowercaseTrim, chunkArray, parseEmojis, diff --git a/src/fetchers/repo-fetcher.js b/src/fetchers/repo-fetcher.js index 4490adb2..0bad5f2b 100644 --- a/src/fetchers/repo-fetcher.js +++ b/src/fetchers/repo-fetcher.js @@ -1,6 +1,6 @@ // @ts-check const retryer = require("../common/retryer"); -const { request } = require("../common/utils"); +const { request, MissingParamError } = require("../common/utils"); /** * @param {import('Axios').AxiosRequestHeaders} variables @@ -48,15 +48,19 @@ const fetcher = (variables, token) => { ); }; +const urlExample = "/api/pin?username=USERNAME&repo=REPO_NAME"; + /** * @param {string} username * @param {string} reponame * @returns {Promise} */ async function fetchRepo(username, reponame) { - if (!username || !reponame) { - throw new Error("Invalid username or reponame"); + if (!username && !reponame) { + throw new MissingParamError(["username", "repo"], urlExample); } + if (!username) throw new MissingParamError(["username"], urlExample); + if (!reponame) throw new MissingParamError(["repo"], urlExample); let res = await retryer(fetcher, { login: username, repo: reponame }); diff --git a/src/fetchers/stats-fetcher.js b/src/fetchers/stats-fetcher.js index 1caa62d1..04b4b3e5 100644 --- a/src/fetchers/stats-fetcher.js +++ b/src/fetchers/stats-fetcher.js @@ -4,7 +4,12 @@ const githubUsernameRegex = require("github-username-regex"); const retryer = require("../common/retryer"); const calculateRank = require("../calculateRank"); -const { request, logger, CustomError } = require("../common/utils"); +const { + request, + logger, + CustomError, + MissingParamError, +} = require("../common/utils"); require("dotenv").config(); @@ -103,7 +108,7 @@ async function fetchStats( count_private = false, include_all_commits = false, ) { - if (!username) throw Error("Invalid username"); + if (!username) throw new MissingParamError(["username"]); const stats = { name: "", diff --git a/src/fetchers/top-languages-fetcher.js b/src/fetchers/top-languages-fetcher.js index 555b454b..9dd109e2 100644 --- a/src/fetchers/top-languages-fetcher.js +++ b/src/fetchers/top-languages-fetcher.js @@ -1,5 +1,5 @@ // @ts-check -const { request, logger } = require("../common/utils"); +const { request, logger, MissingParamError } = require("../common/utils"); const retryer = require("../common/retryer"); require("dotenv").config(); @@ -45,7 +45,7 @@ const fetcher = (variables, token) => { * @returns {Promise} */ async function fetchTopLanguages(username, exclude_repo = []) { - if (!username) throw Error("Invalid username"); + if (!username) throw new MissingParamError(["username"]); const res = await retryer(fetcher, { login: username }); diff --git a/src/fetchers/wakatime-fetcher.js b/src/fetchers/wakatime-fetcher.js index f8080e82..e9779d60 100644 --- a/src/fetchers/wakatime-fetcher.js +++ b/src/fetchers/wakatime-fetcher.js @@ -1,15 +1,18 @@ const axios = require("axios"); +const { MissingParamError } = require("../common/utils"); /** * @param {{username: string, api_domain: string, range: string}} props - * @returns {Promise} + * @returns {Promise} */ const fetchWakatimeStats = async ({ username, api_domain, range }) => { + if (!username) throw new MissingParamError(["username"]); + try { const { data } = await axios.get( `https://${ api_domain ? api_domain.replace(/\/$/gi, "") : "wakatime.com" - }/api/v1/users/${username}/stats/${range || ''}?is_including_today=true`, + }/api/v1/users/${username}/stats/${range || ""}?is_including_today=true`, ); return data.data; diff --git a/tests/fetchWakatime.test.js b/tests/fetchWakatime.test.js index 9ff7cc15..6255890c 100644 --- a/tests/fetchWakatime.test.js +++ b/tests/fetchWakatime.test.js @@ -207,7 +207,7 @@ describe("Wakatime fetcher", () => { mock.onGet(/\/https:\/\/wakatime\.com\/api/).reply(404, wakaTimeData); await expect(fetchWakatimeStats("noone")).rejects.toThrow( - "Wakatime user not found, make sure you have a wakatime profile", + "Missing params \"username\" make sure you pass the parameters in URL", ); }); });