forked from cloudflare/worker-template-router
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patherror_handling.js
106 lines (97 loc) · 2.96 KB
/
error_handling.js
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
import {
CACHE_ERRORS_SECONDS,
PLAIN_TEXT_CONTENT_TYPE,
} from './constants'
/**
* A face will be picked from this list at random. Add and remove faces to suit
* your needs/preferences.
*
* @type {string[]}
*/
const ERROR_FACES = [
"¯\\_(ツ)_/¯",
"(⊙_⊙)?",
"ಥ_ಥ",
"\(〇_o)/",
"¯\\(°_o)/¯",
"╮(╯_╰)╭",
"╮(╯▽╰)╭",
"(⊙_⊙;)",
"(°ロ°) !",
"┐('~`;)┌",
"┐( ̄ヘ ̄;)┌",
"( ಠ ʖ̯ ಠ)",
"ლ(ಠ_ಠ ლ)",
`ლ(¯ロ¯"ლ)`,
"┐( ̄ヮ ̄)┌",
"(눈_눈)",
"(ノ◕ヮ◕)ノ*:・゚✧",
"(ಠ_ಠ)",
"( ̄﹃ ̄)",
"(ʘ ͟ʖ ʘ)",
"( ಥ ʖ̯ ಥ)",
"( ͡° ʖ̯ ͡°)",
]
/**
* A map of response codes to what they mean so we can display some descriptive
* text instead of just a goofy random face when a problem occurs.
*/
const ERROR_CODES = {
"400": "Bad Request",
"401": "Unauthorized",
"402": "Payment Required",
"403": "Forbidden",
"404": "Not Found",
"405": "Method Not Allowed",
"406": "Not Acceptable",
"407": "Proxy Authentication Required",
"408": "Request Timeout",
"409": "Conflict",
"410": "Gone",
"411": "Length Required",
"412": "Precondition Required",
"413": "Request Entry Too Large",
"414": "Request-URI Too Long",
"415": "Unsupported Media Type",
"416": "Requested Range Not Satisfiable",
"417": "Expectation Failed",
"500": "Internal Server Error",
"501": "Not Implemented",
"502": "Bad Gateway",
"503": "Service Unavailable",
"504": "Gateway Timeout",
"505": "HTTP Version Not Supported",
}
export function getFacesPage() {
return new Response(`${ERROR_FACES.join(" \n")}`, {
headers: {"Content-Type": PLAIN_TEXT_CONTENT_TYPE}
})
}
/**
* Rewrite error responses from Backblaze B2 to have fun little faces. This
* obfuscates the fact that Backblaze B2 is being used and is generally a
* more succinct error page for end users.
*
* @param {Request} request the request from the client to B2
* @param {Response} response the response from B2 to copy and modify
* @returns {Promise<Response>} the new response with a face and description
*/
export async function rewriteErrorResponse(request, response) {
console.log("rewriteErrorResponse...")
console.log("Original error response:")
console.log(response)
// pick a random face from our ERROR_FACES array
const randomIdx = Math.floor(Math.random() * ERROR_FACES.length)
const randomFace = ERROR_FACES[randomIdx]
const statusText = ERROR_CODES[response.status]
let responseBody = `${statusText}\n${randomFace}`
return new Response(responseBody, {
status: response.status,
statusText: statusText,
headers: {
"Cache-Control": `public, immutable, max-age=${CACHE_ERRORS_SECONDS}`,
"Content-Type": PLAIN_TEXT_CONTENT_TYPE,
}
})
}
export default rewriteErrorResponse