Skip to content

Commit 8b015f1

Browse files
committed
refactor
1 parent d012052 commit 8b015f1

File tree

2 files changed

+106
-94
lines changed

2 files changed

+106
-94
lines changed

custom-redirect.js

Lines changed: 105 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,111 @@
11
import errorTemplate from './html/error-template.html'
22
import { HELPER } from './helper-functions.js'
33

4-
// Default error values, will be set in c_redirect using env
5-
let errorCode = "500";
6-
let errorType = "";
7-
let errorMessage = "";
8-
let errorGif = "";
9-
let enableReportError = false;
10-
let reportErrorButtonText = 'Signaler cette erreur';
11-
let reportErrorModalHeaderText = "🆘 Signalez-moi l'erreur"
12-
let reportErrorLabelPlaceholder = "Nom / Pseudo"
13-
let reportErrorModalNamePlaceholder = "Ex : Jane Doe"
14-
let reportErrorCancelButtonText = "Annuler"
15-
let reportErrorSubmitButtonText = "Signaler"
16-
let reportErrorSuccessMessage = "Merci pour votre signalement ! 🙏"
17-
let reportErrorFailureMessage = "Une erreur est survenue lors de l'envoi du signalement. 😞"
18-
19-
const REDIRECT = {
20-
/**
21-
* Generates HTML content with appropriate Canva URL
22-
* @returns {string} The HTML with injected URL
23-
*/
24-
generateErrorPage: () => {
25-
return errorTemplate
26-
.replace('ERROR_CODE', errorCode)
27-
.replace('ERROR_TYPE', errorType)
28-
.replace('ERROR_MESSAGE', errorMessage)
29-
.replace('ERROR_GIF', errorGif)
30-
.replace('ENABLE_REPORT_ERROR', enableReportError)
31-
.replace('REPORT_ERROR_BUTTON_TEXT', reportErrorButtonText)
32-
.replace('REPORT_ERROR_MODAL_HEADER_TEXT', reportErrorModalHeaderText)
33-
.replace('REPORT_ERROR_LABEL_PLACEHOLDER', reportErrorLabelPlaceholder)
34-
.replace('REPORT_ERROR_MODAL_NAME_PLACEHOLDER', reportErrorModalNamePlaceholder)
35-
.replace('REPORT_ERROR_CANCEL_BUTTON_TEXT', reportErrorCancelButtonText)
36-
.replace('REPORT_ERROR_SUBMIT_BUTTON_TEXT', reportErrorSubmitButtonText)
37-
.replace('REPORT_ERROR_SUCCESS_MESSAGE', reportErrorSuccessMessage)
38-
.replace('REPORT_ERROR_FAILURE_MESSAGE', reportErrorFailureMessage)
39-
.replace('REPORT_ERROR_CODE', errorCode);
40-
}
41-
};
42-
4+
/**
5+
* Generates the error page HTML with the given details
6+
* @param {Object} details - Error details to inject into the template
7+
* @returns {string} The HTML with injected values
8+
*/
9+
function generateErrorPage(details) {
10+
return errorTemplate
11+
.replace('ERROR_CODE', details.errorCode)
12+
.replace('ERROR_TYPE', details.errorType)
13+
.replace('ERROR_MESSAGE', details.errorMessage)
14+
.replace('ERROR_GIF', details.errorGif)
15+
.replace('ENABLE_REPORT_ERROR', details.enableReportError)
16+
.replace('REPORT_ERROR_BUTTON_TEXT', details.reportErrorButtonText)
17+
.replace('REPORT_ERROR_MODAL_HEADER_TEXT', details.reportErrorModalHeaderText)
18+
.replace('REPORT_ERROR_LABEL_PLACEHOLDER', details.reportErrorLabelPlaceholder)
19+
.replace('REPORT_ERROR_MODAL_NAME_PLACEHOLDER', details.reportErrorModalNamePlaceholder)
20+
.replace('REPORT_ERROR_CANCEL_BUTTON_TEXT', details.reportErrorCancelButtonText)
21+
.replace('REPORT_ERROR_SUBMIT_BUTTON_TEXT', details.reportErrorSubmitButtonText)
22+
.replace('REPORT_ERROR_SUCCESS_MESSAGE', details.reportErrorSuccessMessage)
23+
.replace('REPORT_ERROR_FAILURE_MESSAGE', details.reportErrorFailureMessage)
24+
.replace('REPORT_ERROR_CODE', details.errorCode);
25+
}
4326

