Skip to content

Commit

Permalink
Try to address an unhandled exception that's crashing instances (#4519)
Browse files Browse the repository at this point in the history
* Try to address an unhandled exception that's crashing instances

* Quick amendment
  • Loading branch information
jeremy-rifkin authored and mattgodbolt committed Jan 24, 2023
1 parent 0c04c89 commit a9ef08f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
27 changes: 20 additions & 7 deletions lib/handlers/compile.ts
Expand Up @@ -132,7 +132,7 @@ export class CompileHandler {
// https://github.com/nodejitsu/node-http-proxy/blob/master/examples/middleware/bodyDecoder-middleware.js
// We just keep the body as-is though: no encoding using queryString.stringify(), as we don't use a form
// decoding middleware.
this.proxy.on('proxyReq', function (proxyReq, req) {
this.proxy.on('proxyReq', (proxyReq, req) => {
// TODO ideally I'd work out if this is "ok" - IncomingMessage doesn't have a body, but pragmatically the
// object we get here does.
const body = (req as any).body;
Expand All @@ -144,15 +144,28 @@ export class CompileHandler {

if (contentType === 'application/json') {
bodyData = JSON.stringify(body);
}

if (contentType === 'application/x-www-form-urlencoded') {
} else if (contentType === 'application/x-www-form-urlencoded') {
bodyData = body;
} else {
Sentry.captureException(
new Error(`Unexpected Content-Type received by /compiler/:compiler/compile: ${contentType}`),
);
proxyReq.write('Unexpected Content-Type');
}

if (bodyData) {
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
proxyReq.write(bodyData);
try {
if (bodyData) {
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
proxyReq.write(bodyData);
}
} catch (e: any) {
Sentry.captureException(e);
let json = '<json stringify error>';
try {
json = JSON.stringify(bodyData);
} catch (e) {}
Sentry.captureMessage(`Unknown proxy bodyData: ${bodyData}, JSON.stringify: ${json}`);
proxyReq.write('Proxy error');
}
});
}
Expand Down
8 changes: 2 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -24,7 +24,9 @@
"@fortawesome/fontawesome-free": "^6.2.1",
"@sentry/browser": "^7.28.1",
"@sentry/node": "^7.28.1",
"@types/body-parser": "^1.19.2",
"@types/file-saver": "^2.0.5",
"@types/http-proxy": "^1.17.9",
"@types/request": "^2.48.8",
"aws-sdk": "^2.1048.0",
"big-integer": "^1.6.51",
Expand Down

0 comments on commit a9ef08f

Please sign in to comment.