Merge pull request #6 from CountBleck/fixes

More error-handling fixes
This commit is contained in:
Green! 2022-08-30 22:53:04 -04:00 committed by GitHub
commit 05020a75ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 17 deletions

15
app.js
View file

@ -19,6 +19,7 @@ const serve = new nodeStatic.Server(join(
const server = http.createServer(); const server = http.createServer();
server.on('request', (request, response) => { server.on('request', (request, response) => {
try {
if (custombare.route(request, response)) return true; if (custombare.route(request, response)) return true;
if (bareServer.shouldRoute(request)) { if (bareServer.shouldRoute(request)) {
@ -26,6 +27,12 @@ server.on('request', (request, response) => {
} else { } else {
serve.serve(request, response); serve.serve(request, response);
} }
} catch (e) {
response.writeHead(500, "Internal Server Error", {
"Content-Type": "text/plain"
})
response.end(e.stack)
}
}); });
server.on('upgrade', (req, socket, head) => { server.on('upgrade', (req, socket, head) => {
if (bareServer.shouldRoute(req)) { if (bareServer.shouldRoute(req)) {
@ -37,4 +44,12 @@ server.on('upgrade', (req, socket, head) => {
server.listen(PORT); server.listen(PORT);
if (process.env.UNSAFE_CONTINUE)
process.on("uncaughtException", (err, origin) => {
console.error(`Critical error (${origin}):`)
console.error(err)
console.error("UNSAFELY CONTINUING EXECUTION")
console.error()
})
console.log(`Server running at http://localhost:${PORT}/.`); console.log(`Server running at http://localhost:${PORT}/.`);

View file

@ -51,15 +51,7 @@ async function fetchBare(url, res, req) {
}, },
} }
try {
var request = await fetch(url.href, options); var request = await fetch(url.href, options);
} catch (e) {
var request = {
text() {
return 'Error: ' + e;
},
}
}
try { try {
var contentType = request.headers.get('content-type') || 'application/javascript' var contentType = request.headers.get('content-type') || 'application/javascript'
@ -90,8 +82,8 @@ async function fetchBare(url, res, req) {
request.body.pipe(res) request.body.pipe(res)
} }
} catch (e) { } catch (e) {
res.writeHead(500, 'Error', { res.writeHead(500, 'Internal Server Error', {
'content-type': 'application/javascript' 'Content-Type': 'text/plain'
}) })
res.end(e.stack); res.end(e.stack);
} }