-
Notifications
You must be signed in to change notification settings - Fork 15
Prevent crash when req.logger is undefined in error handler #2765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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
asynckeyword and changed return type fromPromise<void>tovoid(method contains no async operations)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
bcb37
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes sense to me. I wonder though: Are we going to be ignoring errors that we could or should be logging?
|
@zackcl @bcb37 hey I found the actual reproducible issue here, and it's not really an edge-case so it's good that we waited on merging this. the fix ends up being similar, but we want to make sure the error gets logged because there's a legit situation where this would happen. if you send in a request that triggers a CORS error (domain not on whitelist), it will trigger this "req.logger is undefined" error. The reason is that CORS check happens before the logger middleware. I can see why this might have happened while working on the Demo server if something was not quite configured right at the time. if you look at app/index.ts in backend, you can see that CORS is set up in a standard Express config ( So architecturally, there's not really a good way to try and reorder anything middleware-wise, but that would be overkill to solve this, it should be fine to just create a fallback logger in these cases, as we're done with the request at this point anyway. Suggested change: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (req.logger) {
req.logger.error(experimentError);
} else {
const fallbackLogger = new UpgradeLogger();
fallbackLogger.error({
...experimentError,
warning: 'req.logger was undefined so custom logger used - error occurred before LogMiddleware loaded, likely a CORS issue',
});
}
Problem
The UpGrade instance used by the demo app was crashing with the following error:
This occurred when the
ErrorHandlerMiddlewaretried to callreq.logger.error()without checking ifreq.loggerexists. In certain scenarios (e.g., errors occurring before theLogMiddlewareruns or during early request processing),req.loggermay be undefined.Solution
req.loggerexists before callingreq.logger.error(experimentError)asynckeyword and changed return type fromPromise<void>tovoidto properly implement theExpressErrorMiddlewareInterfacecontract (noawaitcalls exist in the method)Changes
if (req.logger) { req.logger.error(experimentError); }async error(...): Promise<void>toerror(...): void