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

Commitc504c93

Browse files
committed
my quick tests for implementing stale-if-error
1 parentaa2e8ff commitc504c93

File tree

9 files changed

+122
-63
lines changed

9 files changed

+122
-63
lines changed

‎api/index.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
parseBoolean,
88
renderError,
99
}from"../src/common/utils.js";
10+
import{HttpException}from"../src/common/exceptions.js";
1011
import{fetchStats}from"../src/fetchers/stats-fetcher.js";
1112
import{isLocaleAvailable}from"../src/translations.js";
1213

@@ -65,7 +66,8 @@ export default async (req, res) => {
6566
"Cache-Control",
6667
`max-age=${
6768
cacheSeconds/2
68-
}, s-maxage=${cacheSeconds}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
69+
}, s-maxage=${cacheSeconds}, stale-while-revalidate=${CONSTANTS.ONE_DAY},
70+
stale-if-error=${CONSTANTS.ONE_HOUR}`,
6971
);
7072

7173
returnres.send(
@@ -93,12 +95,27 @@ export default async (req, res) => {
9395
}),
9496
);
9597
}catch(err){
98+
// Throw error if REST and GraphQL API calls fail. This way we can return a cached
99+
if(errinstanceofHttpException){
100+
// throw err;
101+
// throw new Error(err.message);
102+
// return res.status(404).;
103+
// return {statusCode: 404, body: err.message}
104+
returnres
105+
.status(500)
106+
.send(
107+
renderError(err.errors[0].message,err.errors[0].secondaryMessage),
108+
);
109+
}
110+
111+
// Cache the error response less frequently.
96112
res.setHeader(
97113
"Cache-Control",
98114
`max-age=${CONSTANTS.ERROR_CACHE_SECONDS/2}, s-maxage=${
99115
CONSTANTS.ERROR_CACHE_SECONDS
100-
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
101-
);// Cache the error response less frequently.
116+
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}
117+
stale-if-error=${CONSTANTS.ONE_HOUR}`,
118+
);
102119
returnres.send(renderError(err.message,err.secondaryMessage));
103120
}
104121
};

‎src/common/exceptions.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
*@file GRS Exception/Errors.
3+
*/
4+
5+
constSECONDARY_ERROR_MESSAGES={
6+
MAX_RETRY:
7+
"Please add an env variable called PAT_1 with your github token in vercel",
8+
USER_NOT_FOUND:"Make sure the provided username is not an organization",
9+
GRAPHQL_ERROR:"Please try again later",
10+
};
11+
12+
/**
13+
* Custom error class to handle custom GRS errors.
14+
*
15+
*@extends Error
16+
*/
17+
classCustomErrorextendsError{
18+
/**
19+
*@param {string} message Error message.
20+
*@param {string} type Error type.
21+
*/
22+
constructor(message,type){
23+
super(message);
24+
this.type=type;
25+
this.secondaryMessage=SECONDARY_ERROR_MESSAGES[type]||type;
26+
}
27+
28+
staticMAX_RETRY="MAX_RETRY";
29+
staticUSER_NOT_FOUND="USER_NOT_FOUND";
30+
staticGRAPHQL_ERROR="GRAPHQL_ERROR";
31+
}
32+
33+
/**
34+
* Missing query parameter class.
35+
*
36+
*@extends Error
37+
*/
38+
classMissingParamErrorextendsError{
39+
/**
40+
*@param {string[]} missedParams
41+
*@param {string?=} secondaryMessage
42+
*/
43+
constructor(missedParams,secondaryMessage){
44+
constmsg=`Missing params${missedParams
45+
.map((p)=>`"${p}"`)
46+
.join(", ")} make sure you pass the parameters in URL`;
47+
super(msg);
48+
this.missedParams=missedParams;
49+
this.secondaryMessage=secondaryMessage;
50+
}
51+
}
52+
53+
/**
54+
* HttpException class.
55+
*
56+
*@extends Error
57+
*/
58+
classHttpExceptionextendsError{
59+
/**
60+
* Create a HttpException.
61+
*
62+
*@param {number} statusCode - The status code.
63+
*@param {string} message - The error message.
64+
*@param {string[]} errors - The errors.
65+
*/
66+
constructor(
67+
statusCode,
68+
message="Exception occurred during the processing of HTTP requests.",
69+
errors=[],
70+
){
71+
super(message);
72+
this.statusCode=statusCode;
73+
this.errors=errors;
74+
}
75+
}
76+
77+
export{
78+
SECONDARY_ERROR_MESSAGES,
79+
CustomError,
80+
MissingParamError,
81+
HttpException,
82+
};

‎src/common/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ export {
2121
wrapTextMultiline,
2222
logger,
2323
CONSTANTS,
24-
CustomError,
25-
MissingParamError,
2624
measureText,
2725
lowercaseTrim,
2826
chunkArray,
2927
parseEmojis,
3028
}from"./utils.js";
29+
export{CustomError,MissingParamError,HttpException}from"./exceptions.js";

‎src/common/retryer.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import{CustomError,logger}from"./utils.js";
1+
import{logger}from"./utils.js";
2+
import{
3+
CustomError,
4+
HttpException,
5+
}from"./exceptions.js";
26

