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

Sentry caught my Express error. The request still hung forever

Описание и применение

В Express 4 асинхронные маршруты, выбрасывающие ошибки после await, становятся необработанными отклонениями, если инициализирован SDK Sentry, что приводит к зависанию запроса. Решение: обернуть обработчик в .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