4427
/**
4528
* Creates an HTTP response with specified content
4629
* @param {string} content - HTML content for the response
30+
* @param {string} statusCode - HTTP status code
4731
* @returns {Response} The formatted response
4832
*/
49-
function makeResponse(content) {
33+
function makeResponse(content, statusCode) {
5034
return new Response(content, {
51-
status: errorCode,
35+
status: parseInt(statusCode, 10),
5236
headers: {
5337
'Content-Type': 'text/html',
5438
'X-Worker-Handled': 'true'
5539
}
5640
});
5741
}
5842

59-
function getErrorDetailsFromCfCode(cfCode, env) {
60-
if(cfCode == "MAINTENANCE") {
61-
errorCode = "503";
62-
errorType = env.TEXT_MAINTENANCE_TYPE;
63-
errorMessage = env.TEXT_MAINTENANCE_MESSAGE;
64-
errorGif = env.TEXT_MAINTENANCE_GIF;
65-
return;
43+
/**
44+
* Builds error details object from an error code and env config
45+
* @param {number|string} cfCode - The error code or "MAINTENANCE"
46+
* @param {Object} env - Environment variables
47+
* @returns {Object} Error details for the template
48+
*/
49+
function getErrorDetails(cfCode, env) {
50+
const details = {
51+
errorCode: "500",
52+
errorType: env.TEXT_GENERIC_ERROR_TYPE,
53+
errorMessage: env.TEXT_GENERIC_ERROR_MESSAGE,
54+
errorGif: env.TEXT_GENERIC_ERROR_GIF,
55+
enableReportError: false,
56+
reportErrorButtonText: '',
57+
reportErrorModalHeaderText: '',
58+
reportErrorLabelPlaceholder: '',
59+
reportErrorModalNamePlaceholder: '',
60+
reportErrorCancelButtonText: '',
61+
reportErrorSubmitButtonText: '',
62+
reportErrorSuccessMessage: '',
63+
reportErrorFailureMessage: ''
64+
};
65+
66+
if (cfCode === "MAINTENANCE") {
67+
details.errorCode = "503";
68+
details.errorType = env.TEXT_MAINTENANCE_TYPE;
69+
details.errorMessage = env.TEXT_MAINTENANCE_MESSAGE;
70+
details.errorGif = env.TEXT_MAINTENANCE_GIF;
71+
return details;
6672
}
67-
errorCode = cfCode ? cfCode.toString() : "500";
68-
enableReportError = env.ENABLE_REPORT_ERROR;
69-
if(enableReportError) {
70-
reportErrorButtonText = env.REPORT_ERROR_BUTTON_TEXT
71-
reportErrorModalHeaderText = env.REPORT_ERROR_MODAL_HEADER_TEXT
72-
reportErrorLabelPlaceholder = env.REPORT_ERROR_LABEL_PLACEHOLDER
73-
reportErrorModalNamePlaceholder = env.REPORT_ERROR_MODAL_NAME_PLACEHOLDER
74-
reportErrorCancelButtonText = env.REPORT_ERROR_CANCEL_BUTTON_TEXT
75-
reportErrorSubmitButtonText = env.REPORT_ERROR_SUBMIT_BUTTON_TEXT
76-
reportErrorSuccessMessage = env.REPORT_ERROR_SUCCESS_MESSAGE
77-
reportErrorFailureMessage = env.REPORT_ERROR_FAILURE_MESSAGE
73+
74+
details.errorCode = cfCode ? cfCode.toString() : "500";
75+
console.log(`Handling error with code: ${details.errorCode}`);
76+
77+
const enableReport = env.ENABLE_REPORT_ERROR === true || env.ENABLE_REPORT_ERROR === 'true';
78+
details.enableReportError = enableReport;
79+
if (enableReport) {
80+
details.reportErrorButtonText = env.REPORT_ERROR_BUTTON_TEXT;
81+
details.reportErrorModalHeaderText = env.REPORT_ERROR_MODAL_HEADER_TEXT;
82+
details.reportErrorLabelPlaceholder = env.REPORT_ERROR_LABEL_PLACEHOLDER;
83+
details.reportErrorModalNamePlaceholder = env.REPORT_ERROR_MODAL_NAME_PLACEHOLDER;
84+
details.reportErrorCancelButtonText = env.REPORT_ERROR_CANCEL_BUTTON_TEXT;
85+
details.reportErrorSubmitButtonText = env.REPORT_ERROR_SUBMIT_BUTTON_TEXT;
86+
details.reportErrorSuccessMessage = env.REPORT_ERROR_SUCCESS_MESSAGE;
87+
details.reportErrorFailureMessage = env.REPORT_ERROR_FAILURE_MESSAGE;
7888
}
7989

80-
if (env.TEXT_CONTAINER_ERROR_CODE.includes(cfCode)) {
81-
errorType = env.TEXT_CONTAINER_ERROR_TYPE;
82-
errorMessage = env.TEXT_CONTAINER_ERROR_MESSAGE;
83-
errorGif = env.TEXT_CONTAINER_ERROR_GIF;
84-
} else if (env.TEXT_BOX_ERROR_CODE.includes(cfCode)) {
85-
errorType = env.TEXT_BOX_ERROR_TYPE;
86-
errorMessage = env.TEXT_BOX_ERROR_MESSAGE;
87-
errorGif = env.TEXT_BOX_ERROR_GIF;
88-
} else if (env.TEXT_TUNNEL_ERROR_CODE.includes(cfCode)) {
89-
errorType = env.TEXT_TUNNEL_ERROR_TYPE;
90-
errorMessage = env.TEXT_TUNNEL_ERROR_MESSAGE;
91-
errorGif = env.TEXT_TUNNEL_ERROR_GIF;
90+
const containerCodes = env.TEXT_CONTAINER_ERROR_CODE || [];
91+
const boxCodes = env.TEXT_BOX_ERROR_CODE || [];
92+
const tunnelCodes = env.TEXT_TUNNEL_ERROR_CODE || [];
93+
94+
if (containerCodes.includes(cfCode)) {
95+
details.errorType = env.TEXT_CONTAINER_ERROR_TYPE;
96+
details.errorMessage = env.TEXT_CONTAINER_ERROR_MESSAGE;
97+
details.errorGif = env.TEXT_CONTAINER_ERROR_GIF;
98+
} else if (boxCodes.includes(cfCode)) {
99+
details.errorType = env.TEXT_BOX_ERROR_TYPE;
100+
details.errorMessage = env.TEXT_BOX_ERROR_MESSAGE;
101+
details.errorGif = env.TEXT_BOX_ERROR_GIF;
102+
} else if (tunnelCodes.includes(cfCode)) {
103+
details.errorType = env.TEXT_TUNNEL_ERROR_TYPE;
104+
details.errorMessage = env.TEXT_TUNNEL_ERROR_MESSAGE;
105+
details.errorGif = env.TEXT_TUNNEL_ERROR_GIF;
92106
}
107+
108+
return details;
93109
}
94110

95111
/**
@@ -101,37 +117,33 @@ function getErrorDetailsFromCfCode(cfCode, env) {
101117
* @param {Object} env - Environment variables
102118
* @returns {Promise<Response|null>} Appropriate error response or null
103119
*/
104-
export async function c_redirect(request, response, thrownError = null, isMaintenance = false, env) {
105-
// Set default error details using env inside the function
106-
errorType = env.TEXT_GENERIC_ERROR_TYPE;
107-
errorMessage = env.TEXT_GENERIC_ERROR_MESSAGE;
108-
errorGif = env.TEXT_GENERIC_ERROR_GIF;
109-
120+
export async function c_redirect(request, response, thrownError, isMaintenance, env) {
110121
// Maintenance mode
111122
if (isMaintenance) {
112-
getErrorDetailsFromCfCode("MAINTENANCE", env);
113-
return makeResponse(REDIRECT.generateErrorPage());
123+
const details = getErrorDetails("MAINTENANCE", env);
124+
return makeResponse(generateErrorPage(details), details.errorCode);
114125
}
115126

127+
// Check if origin is reachable (only if ORIGIN_PING_URL is configured)
116128
const originUp = await HELPER.isOriginReachable(undefined, env).catch(() => null);
117-
//const npmUp = await HELPER.isNpmUp(undefined, env).catch(() => false);
118129

119-
// Internet down
120-
if(!originUp) {
121-
getErrorDetailsFromCfCode(504, env);
122-
return makeResponse(REDIRECT.generateErrorPage());
130+
// Origin confirmed down (originUp === false means the check ran and failed)
131+
// originUp === null means ORIGIN_PING_URL is not configured, so we skip this check
132+
if (originUp === false) {
133+
const details = getErrorDetails(504, env);
134+
return makeResponse(generateErrorPage(details), details.errorCode);
123135
}
124136

125-
/*
126-
// NPM down so all services down
127-
if(!npmUp) {
128-
// it's the default message so no need to change anything
129-
} */
137+
// Handle server errors (5xx) from the response
138+
if (response && response.status >= 500) {
139+
const details = getErrorDetails(response.status, env);
140+
return makeResponse(generateErrorPage(details), details.errorCode);
141+
}
130142

131-
// Handle server errors (5xx)
132-
if(response && response.status >= 500) {
133-
getErrorDetailsFromCfCode(response.status, env);
134-
return makeResponse(REDIRECT.generateErrorPage());
143+
// Handle thrown errors (fetch failed entirely, e.g. container down / connection refused)
144+
if (thrownError && !response) {
145+
const details = getErrorDetails(502, env);
146+
return makeResponse(generateErrorPage(details), details.errorCode);
135147
}
136148

137149
return null;

worker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export default {
146146
const url = new URL(request.url);
147147

148148
// Handle /report-error POST for Discord webhook if enabled
149-
if (env.ENABLE_REPORT_ERROR === true && url.pathname === '/report-error' && request.method === 'POST') {
149+
if ((env.ENABLE_REPORT_ERROR === true || env.ENABLE_REPORT_ERROR === 'true') && url.pathname === '/report-error' && request.method === 'POST') {
150150
return await handleReportError(request, env);
151151
}
152152

0 commit comments

Comments
 (0)