37
// Script variables.
48
constPATs=Object.keys(process.env).filter((key)=>
@@ -16,8 +20,13 @@ const RETRIES = PATs ? PATs : 7;
1620
*@returns Promise<retryer>
1721
*/
1822
constretryer=async(fetcher,variables,retries=0)=>{
19-
if(retries>RETRIES){
20-
thrownewCustomError("Maximum retries exceeded",CustomError.MAX_RETRY);
23+
// if (retries > RETRIES) {
24+
if(true){// FIXME: Test out error
25+
thrownewHttpException(
26+
statusCode=500,
27+
message=`Max GraphQL retries exceeded. Please add an env variable called PAT_1 with your github token in vercel.`,
28+
errors=[newCustomError("Maximum retries exceeded",CustomError.MAX_RETRY)]
29+
);
2130
}
2231
try{
2332
// try to fetch with the first token since RETRIES is 0 index i'm adding +1

‎src/common/utils.js

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ const renderError = (message, secondaryMessage = "") => {
2222
.small { font: 600 12px 'Segoe UI', Ubuntu, Sans-Serif; fill: #252525 }
2323
.gray { fill: #858585 }
2424
</style>
25-
<rect x="0.5" y="0.5" width="${
26-
ERROR_CARD_LENGTH-1
25+
<rect x="0.5" y="0.5" width="${ERROR_CARD_LENGTH-1
2726
}" height="99%" rx="4.5" fill="#FFFEFE" stroke="#E4E2E2"/>
2827
<text x="25" y="45" class="text">Something went wrong! file an issue at https://tiny.one/readme-stats</text>
2928
<text data-testid="message" x="25" y="55" class="text small">
@@ -288,7 +287,7 @@ const wrapTextMultiline = (text, width = 59, maxLines = 3) => {
288287
returnmultiLineText;
289288
};
290289

291-
constnoop=()=>{};
290+
constnoop=()=>{};
292291
// return console instance based on the environment
293292
constlogger=
294293
process.env.NODE_ENV!=="test" ?console :{log:noop,error:noop};
@@ -310,50 +309,6 @@ const CONSTANTS = {
310309
ERROR_CACHE_SECONDS:ERROR_CACHE_SECONDS,
311310
};
312311

313-
constSECONDARY_ERROR_MESSAGES={
314-
MAX_RETRY:
315-
"Please add an env variable called PAT_1 with your github token in vercel",
316-
USER_NOT_FOUND:"Make sure the provided username is not an organization",
317-
GRAPHQL_ERROR:"Please try again later",
318-
};
319-
320-
/**
321-
* Custom error class to handle custom GRS errors.
322-
*/
323-
classCustomErrorextendsError{
324-
/**
325-
*@param {string} message Error message.
326-
*@param {string} type Error type.
327-
*/
328-
constructor(message,type){
329-
super(message);
330-
this.type=type;
331-
this.secondaryMessage=SECONDARY_ERROR_MESSAGES[type]||type;
332-
}
333-
334-
staticMAX_RETRY="MAX_RETRY";
335-
staticUSER_NOT_FOUND="USER_NOT_FOUND";
336-
staticGRAPHQL_ERROR="GRAPHQL_ERROR";
337-
}
338-
339-
/**
340-
* Missing query parameter class.
341-
*/
342-
classMissingParamErrorextendsError{
343-
/**
344-
*@param {string[]} missedParams
345-
*@param {string?=} secondaryMessage
346-
*/
347-
constructor(missedParams,secondaryMessage){
348-
constmsg=`Missing params${missedParams
349-
.map((p)=>`"${p}"`)
350-
.join(", ")} make sure you pass the parameters in URL`;
351-
super(msg);
352-
this.missedParams=missedParams;
353-
this.secondaryMessage=secondaryMessage;
354-
}
355-
}
356-
357312
/**
358313
* Retrieve text length.
359314
*
@@ -451,8 +406,6 @@ export {
451406
wrapTextMultiline,
452407
logger,
453408
CONSTANTS,
454-
CustomError,
455-
MissingParamError,
456409
measureText,
457410
lowercaseTrim,
458411
chunkArray,

‎src/fetchers/repo-fetcher.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@ts-check
22
import{retryer}from"../common/retryer.js";
3-
import{MissingParamError,request}from"../common/utils.js";
3+
import{request}from"../common/utils.js";
4+
import{MissingParamError}from"../common/exceptions.js";
45

56
/**
67
* Repo data fetcher.

‎src/fetchers/stats-fetcher.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ import githubUsernameRegex from "github-username-regex";
55
import{calculateRank}from"../calculateRank.js";
66
import{retryer}from"../common/retryer.js";
77
import{
8-
CustomError,
98
logger,
10-
MissingParamError,
119
request,
1210
wrapTextMultiline,
1311
}from"../common/utils.js";
12+
import{CustomError,MissingParamError}from"../common/exceptions.js";
1413

1514
dotenv.config();
1615

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
//@ts-check
22
import{retryer}from"../common/retryer.js";
33
import{
4-
CustomError,
54
logger,
6-
MissingParamError,
75
request,
86
wrapTextMultiline,
97
}from"../common/utils.js";
8+
import{CustomError,MissingParamError}from"../common/exceptions.js";
109

1110
/**
1211
* Top languages fetcher object.

‎src/fetchers/wakatime-fetcher.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
importaxiosfrom"axios";
2-
import{MissingParamError}from"../common/utils.js";
2+
import{MissingParamError}from"../common/exceptions.js";
33

44
/**
55
* WakaTime data fetcher.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp