Loading page...Загружаем страницу...正在加载页面...
AtlasRepo

Sentry caught my Express error. The request still hung forever

说明与用途

在 Express 4 中,async 路由在 await 后抛出的错误会变成未处理的拒绝,导致请求挂起。解决方法是将 async 处理程序包装为 .catch(next) 或升级到 Express 5。

问题

Express 4 async route throws after await, Sentry reports error but request never completes, process stays alive.

结果

Express 4 async errors become unhandled rejections when Sentry is initialized, causing hanging requests. Express 5 handles them correctly. Workaround: wrap async handlers with .catch(next) or upgrade.

工作流

  • Define sync and async routes that throw.
  • Initialize Sentry if SENTRY env var set.
  • Setup Sentry Express error handler.
  • Add custom error middleware to log and respond 500.
  • Run server on port 3000.
  • Send requests to /sync and /async.
  • Observe Sentry reports and request behavior.
  • Identify that Express 4 does not handle rejected promises automatically.
  • Apply workaround: wrap async handler with .catch(next) or upgrade to Express 5.
  • Verify request completes with 500 response.

证据

Express 4 does not look at the promise your async handler returns. A throw after an await becomes an unhandled rejection, not a next(err) call.

来源

Without an error SDK, Node 20 kills the process. With @sentry/node initialised, the process survives, the error is reported, and the request hangs.

来源

Fix on Express 4: wrap the handler and .catch(next). Or move to Express 5, where rejected promises go to your error handler on their own.

来源

  • 记录日期: 2026-10-16T21:41:07.561Z