feat: adde CustomError class for better secondary errors (#355)

This commit is contained in:
Anurag Hazra 2020-08-10 19:36:56 +05:30 committed by GitHub
parent 4df291e697
commit ee513240ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 15 deletions

View File

@ -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(

View File

@ -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(

View File

@ -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(

View File

@ -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}`);

View File

@ -14,7 +14,7 @@ const renderError = (message, secondaryMessage = "") => {
<text x="25" y="45" class="text">Something went wrong! file an issue at https://git.io/JJmN9</text>
<text data-testid="message" x="25" y="55" class="text small">
<tspan x="25" dy="18">${encodeHTML(message)}</tspan>
<tspan x="25" dy="18" class="gray">${secondaryMessage}</tspan>
<tspan x="25" dy="18" class="gray">${secondaryMessage}</tspan>
</text>
</svg>
`;
@ -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,
};

View File

@ -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;