Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit36a8c39

Browse files
committed
fix: improve graphql error handling
1 parent343058c commit36a8c39

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

‎src/common/retryer.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ const retryer = async (fetcher, variables, retries = 0) => {
4545
retries++;
4646
// directly return from the function
4747
returnretryer(fetcher,variables,retries);
48+
}else{
49+
returnerr.response;
4850
}
4951
}
5052
};

‎src/common/utils.js‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import toEmoji from "emoji-name-map";
44
importwrapfrom"word-wrap";
55
import{themes}from"../../themes/index.js";
66

7+
// Script parameters.
8+
constERROR_CARD_LENGTH=576.5;
9+
710
/**
811
* Renders error message on the card.
912
*
@@ -13,13 +16,15 @@ import { themes } from "../../themes/index.js";
1316
*/
1417
constrenderError=(message,secondaryMessage="")=>{
1518
return`
16-
<svg width="576.5" height="120" viewBox="0 0576.5 120" fill="none" xmlns="http://www.w3.org/2000/svg">
19+
<svg width="${ERROR_CARD_LENGTH}" height="120" viewBox="0 0${ERROR_CARD_LENGTH} 120" fill="none" xmlns="http://www.w3.org/2000/svg">
1720
<style>
1821
.text { font: 600 16px 'Segoe UI', Ubuntu, Sans-Serif; fill: #2F80ED }
1922
.small { font: 600 12px 'Segoe UI', Ubuntu, Sans-Serif; fill: #252525 }
2023
.gray { fill: #858585 }
2124
</style>
22-
<rect x="0.5" y="0.5" width="575.5" height="99%" rx="4.5" fill="#FFFEFE" stroke="#E4E2E2"/>
25+
<rect x="0.5" y="0.5" width="${
26+
ERROR_CARD_LENGTH-1
27+
}" height="99%" rx="4.5" fill="#FFFEFE" stroke="#E4E2E2"/>
2328
<text x="25" y="45" class="text">Something went wrong! file an issue at https://tiny.one/readme-stats</text>
2429
<text data-testid="message" x="25" y="55" class="text small">
2530
<tspan x="25" dy="18">${encodeHTML(message)}</tspan>
@@ -241,7 +246,7 @@ function getCardColors({
241246
* Split text over multiple lines based on the card width.
242247
*
243248
*@param {string} text Text to split.
244-
*@param {number} widthCard width.
249+
*@param {number} widthLine width in number of characters.
245250
*@param {number} maxLines Maximum number of lines.
246251
*@returns {string[]} Array of lines.
247252
*/
@@ -288,6 +293,7 @@ const SECONDARY_ERROR_MESSAGES = {
288293
MAX_RETRY:
289294
"Please add an env variable called PAT_1 with your github token in vercel",
290295
USER_NOT_FOUND:"Make sure the provided username is not an organization",
296+
GRAPHQL_ERROR:"Please try again later",
291297
};
292298

293299
/**
@@ -306,6 +312,7 @@ class CustomError extends Error {
306312

307313
staticMAX_RETRY="MAX_RETRY";
308314
staticUSER_NOT_FOUND="USER_NOT_FOUND";
315+
staticGRAPHQL_ERROR="GRAPHQL_ERROR";
309316
}
310317

311318
/**
@@ -427,4 +434,5 @@ export {
427434
lowercaseTrim,
428435
chunkArray,
429436
parseEmojis,
437+
ERROR_CARD_LENGTH,
430438
};

‎src/fetchers/stats-fetcher.js‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
logger,
1010
MissingParamError,
1111
request,
12+
wrapTextMultiline,
1213
}from"../common/utils.js";
1314

1415
dotenv.config();
@@ -207,11 +208,24 @@ async function fetchStats(
207208

208209
letres=awaitretryer(fetcher,{login:username});
209210

211+
// Catch GraphQL errors.
210212
if(res.data.errors){
211213
logger.error(res.data.errors);
214+
if(res.data.errors[0].type==="NOT_FOUND"){
215+
thrownewCustomError(
216+
res.data.errors[0].message||"Could not fetch user.",
217+
CustomError.USER_NOT_FOUND,
218+
);
219+
}
220+
if(res.data.errors[0].message){
221+
thrownewCustomError(
222+
wrapTextMultiline(res.data.errors[0].message,90,1)[0],
223+
res.statusText,
224+
);
225+
}
212226
thrownewCustomError(
213-
res.data.errors[0].message||"Could not fetch user",
214-
CustomError.USER_NOT_FOUND,
227+
"Something went while trying to retrieve the stats data using the GraphQL API.",
228+
CustomError.GRAPHQL_ERROR,
215229
);
216230
}
217231

‎src/fetchers/top-languages-fetcher.js‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
//@ts-check
22
import*asdotenvfrom"dotenv";
33
import{retryer}from"../common/retryer.js";
4-
import{logger,MissingParamError,request}from"../common/utils.js";
4+
import{
5+
CustomError,
6+
logger,
7+
MissingParamError,
8+
request,
9+
wrapTextMultiline,
10+
}from"../common/utils.js";
511

612
dotenv.config();
713

@@ -61,6 +67,27 @@ async function fetchTopLanguages(username, exclude_repo = []) {
6167
throwError(res.data.errors[0].message||"Could not fetch user");
6268
}
6369

70+
// Catch GraphQL errors.
71+
if(res.data.errors){
72+
logger.error(res.data.errors);
73+
if(res.data.errors[0].type==="NOT_FOUND"){
74+
thrownewCustomError(
75+
res.data.errors[0].message||"Could not fetch user.",
76+
CustomError.USER_NOT_FOUND,
77+
);
78+
}
79+
if(res.data.errors[0].message){
80+
thrownewCustomError(
81+
wrapTextMultiline(res.data.errors[0].message,90,1)[0],
82+
res.statusText,
83+
);
84+
}
85+
thrownewCustomError(
86+
"Something went while trying to retrieve the language data using the GraphQL API.",
87+
CustomError.GRAPHQL_ERROR,
88+
);
89+
}
90+
6491
letrepoNodes=res.data.data.user.repositories.nodes;
6592
letrepoToHide={};
6693

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp