Skip to content

Conversation

@zackcl
Copy link
Collaborator

@zackcl zackcl commented Dec 1, 2025

Problem

The UpGrade instance used by the demo app was crashing with the following error:

uncaughtException: Cannot read properties of undefined (reading 'error')
at ErrorHandlerMiddleware.ts:126:16

This occurred when the ErrorHandlerMiddleware tried to call req.logger.error() without checking if req.logger exists. In certain scenarios (e.g., errors occurring before the LogMiddleware runs or during early request processing), req.logger may be undefined.

Solution

  1. Added null check before accessing req.logger: Prevents the crash by checking if req.logger exists before calling req.logger.error(experimentError)
  2. Fixed TypeScript warning: Removed async keyword and changed return type from Promise<void> to void to properly implement the ExpressErrorMiddlewareInterface contract (no await calls exist in the method)

Changes

  • Added defensive null check: if (req.logger) { req.logger.error(experimentError); }
  • Changed method signature from async error(...): Promise<void> to error(...): void

@zackcl zackcl self-assigned this Dec 1, 2025
Copilot finished reviewing on behalf of zackcl December 1, 2025 19:28
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a crash in the ErrorHandlerMiddleware when req.logger is undefined. The crash occurred when errors happened before the LogMiddleware set up the logger, causing a "Cannot read properties of undefined" error at runtime.

  • Added defensive null check before accessing req.logger.error()
  • Removed unnecessary async keyword and changed return type from Promise<void> to void (method contains no async operations)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants