Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/ecs-helpers/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @elastic/ecs-helpers Changelog

## v2.2.0

- `formatError()` now includes the [`error.id`](https://www.elastic.co/docs/reference/ecs/ecs-error#field-error-id)
and [`error.code`](https://www.elastic.co/docs/reference/ecs/ecs-error#field-error-code)
fields on the emitted `error` object when the source `Error` instance has
matching `id` or `code` properties. Previously these were silently dropped,
even though they are part of the ECS [error field set](https://www.elastic.co/docs/reference/ecs/ecs-error).

## v2.1.1

- fix: Use `req.originalUrl`, if available, when converting `req` to ECS fields.
Expand Down
6 changes: 6 additions & 0 deletions packages/ecs-helpers/lib/error-formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ function formatError (ecsFields, err) {
message: err.message,
stack_trace: err.stack
}
if (err.code !== undefined) {
ecsFields.error.code = err.code
}
if (err.id !== undefined) {
ecsFields.error.id = err.id
}

return true
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ecs-helpers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@elastic/ecs-helpers",
"version": "2.1.1",
"version": "2.2.0",
"description": "ecs-logging-nodejs helpers",
"publishConfig": {
"access": "public",
Expand Down
19 changes: 19 additions & 0 deletions packages/ecs-helpers/test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,25 @@ test('formatError: MyError with removed constructor', t => {
t.end()
})

test('formatError: Error with code and id', t => {
const rec = {}
const err = new Error('boom')
err.code = 'ESOMETHING'
err.id = 'req-123'
formatError(rec, err)
t.equal(rec.error.code, 'ESOMETHING')
t.equal(rec.error.id, 'req-123')
t.end()
})

test('formatError: Error without code or id', t => {
const rec = {}
formatError(rec, new Error('boom'))
t.notOk('code' in rec.error, 'error.code should be absent')
t.notOk('id' in rec.error, 'error.id should be absent')
t.end()
})

test('formatError: non-Error', t => {
const rec = {}
const nonError = { foo: 'bar' }
Expand Down
Loading