Skip to content

Commit b164ece

Browse files
refactor: move errors related code into separate file (anuraghazra#4554)
* refactor: move errors related code into separate file * jsdoc --------- Co-authored-by: Alexandr <qwerty541zxc@gmail.com>
1 parent f6e5d50 commit b164ece

11 files changed

Lines changed: 95 additions & 83 deletions

File tree

src/cards/stats.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// @ts-check
2+
23
import { Card } from "../common/Card.js";
4+
import { CustomError } from "../common/error.js";
35
import { I18n } from "../common/I18n.js";
46
import { icons, rankIcon } from "../common/icons.js";
57
import {
6-
CustomError,
78
clampValue,
89
flexLayout,
910
getCardColors,

src/common/error.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @type {string} A general message to ask user to try again later.
3+
*/
4+
const TRY_AGAIN_LATER = "Please try again later";
5+
6+
/**
7+
* @type {Object<string, string>} A map of error types to secondary error messages.
8+
*/
9+
const SECONDARY_ERROR_MESSAGES = {
10+
MAX_RETRY:
11+
"You can deploy own instance or wait until public will be no longer limited",
12+
NO_TOKENS:
13+
"Please add an env variable called PAT_1 with your GitHub API token in vercel",
14+
USER_NOT_FOUND: "Make sure the provided username is not an organization",
15+
GRAPHQL_ERROR: TRY_AGAIN_LATER,
16+
GITHUB_REST_API_ERROR: TRY_AGAIN_LATER,
17+
WAKATIME_USER_NOT_FOUND: "Make sure you have a public WakaTime profile",
18+
};
19+
20+
/**
21+
* Custom error class to handle custom GRS errors.
22+
*/
23+
class CustomError extends Error {
24+
/**
25+
* Custom error constructor.
26+
*
27+
* @param {string} message Error message.
28+
* @param {string} type Error type.
29+
*/
30+
constructor(message, type) {
31+
super(message);
32+
this.type = type;
33+
this.secondaryMessage = SECONDARY_ERROR_MESSAGES[type] || type;
34+
}
35+
36+
static MAX_RETRY = "MAX_RETRY";
37+
static NO_TOKENS = "NO_TOKENS";
38+
static USER_NOT_FOUND = "USER_NOT_FOUND";
39+
static GRAPHQL_ERROR = "GRAPHQL_ERROR";
40+
static GITHUB_REST_API_ERROR = "GITHUB_REST_API_ERROR";
41+
static WAKATIME_ERROR = "WAKATIME_ERROR";
42+
}
43+
44+
/**
45+
* Missing query parameter class.
46+
*/
47+
class MissingParamError extends Error {
48+
/**
49+
* Missing query parameter error constructor.
50+
*
51+
* @param {string[]} missedParams An array of missing parameters names.
52+
* @param {string=} secondaryMessage Optional secondary message to display.
53+
*/
54+
constructor(missedParams, secondaryMessage) {
55+
const msg = `Missing params ${missedParams
56+
.map((p) => `"${p}"`)
57+
.join(", ")} make sure you pass the parameters in URL`;
58+
super(msg);
59+
this.missedParams = missedParams;
60+
this.secondaryMessage = secondaryMessage;
61+
}
62+
}
63+
64+
export {
65+
CustomError,
66+
MissingParamError,
67+
SECONDARY_ERROR_MESSAGES,
68+
TRY_AGAIN_LATER,
69+
};

src/common/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ export {
2222
getCardColors,
2323
wrapTextMultiline,
2424
logger,
25-
CustomError,
26-
MissingParamError,
2725
measureText,
2826
lowercaseTrim,
2927
chunkArray,

src/common/retryer.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { CustomError, logger } from "./utils.js";
1+
// @ts-check
2+
3+
import { CustomError } from "./error.js";
4+
import { logger } from "./utils.js";
25

36
// Script variables.
47

@@ -10,7 +13,7 @@ const RETRIES = process.env.NODE_ENV === "test" ? 7 : PATs;
1013

1114
/**
1215
* @typedef {import("axios").AxiosResponse} AxiosResponse Axios response.
13-
* @typedef {(variables: object, token: string) => Promise<AxiosResponse>} FetcherFunction Fetcher function.
16+
* @typedef {(variables: object, token: string, retriesForTests?: number) => Promise<AxiosResponse>} FetcherFunction Fetcher function.
1417
*/
1518

1619
/**
@@ -19,7 +22,7 @@ const RETRIES = process.env.NODE_ENV === "test" ? 7 : PATs;
1922
* @param {FetcherFunction} fetcher The fetcher function.
2023
* @param {object} variables Object with arguments to pass to the fetcher function.
2124
* @param {number} retries How many times to retry.
22-
* @returns {Promise<T>} The response from the fetcher function.
25+
* @returns {Promise<any>} The response from the fetcher function.
2326
*/
2427
const retryer = async (fetcher, variables, retries = 0) => {
2528
if (!RETRIES) {
@@ -37,7 +40,9 @@ const retryer = async (fetcher, variables, retries = 0) => {
3740
// try to fetch with the first token since RETRIES is 0 index i'm adding +1
3841
let response = await fetcher(
3942
variables,
43+
// @ts-ignore
4044
process.env[`PAT_${retries + 1}`],
45+
// used in tests for faking rate limit
4146
retries,
4247
);
4348

src/common/utils.js

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,10 @@
11
// @ts-check
2+
23
import axios from "axios";
34
import toEmoji from "emoji-name-map";
45
import wrap from "word-wrap";
56
import { themes } from "../../themes/index.js";
6-
7-
const TRY_AGAIN_LATER = "Please try again later";
8-
9-
const SECONDARY_ERROR_MESSAGES = {
10-
MAX_RETRY:
11-
"You can deploy own instance or wait until public will be no longer limited",
12-
NO_TOKENS:
13-
"Please add an env variable called PAT_1 with your GitHub API token in vercel",
14-
USER_NOT_FOUND: "Make sure the provided username is not an organization",
15-
GRAPHQL_ERROR: TRY_AGAIN_LATER,
16-
GITHUB_REST_API_ERROR: TRY_AGAIN_LATER,
17-
WAKATIME_USER_NOT_FOUND: "Make sure you have a public WakaTime profile",
18-
};
19-
20-
/**
21-
* Custom error class to handle custom GRS errors.
22-
*/
23-
class CustomError extends Error {
24-
/**
25-
* @param {string} message Error message.
26-
* @param {string} type Error type.
27-
*/
28-
constructor(message, type) {
29-
super(message);
30-
this.type = type;
31-
this.secondaryMessage = SECONDARY_ERROR_MESSAGES[type] || type;
32-
}
33-
34-
static MAX_RETRY = "MAX_RETRY";
35-
static NO_TOKENS = "NO_TOKENS";
36-
static USER_NOT_FOUND = "USER_NOT_FOUND";
37-
static GRAPHQL_ERROR = "GRAPHQL_ERROR";
38-
static GITHUB_REST_API_ERROR = "GITHUB_REST_API_ERROR";
39-
static WAKATIME_ERROR = "WAKATIME_ERROR";
40-
}
7+
import { SECONDARY_ERROR_MESSAGES, TRY_AGAIN_LATER } from "./error.js";
418

429
/**
4310
* Auto layout utility, allows us to layout things vertically or horizontally with
@@ -452,26 +419,6 @@ const noop = () => {};
452419
const logger =
453420
process.env.NODE_ENV === "test" ? { log: noop, error: noop } : console;
454421

455-
/**
456-
* Missing query parameter class.
457-
*/
458-
class MissingParamError extends Error {
459-
/**
460-
* Missing query parameter error constructor.
461-
*
462-
* @param {string[]} missedParams An array of missing parameters names.
463-
* @param {string=} secondaryMessage Optional secondary message to display.
464-
*/
465-
constructor(missedParams, secondaryMessage) {
466-
const msg = `Missing params ${missedParams
467-
.map((p) => `"${p}"`)
468-
.join(", ")} make sure you pass the parameters in URL`;
469-
super(msg);
470-
this.missedParams = missedParams;
471-
this.secondaryMessage = secondaryMessage;
472-
}
473-
}
474-
475422
/**
476423
* Retrieve text length.
477424
*
@@ -620,8 +567,6 @@ export {
620567
getCardColors,
621568
wrapTextMultiline,
622569
logger,
623-
CustomError,
624-
MissingParamError,
625570
measureText,
626571
lowercaseTrim,
627572
chunkArray,

src/fetchers/gist.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// @ts-check
22

3-
import { request, MissingParamError } from "../common/utils.js";
3+
import { request } from "../common/utils.js";
44
import { retryer } from "../common/retryer.js";
5+
import { MissingParamError } from "../common/error.js";
56

67
/**
78
* @typedef {import('axios').AxiosRequestHeaders} AxiosRequestHeaders Axios request headers.

src/fetchers/repo.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// @ts-check
2+
3+
import { MissingParamError } from "../common/error.js";
24
import { retryer } from "../common/retryer.js";
3-
import { MissingParamError, request } from "../common/utils.js";
5+
import { request } from "../common/utils.js";
46

57
/**
68
* @typedef {import('axios').AxiosRequestHeaders} AxiosRequestHeaders Axios request headers.

src/fetchers/stats.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@ import * as dotenv from "dotenv";
55
import githubUsernameRegex from "github-username-regex";
66
import { calculateRank } from "../calculateRank.js";
77
import { retryer } from "../common/retryer.js";
8-
import {
9-
CustomError,
10-
logger,
11-
MissingParamError,
12-
request,
13-
wrapTextMultiline,
14-
} from "../common/utils.js";
8+
import { logger, request, wrapTextMultiline } from "../common/utils.js";
159
import { excludeRepositories } from "../common/envs.js";
10+
import { CustomError, MissingParamError } from "../common/error.js";
1611

1712
dotenv.config();
1813

src/fetchers/top-languages.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
// @ts-check
22

33
import { retryer } from "../common/retryer.js";
4-
import {
5-
CustomError,
6-
logger,
7-
MissingParamError,
8-
request,
9-
wrapTextMultiline,
10-
} from "../common/utils.js";
4+
import { logger, request, wrapTextMultiline } from "../common/utils.js";
115
import { excludeRepositories } from "../common/envs.js";
6+
import { CustomError, MissingParamError } from "../common/error.js";
127

138
/**
149
* @typedef {import("axios").AxiosRequestHeaders} AxiosRequestHeaders Axios request headers.

src/fetchers/wakatime.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
// @ts-check
2+
13
import axios from "axios";
2-
import { CustomError, MissingParamError } from "../common/utils.js";
4+
import { CustomError, MissingParamError } from "../common/error.js";
35

46
/**
57
* WakaTime data fetcher.
68
*
79
* @param {{username: string, api_domain: string }} props Fetcher props.
8-
* @returns {Promise<WakaTimeData>} WakaTime data response.
10+
* @returns {Promise<import("./types").WakaTimeData>} WakaTime data response.
911
*/
1012
const fetchWakatimeStats = async ({ username, api_domain }) => {
1113
if (!username) {

0 commit comments

Comments
 (0)