feat: added better error messages (#1608)

This commit is contained in:
Anurag Hazra 2022-02-23 20:52:18 +05:30 committed by GitHub
parent d57251cdf1
commit e00fe88ed3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 39 additions and 11 deletions

View File

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

View File

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

View File

@ -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: "",

View File

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

View File

@ -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>}
*/
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;

View File

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