-
Notifications
You must be signed in to change notification settings - Fork 237
/
errorServer.js
136 lines (123 loc) · 4.27 KB
/
errorServer.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
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
const path = require('path')
const fs = require('fs')
const { getNunjucksAppEnv } = require('./nunjucks/nunjucksConfiguration')
const { getErrorModel } = require('./utils/errorModel')
const { verboseLog } = require('./utils/verboseLogger')
const syncChanges = require('./sync-changes')
const { flagError } = require('./sync-changes')
const { packageDir } = require('./utils/paths')
const { getInternalGovukFrontendDir } = require('./utils')
function runErrorServer (error) {
flagError(error)
let port
verboseLog('- - - Raw Error Start - - -')
verboseLog(JSON.stringify({ message: error.message, stack: error.stack }))
verboseLog('- - - Raw Error End - - -')
try {
port = require('./config.js').getConfig().port
} catch (e) {
port = process.env.PORT || 3000
}
const proxyPort = port - 50
const http = require('http')
const fileExtensionsToMimeTypes = {
js: 'application/javascript',
css: 'text/css'
}
const knownPaths = {
'/public/stylesheets/application.css': {
type: 'text/css',
contents: fs.readFileSync(path.join(process.cwd(), '.tmp', 'public', 'stylesheets', 'manage-prototype.css'))
}
}
const requestListener = function (req, res) {
if (req.url.startsWith('/browser-sync')) {
return
}
if (req.url.startsWith('/manage-prototype/page-loaded')) {
const result = syncChanges.pageLoaded()
res.end(JSON.stringify(result))
return
}
if (knownPaths[req.url]) {
res.setHeader('Content-Type', knownPaths[req.url].type)
res.writeHead(200)
res.end(knownPaths[req.url].contents)
return
}
if (req.url.startsWith('/plugin-assets')) {
res.setHeader('Content-Type', fileExtensionsToMimeTypes[req.url.split('.').at(-1)] || 'text/plain')
const urlParts = req.url.split('/').slice(2)
const pluginName = urlParts.shift()
let filePath
if (pluginName === 'govuk-frontend') {
filePath = path.join(getInternalGovukFrontendDir(), ...urlParts)
} else {
filePath = path.join(process.cwd(), 'node_modules', pluginName, ...urlParts)
}
try {
const contents = fs.readFileSync(filePath)
res.writeHead(200)
res.end(contents)
} catch (e) {
console.log('Couldn\'t load url in error server: ', req.url)
res.writeHead(500)
res.end('500 Server Error')
}
return
}
res.setHeader('Cookies', '')
res.setHeader('Content-Type', 'text/html')
res.writeHead(500)
const fileContentsParts = []
try {
const nunjucksAppEnv = getNunjucksAppEnv([
path.join(__dirname, 'nunjucks'),
path.join(packageDir, 'node_modules', 'govuk-frontend'),
path.join(process.cwd(), 'node_modules', 'govuk-frontend')
])
res.end(nunjucksAppEnv.render('views/error-handling/server-error', getErrorModel(error)))
} catch (ignoreThisError) {
console.log(JSON.stringify({ ignoreThisError }, null, 2))
fileContentsParts.push('<h1 class="govuk-heading-l">There is an error</h1>')
fileContentsParts.push('<p>')
fileContentsParts.push('You can try and fix this yourself or <a class="govuk-link" href="https://prototype-kit.service.gov.uk/docs/support">contact the GOV.UK Prototype Kit team</a> if you need help.')
fileContentsParts.push('</p>')
fileContentsParts.push('<pre><code>')
fileContentsParts.push(error.stack)
fileContentsParts.push('</code></pre>')
const refreshScript = `
<script>
const sendPageLoadedRequest = function () {
fetch('/manage-prototype/page-loaded').catch(() => {
setTimeout(sendPageLoadedRequest, 500)
})
}
sendPageLoadedRequest()
</script>
`
res.end(fileContentsParts.join('\n') + refreshScript)
}
}
const server = http.createServer(requestListener)
server.listen(proxyPort, () => {
console.log('')
console.log('')
console.log(`There's an error, you can see it below or at http://localhost:${port}`)
console.log('')
console.log('')
console.log('')
console.log(error)
console.log('')
console.log('')
console.log('')
syncChanges.sync({
port,
proxyPort,
files: ['app/**/*.*']
})
})
}
module.exports = {
runErrorServer
}