Skip to content

Commit

Permalink
fix(winston): fix unhandledRejection handling for [email protected] chan…
Browse files Browse the repository at this point in the history
…ges; release @elastic/[email protected] (#181)

In [email protected], the handling of unhandledRejection events changed
slightly: setting '<record>.rejection=true` rather than `<record>.exception`.
  • Loading branch information
trentm authored Mar 18, 2024
1 parent 98f40a3 commit f884e24
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
7 changes: 7 additions & 0 deletions packages/ecs-winston-format/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @elastic/ecs-winston-format Changelog

## v1.5.3

- Fix format handling for the log record emitted by Winston for
`unhandledRejection` events (when configured to handle them) as of
[email protected]. In that version the log record changed slightly to
set `record.rejection` rather than `record.exception`.

## v1.5.2

- Fix the Winston transformers to *not* delete the `level` property from the
Expand Down
13 changes: 7 additions & 6 deletions packages/ecs-winston-format/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ class EcsFieldsTransform {
// in place of `info.stack`.
//
// 2. Winston logger configured to handle uncaughtException and/or unhandledRejection.
// If `info.exception: true` and level is "error" and `info.trace` is an
// Array and `info.message` starts with "uncaughtException:" or
// "unhandledRejection:", then convert to `error.*` fields. These
// conditions are to infer the `info` shape returned by Winston's
// `ExceptionHandler` and `RejectionHandler`.
// If `info.exception: true` or `info.rejection: true`, and level is
// "error", and `info.trace` is an Array and `info.message` starts with
// "uncaughtException:" or "unhandledRejection:", then convert to
// `error.*` fields. These conditions are to infer the `info` shape
// returned by Winston's `ExceptionHandler` and `RejectionHandler`.
// In this case the redundant `stack`, `trace`, `date` fields are dropped
// and error details are moved to the `error.*` fields.
//
Expand Down Expand Up @@ -99,7 +99,8 @@ class EcsFieldsTransform {
for (const propName in err) {
delete info[propName]
}
} else if (info.exception === true &&
} else if (
(info.exception === true || info.rejection === true) &&
info.level === 'error' &&
Array.isArray(info.trace) &&
(info.message.startsWith('uncaughtException:') ||
Expand Down
2 changes: 1 addition & 1 deletion packages/ecs-winston-format/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@elastic/ecs-winston-format",
"version": "1.5.2",
"version": "1.5.3",
"description": "A formatter for the winston logger compatible with Elastic Common Schema.",
"main": "index.js",
"types": "index.d.ts",
Expand Down
8 changes: 7 additions & 1 deletion packages/ecs-winston-format/test/errors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,13 @@ if (!TEST_SKIP_SLOW) {
t.equal(rec.error.message, 'funcb boom', 'error.message')
t.match(rec.error.stack_trace, /^Error: funcb boom\n {4}at/, 'error.stack_trace')
t.equal(rec.error.code, 42, 'error.code')
t.equal(rec.exception, true, 'exception')
if (semver.gte(winston.version, '3.12.0')) {
// https://github.com/winstonjs/winston/pull/2390 changed behaviour
// with unhandledRejection in [email protected].
t.equal(rec.rejection, true, 'rejection')
} else {
t.equal(rec.exception, true, 'exception')
}
t.end()
}
)
Expand Down

0 comments on commit f884e24

Please sign in to comment.