Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added better error messages #1608

Merged
merged 1 commit into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -372,6 +387,7 @@ module.exports = {
logger,
CONSTANTS,
CustomError,
MissingParamError,
lowercaseTrim,
chunkArray,
parseEmojis,
Expand Down
10 changes: 7 additions & 3 deletions src/fetchers/repo-fetcher.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 });

Expand Down
9 changes: 7 additions & 2 deletions src/fetchers/stats-fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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: "",
Expand Down
4 changes: 2 additions & 2 deletions src/fetchers/top-languages-fetcher.js
Original file line number Diff line number Diff line change
@@ -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();

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

Expand Down
7 changes: 5 additions & 2 deletions src/fetchers/wakatime-fetcher.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/fetchWakatime.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
});
});
Expand Down