-
-
Notifications
You must be signed in to change notification settings - Fork 3
Protects against logger failure #555
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: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,13 +12,13 @@ | |
| /** | ||
| * Make an express callback for the controller | ||
| * @param {object} params | ||
| * @param {Function} params.controller | ||
| * @param {object} params.specification | ||
| * @param {boolean=} params.errorDetails | ||
| * @param {Logger=} params.logger | ||
| * @param {object=} params.meta | ||
| * @param {boolean=} params.mock | ||
| * @returns {Function} | ||
| */ | ||
| export const makeExpressCallback | ||
| = ({ controller, specification, errorDetails, logger, meta, mock }) => | ||
|
|
@@ -28,9 +28,9 @@ | |
| * @param {Context} context | ||
| * @param {Request} request | ||
| * @param {Response} response | ||
| * @returns {Promise<any>} | ||
| */ | ||
| async (context, request, response) => { | ||
|
Check failure on line 33 in src/express-callback.js
|
||
| const startTime = hrtime() | ||
| try { | ||
| const allParameters = { | ||
|
|
@@ -67,27 +67,37 @@ | |
| const responseBody = await controller(feedback) | ||
| const responseTime = hrtime(startTime)[1] / 1000000 // convert to milliseconds | ||
|
|
||
| logger.debug({ | ||
| url, | ||
| parameters, | ||
| post: request.body, | ||
| response: responseBody, | ||
| method, | ||
| ip, | ||
| userAgent, | ||
| responseTime, | ||
| statusCode: response.statusCode || 200, | ||
| message: 'access' | ||
| }) | ||
| try { | ||
| logger?.debug({ | ||
| url, | ||
| parameters, | ||
| post: request.body, | ||
| response: responseBody, | ||
| method, | ||
| ip, | ||
| userAgent, | ||
| responseTime, | ||
| statusCode: response.statusCode || 200, | ||
| message: 'access' | ||
| }) | ||
| } catch (logError) { | ||
| // Prevent logging errors from affecting the request | ||
| console.error('Logger failed:', logError) | ||
| } | ||
|
Comment on lines
+70
to
+86
|
||
|
|
||
| return responseBody | ||
| } catch (error) { | ||
| const errorCodeStatus = getStatusByError(error) | ||
|
|
||
| if (errorCodeStatus >= 500) { | ||
| logger.error(error) | ||
| } else { | ||
| logger.warn(error) | ||
| try { | ||
| if (errorCodeStatus >= 500) { | ||
| logger?.error(error) | ||
| } else { | ||
| logger?.warn(error) | ||
| } | ||
| } catch (logError) { | ||
| // Prevent logging errors from affecting the request | ||
| console.error('Logger failed:', logError) | ||
|
Comment on lines
+98
to
+100
|
||
| } | ||
|
Comment on lines
+92
to
101
|
||
|
|
||
| response.status(errorCodeStatus) | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,13 +19,18 @@ export default (logger, validateResponse) => (context, request, response) => { | |||||||
| : null | ||||||||
| if (valid?.errors) { | ||||||||
| if (logger) { | ||||||||
| logger.error({ | ||||||||
| message: 'Response validation failed', | ||||||||
| errors: valid.errors, | ||||||||
| operation: context.operation, | ||||||||
| statusCode: response.statusCode, | ||||||||
| response: context.response | ||||||||
| }) | ||||||||
| try { | ||||||||
| logger.error({ | ||||||||
| message: 'Response validation failed', | ||||||||
| errors: valid.errors, | ||||||||
| operation: context.operation, | ||||||||
| statusCode: response.statusCode, | ||||||||
| response: context.response | ||||||||
| }) | ||||||||
| } catch (logError) { | ||||||||
| // Prevent logging errors from affecting the response | ||||||||
| console.error('Logger failed:', logError) | ||||||||
|
Comment on lines
+31
to
+32
|
||||||||
| // Prevent logging errors from affecting the response | |
| console.error('Logger failed:', logError) | |
| // Intentionally ignore logging errors to avoid interfering with the response |
Copilot
AI
Feb 19, 2026
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.
Inconsistent logger null-safety approach between files. In response-validation.js, the code uses if (logger) check followed by a try-catch, while in express-callback.js, it uses optional chaining (logger?.) with try-catch. For consistency and clarity, the same pattern should be used throughout the codebase. Consider standardizing on one approach across both files.
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.
Using
console.erroras a fallback when the logger fails creates a circular issue: if the logger failed due to a general logging infrastructure problem,console.errormight also fail or be unavailable in certain environments. Additionally, this creates inconsistent logging output (some logs go through the configured logger, others through console), making log aggregation and monitoring more difficult. Consider either: 1) silently swallowing the error if resilience is the primary goal, or 2) using a more robust fallback mechanism.