mirror of
https://github.com/anuraghazra/github-readme-stats.git
synced 2024-12-15 06:04:17 +08:00
feat: added better error messages (#1608)
This commit is contained in:
parent
d57251cdf1
commit
e00fe88ed3
@ -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,
|
||||
|
@ -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<import("./types").RepositoryData>}
|
||||
*/
|
||||
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 });
|
||||
|
||||
|
@ -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: "",
|
||||
|
@ -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<import("./types").TopLangData>}
|
||||
*/
|
||||
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 });
|
||||
|
||||
|
@ -1,15 +1,18 @@
|
||||
const axios = require("axios");
|
||||
const { MissingParamError } = require("../common/utils");
|
||||
|
||||
/**
|
||||
* @param {{username: string, api_domain: string, range: string}} props
|
||||
* @returns {Promise<WakaTimeData>}
|
||||
* @returns {Promise<WakaTimeData>}
|
||||
*/
|
||||
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;
|
||||
|
@ -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",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user