From ee513240ef7dd57055db7fc8270e6f7365b03f2a Mon Sep 17 00:00:00 2001 From: Anurag Hazra Date: Mon, 10 Aug 2020 19:36:56 +0530 Subject: [PATCH] feat: adde CustomError class for better secondary errors (#355) --- api/index.js | 7 +------ api/pin.js | 4 +--- api/top-langs.js | 2 +- src/common/retryer.js | 4 ++-- src/common/utils.js | 20 +++++++++++++++++++- src/fetchers/stats-fetcher.js | 7 +++++-- 6 files changed, 29 insertions(+), 15 deletions(-) diff --git a/api/index.js b/api/index.js index b1b0b3a..63954ba 100644 --- a/api/index.js +++ b/api/index.js @@ -38,12 +38,7 @@ module.exports = async (req, res) => { parseBoolean(include_all_commits) ); } catch (err) { - return res.send( - renderError( - err.message, - "Make sure the provided username is not an organization" - ) - ); + return res.send(renderError(err.message, err.secondaryMessage)); } const cacheSeconds = clampValue( diff --git a/api/pin.js b/api/pin.js index 22eb851..cf8ae72 100644 --- a/api/pin.js +++ b/api/pin.js @@ -4,7 +4,6 @@ const { parseBoolean, clampValue, CONSTANTS, - logger, } = require("../src/common/utils"); const fetchRepo = require("../src/fetchers/repo-fetcher"); const renderRepoCard = require("../src/cards/repo-card"); @@ -29,8 +28,7 @@ module.exports = async (req, res) => { try { repoData = await fetchRepo(username, repo); } catch (err) { - logger.error(err); - return res.send(renderError(err.message)); + return res.send(renderError(err.message, err.secondaryMessage)); } let cacheSeconds = clampValue( diff --git a/api/top-langs.js b/api/top-langs.js index 552dd1e..43866d0 100644 --- a/api/top-langs.js +++ b/api/top-langs.js @@ -30,7 +30,7 @@ module.exports = async (req, res) => { try { topLangs = await fetchTopLanguages(username); } catch (err) { - return res.send(renderError(err.message)); + return res.send(renderError(err.message, err.secondaryMessage)); } const cacheSeconds = clampValue( diff --git a/src/common/retryer.js b/src/common/retryer.js index 7771e22..9eaaf0a 100644 --- a/src/common/retryer.js +++ b/src/common/retryer.js @@ -1,8 +1,8 @@ -const { logger } = require("../common/utils"); +const { logger, CustomError } = require("../common/utils"); const retryer = async (fetcher, variables, retries = 0) => { if (retries > 7) { - throw new Error("Maximum retries exceeded"); + throw new CustomError("Maximum retries exceeded", CustomError.MAX_RETRY); } try { logger.log(`Trying PAT_${retries + 1}`); diff --git a/src/common/utils.js b/src/common/utils.js index 4bb1212..3b44bbb 100644 --- a/src/common/utils.js +++ b/src/common/utils.js @@ -14,7 +14,7 @@ const renderError = (message, secondaryMessage = "") => { Something went wrong! file an issue at https://git.io/JJmN9 ${encodeHTML(message)} - ${secondaryMessage} + ${secondaryMessage} `; @@ -170,6 +170,23 @@ const CONSTANTS = { ONE_DAY: 86400, }; +const SECONDARY_ERROR_MESSAGES = { + MAX_RETRY: + "Please add an env variable called PAT_1 with your github token in vercel", + USER_NOT_FOUND: "Make sure the provided username is not an organization", +}; + +class CustomError extends Error { + constructor(message, type) { + super(message); + this.type = type; + this.secondaryMessage = SECONDARY_ERROR_MESSAGES[type] || "adsad"; + } + + static MAX_RETRY = "MAX_RETRY"; + static USER_NOT_FOUND = "USER_NOT_FOUND"; +} + module.exports = { renderError, kFormatter, @@ -185,4 +202,5 @@ module.exports = { wrapTextMultiline, logger, CONSTANTS, + CustomError, }; diff --git a/src/fetchers/stats-fetcher.js b/src/fetchers/stats-fetcher.js index b1ef221..b7cc2af 100644 --- a/src/fetchers/stats-fetcher.js +++ b/src/fetchers/stats-fetcher.js @@ -1,4 +1,4 @@ -const { request, logger } = require("../common/utils"); +const { request, logger, CustomError } = require("../common/utils"); const axios = require("axios"); const retryer = require("../common/retryer"); const calculateRank = require("../calculateRank"); @@ -109,7 +109,10 @@ async function fetchStats( if (res.data.errors) { logger.error(res.data.errors); - throw Error(res.data.errors[0].message || "Could not fetch user"); + throw new CustomError( + res.data.errors[0].message || "Could not fetch user", + CustomError.USER_NOT_FOUND + ); } const user = res.data.data.user;