How to suppress BadRequestError being thrown by invalid requests to an Express server

Viewed 123

I'm getting this error in my node service - and I know why it's occurring, it was a long debugging process, but essentially some clients are invoking my API with a Content-Length header that mismatches the length of the actual body (the Content-Length header is larger than it should be or the body is being truncated due to poor network conditions). There's not much I can do on the server-side to fix this issue, so I want to suppress these errors since they are setting off a lot of monitors, and creating too much noise along with majorly affecting latency statistics (these failed requests take 5 minutes each for some reason)

BadRequestError: request aborted
  File "/var/app/node_modules/raw-body/index.js", line 231, in IncomingMessage.onAborted
    done(createError(400, 'request aborted', {
  File "node:events", line 390, in IncomingMessage.emit
  File "node:domain", line 537, in IncomingMessage.emit
  File "node:_http_incoming", line 179, in IncomingMessage._destroy
  File "node:internal/streams/destroy", line 102, in _destroy

I've done some digging, and while other people online correctly identify the error and its source, nothing gives an example of code that successfully suppresses it and stops the error from being thrown.

Additionally, I would like to find a way to shorten the time express waits before throwing this error/giving up on the request. I set envoy to have a timeout of 60 seconds on this service, and it should be killing off the request after 60 seconds, but express is hanging onto it for 5 minutes and I cannot seem to figure out why. The 0.3% of requests that are throwing this error are currently about 50% of the compute time for my service.

I've included snippets of what I believe is the relevant middleware here (left a bunch of stuff out like sentry integrations, datadog integrations, etc.) - if anything else would be helpful, let me know and I will add it

const app = express()
...
app.use(express.json({ limit: '1mb' }));
app.use(helmet());
...
app.listen(process.env.APP_PORT ?? 4444)

TLDR; How do I suppress the error above so it fails silently, and how do I shorten the timeout so it fails sooner?

0 Answers
Related