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)
|
parseBoolean(include_all_commits)
|
||||||
);
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return res.send(
|
return res.send(renderError(err.message, err.secondaryMessage));
|
||||||
renderError(
|
|
||||||
err.message,
|
|
||||||
"Make sure the provided username is not an organization"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const cacheSeconds = clampValue(
|
const cacheSeconds = clampValue(
|
||||||
|
@ -4,7 +4,6 @@ const {
|
|||||||
parseBoolean,
|
parseBoolean,
|
||||||
clampValue,
|
clampValue,
|
||||||
CONSTANTS,
|
CONSTANTS,
|
||||||
logger,
|
|
||||||
} = require("../src/common/utils");
|
} = require("../src/common/utils");
|
||||||
const fetchRepo = require("../src/fetchers/repo-fetcher");
|
const fetchRepo = require("../src/fetchers/repo-fetcher");
|
||||||
const renderRepoCard = require("../src/cards/repo-card");
|
const renderRepoCard = require("../src/cards/repo-card");
|
||||||
@ -29,8 +28,7 @@ module.exports = async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
repoData = await fetchRepo(username, repo);
|
repoData = await fetchRepo(username, repo);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error(err);
|
return res.send(renderError(err.message, err.secondaryMessage));
|
||||||
return res.send(renderError(err.message));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let cacheSeconds = clampValue(
|
let cacheSeconds = clampValue(
|
||||||
|
@ -30,7 +30,7 @@ module.exports = async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
topLangs = await fetchTopLanguages(username);
|
topLangs = await fetchTopLanguages(username);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return res.send(renderError(err.message));
|
return res.send(renderError(err.message, err.secondaryMessage));
|
||||||
}
|
}
|
||||||
|
|
||||||
const cacheSeconds = clampValue(
|
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) => {
|
const retryer = async (fetcher, variables, retries = 0) => {
|
||||||
if (retries > 7) {
|
if (retries > 7) {
|
||||||
throw new Error("Maximum retries exceeded");
|
throw new CustomError("Maximum retries exceeded", CustomError.MAX_RETRY);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
logger.log(`Trying PAT_${retries + 1}`);
|
logger.log(`Trying PAT_${retries + 1}`);
|
||||||
|
@ -170,6 +170,23 @@ const CONSTANTS = {
|
|||||||
ONE_DAY: 86400,
|
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 = {
|
module.exports = {
|
||||||
renderError,
|
renderError,
|
||||||
kFormatter,
|
kFormatter,
|
||||||
@ -185,4 +202,5 @@ module.exports = {
|
|||||||
wrapTextMultiline,
|
wrapTextMultiline,
|
||||||
logger,
|
logger,
|
||||||
CONSTANTS,
|
CONSTANTS,
|
||||||
|
CustomError,
|
||||||
};
|
};
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
const { request, logger } = require("../common/utils");
|
const { request, logger, CustomError } = require("../common/utils");
|
||||||
const axios = require("axios");
|
const axios = require("axios");
|
||||||
const retryer = require("../common/retryer");
|
const retryer = require("../common/retryer");
|
||||||
const calculateRank = require("../calculateRank");
|
const calculateRank = require("../calculateRank");
|
||||||
@ -109,7 +109,10 @@ async function fetchStats(
|
|||||||
|
|
||||||
if (res.data.errors) {
|
if (res.data.errors) {
|
||||||
logger.error(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;
|
const user = res.data.data.user;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user