Skip to content

Commit 24ca677

Browse files
committed
Fix timeout parsing, and show timeout on Vercel
1 parent ed53c29 commit 24ca677

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

api/_common/middleware.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ const normalizeUrl = (url) => {
22
return url.startsWith('http') ? url : `https://${url}`;
33
};
44

5-
65
// If present, set a shorter timeout for API requests
7-
const TIMEOUT = parseInt(process.env.API_TIMEOUT_LIMIT, 10) || 60000;
6+
const TIMEOUT = process.env.API_TIMEOUT_LIMIT ? parseInt(process.env.API_TIMEOUT_LIMIT, 10) : 60000;
87

98
// If present, set CORS allowed origins for responses
109
const ALLOWED_ORIGINS = process.env.API_CORS_ORIGIN || '*';
@@ -23,21 +22,23 @@ const headers = {
2322
};
2423

2524

25+
const timeoutErrorMsg = 'You can re-trigger this request, by clicking "Retry"\n'
26+
+ 'If you\'re running your own instance of Web Check, then you can '
27+
+ 'resolve this issue, by increasing the timeout limit in the '
28+
+ '`API_TIMEOUT_LIMIT` environmental variable to a higher value (in milliseconds), '
29+
+ 'or if you\'re hosting on Vercel increase the maxDuration in vercel.json.\n\n'
30+
+ `The public instance currently has a lower timeout of ${TIMEOUT}ms `
31+
+ 'in order to keep running costs affordable, so that Web Check can '
32+
+ 'remain freely available for everyone.';
33+
2634
// A middleware function used by all API routes on all platforms
2735
const commonMiddleware = (handler) => {
2836

2937
// Create a timeout promise, to throw an error if a request takes too long
3038
const createTimeoutPromise = (timeoutMs) => {
3139
return new Promise((_, reject) => {
3240
setTimeout(() => {
33-
const howToResolve = 'You can re-trigger this request, by clicking "Retry"\n'
34-
+ 'If you\'re running your own instance of Web Check, then you can '
35-
+ 'resolve this issue, by increasing the timeout limit in the '
36-
+ '`API_TIMEOUT_LIMIT` environmental variable to a higher value (in milliseconds). \n\n'
37-
+ `The public instance currently has a lower timeout of ${timeoutMs}ms `
38-
+ 'in order to keep running costs affordable, so that Web Check can '
39-
+ 'remain freely available for everyone.';
40-
reject(new Error(`Request timed-out after ${timeoutMs} ms.\n\n${howToResolve}`));
41+
reject(new Error(`Request timed-out after ${timeoutMs} ms`));
4142
}, timeoutMs);
4243
});
4344
};
@@ -69,8 +70,9 @@ const commonMiddleware = (handler) => {
6970
}
7071
} catch (error) {
7172
let errorCode = 500;
72-
if (error.message.includes('timed-out')) {
73+
if (error.message.includes('timed-out') || response.statusCode === 504) {
7374
errorCode = 408;
75+
error.message = `${error.message}\n\n${timeoutErrorMsg}`;
7476
}
7577
response.status(errorCode).json({ error: error.message });
7678
}

0 commit comments

Comments
 (0)