mirror of
https://github.com/Aviortheking/codestats-readme.git
synced 2025-04-22 10:42:08 +00:00
feat: adde CustomError class for better secondary errors (#355)
This commit is contained in:
parent
4df291e697
commit
ee513240ef
@ -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(
|
||||
|
@ -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(
|
||||
|
@ -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(
|
||||
|
@ -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}`);
|
||||
|
@ -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,
|
||||
};
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user