-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcustom-redirect.js
More file actions
150 lines (135 loc) · 5.99 KB
/
Copy pathcustom-redirect.js
File metadata and controls
150 lines (135 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import errorTemplate from './html/error-template.html'
import { HELPER } from './helper-functions.js'
/**
* Generates the error page HTML with the given details
* @param {Object} details - Error details to inject into the template
* @returns {string} The HTML with injected values
*/
function generateErrorPage(details) {
return errorTemplate
.replace('ERROR_CODE', details.errorCode)
.replace('ERROR_TYPE', details.errorType)
.replace('ERROR_MESSAGE', details.errorMessage)
.replace('ERROR_GIF', details.errorGif)
.replace('ENABLE_REPORT_ERROR', details.enableReportError)
.replace('REPORT_ERROR_BUTTON_TEXT', details.reportErrorButtonText)
.replace('REPORT_ERROR_MODAL_HEADER_TEXT', details.reportErrorModalHeaderText)
.replace('REPORT_ERROR_LABEL_PLACEHOLDER', details.reportErrorLabelPlaceholder)
.replace('REPORT_ERROR_MODAL_NAME_PLACEHOLDER', details.reportErrorModalNamePlaceholder)
.replace('REPORT_ERROR_CANCEL_BUTTON_TEXT', details.reportErrorCancelButtonText)
.replace('REPORT_ERROR_SUBMIT_BUTTON_TEXT', details.reportErrorSubmitButtonText)
.replace('REPORT_ERROR_SUCCESS_MESSAGE', details.reportErrorSuccessMessage)
.replace('REPORT_ERROR_FAILURE_MESSAGE', details.reportErrorFailureMessage)
.replace('REPORT_ERROR_CODE', details.errorCode);
}
/**
* Creates an HTTP response with specified content
* @param {string} content - HTML content for the response
* @param {string} statusCode - HTTP status code
* @returns {Response} The formatted response
*/
function makeResponse(content, statusCode) {
return new Response(content, {
status: parseInt(statusCode, 10),
headers: {
'Content-Type': 'text/html',
'X-Worker-Handled': 'true'
}
});
}
/**
* Builds error details object from an error code and env config
* @param {number|string} cfCode - The error code or "MAINTENANCE"
* @param {Object} env - Environment variables
* @returns {Object} Error details for the template
*/
function getErrorDetails(cfCode, env) {
const details = {
errorCode: "500",
errorType: env.TEXT_GENERIC_ERROR_TYPE,
errorMessage: env.TEXT_GENERIC_ERROR_MESSAGE,
errorGif: env.TEXT_GENERIC_ERROR_GIF,
enableReportError: false,
reportErrorButtonText: '',
reportErrorModalHeaderText: '',
reportErrorLabelPlaceholder: '',
reportErrorModalNamePlaceholder: '',
reportErrorCancelButtonText: '',
reportErrorSubmitButtonText: '',
reportErrorSuccessMessage: '',
reportErrorFailureMessage: ''
};
if (cfCode === "MAINTENANCE") {
details.errorCode = "503";
details.errorType = env.TEXT_MAINTENANCE_TYPE;
details.errorMessage = env.TEXT_MAINTENANCE_MESSAGE;
details.errorGif = env.TEXT_MAINTENANCE_GIF;
return details;
}
details.errorCode = cfCode ? cfCode.toString() : "500";
console.log(`Handling error with code: ${details.errorCode}`);
const enableReport = env.ENABLE_REPORT_ERROR === true || env.ENABLE_REPORT_ERROR === 'true';
details.enableReportError = enableReport;
if (enableReport) {
details.reportErrorButtonText = env.REPORT_ERROR_BUTTON_TEXT;
details.reportErrorModalHeaderText = env.REPORT_ERROR_MODAL_HEADER_TEXT;
details.reportErrorLabelPlaceholder = env.REPORT_ERROR_LABEL_PLACEHOLDER;
details.reportErrorModalNamePlaceholder = env.REPORT_ERROR_MODAL_NAME_PLACEHOLDER;
details.reportErrorCancelButtonText = env.REPORT_ERROR_CANCEL_BUTTON_TEXT;
details.reportErrorSubmitButtonText = env.REPORT_ERROR_SUBMIT_BUTTON_TEXT;
details.reportErrorSuccessMessage = env.REPORT_ERROR_SUCCESS_MESSAGE;
details.reportErrorFailureMessage = env.REPORT_ERROR_FAILURE_MESSAGE;
}
const containerCodes = env.TEXT_CONTAINER_ERROR_CODE || [];
const boxCodes = env.TEXT_BOX_ERROR_CODE || [];
const tunnelCodes = env.TEXT_TUNNEL_ERROR_CODE || [];
if (containerCodes.includes(cfCode)) {
details.errorType = env.TEXT_CONTAINER_ERROR_TYPE;
details.errorMessage = env.TEXT_CONTAINER_ERROR_MESSAGE;
details.errorGif = env.TEXT_CONTAINER_ERROR_GIF;
} else if (boxCodes.includes(cfCode)) {
details.errorType = env.TEXT_BOX_ERROR_TYPE;
details.errorMessage = env.TEXT_BOX_ERROR_MESSAGE;
details.errorGif = env.TEXT_BOX_ERROR_GIF;
} else if (tunnelCodes.includes(cfCode)) {
details.errorType = env.TEXT_TUNNEL_ERROR_TYPE;
details.errorMessage = env.TEXT_TUNNEL_ERROR_MESSAGE;
details.errorGif = env.TEXT_TUNNEL_ERROR_GIF;
}
return details;
}
/**
* Main redirection and error handling function
* @param {Request} request - Incoming request
* @param {Response|null} response - Server response if available
* @param {Error|null} thrownError - Thrown error if present
* @param {boolean} isMaintenance - Maintenance mode state
* @param {Object} env - Environment variables
* @returns {Promise<Response|null>} Appropriate error response or null
*/
export async function c_redirect(request, response, thrownError, isMaintenance, env) {
// Maintenance mode
if (isMaintenance) {
const details = getErrorDetails("MAINTENANCE", env);
return makeResponse(generateErrorPage(details), details.errorCode);
}
// Check if origin is reachable (only if ORIGIN_PING_URL is configured)
const originUp = await HELPER.isOriginReachable(undefined, env).catch(() => null);
// Origin confirmed down (originUp === false means the check ran and failed)
// originUp === null means ORIGIN_PING_URL is not configured, so we skip this check
if (originUp === false) {
const details = getErrorDetails(504, env);
return makeResponse(generateErrorPage(details), details.errorCode);
}
// Handle server errors (5xx) from the response
if (response && response.status >= 500) {
const details = getErrorDetails(response.status, env);
return makeResponse(generateErrorPage(details), details.errorCode);
}
// Handle thrown errors (fetch failed entirely, e.g. container down / connection refused)
if (thrownError && !response) {
const details = getErrorDetails(502, env);
return makeResponse(generateErrorPage(details), details.errorCode);
}
return null;
}