Skip to content
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ Notes:
[float]
===== Bug fixes

- Capturing an error would fail if the Error instance had an attribute that
was an invalid date. ({issues}2030[#2030])

- Fix the span for an instrumented S3 ListBuckets API call to not be invalid
for APM server intake. ({pull}2866[#2866])

Expand Down
8 changes: 6 additions & 2 deletions lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,13 @@ function attributesFromErr (err) {
continue
case 'object':
// Ignore all objects except Dates.
if (typeof val.toISOString !== 'function') {
if (typeof val.toISOString !== 'function' || typeof val.getTime !== 'function') {
continue
} else if (Number.isNaN(val.getTime())) {
val = 'Invalid Date' // calling toISOString() on invalid dates throws
} else {
val = val.toISOString()
}
val = val.toISOString()
}
attrs[key] = val
n++
Expand Down Expand Up @@ -280,5 +283,6 @@ module.exports = {
createAPMError,

// Exported for testing.
attributesFromErr,
_moduleNameFromFrames
}
42 changes: 41 additions & 1 deletion test/errors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const path = require('path')
const tape = require('tape')

const logging = require('../lib/logging')
const { createAPMError, generateErrorId, _moduleNameFromFrames } = require('../lib/errors')
const { createAPMError, generateErrorId, attributesFromErr, _moduleNameFromFrames } = require('../lib/errors')
const { dottedLookup } = require('./_utils')

const log = logging.createLogger('off')
Expand Down Expand Up @@ -331,3 +331,43 @@ tape.test('#_moduleNameFromFrames()', function (suite) {

suite.end()
})

tape.test('#attributesFromErr()', function (suite) {
var cases = [
// 'err' is an Error instance, or a function that returns one.
{
name: 'no attrs',
err: new Error('boom'),
expectedAttrs: undefined
},
{
name: 'string attr',
err: () => {
const err = new Error('boom')
err.aStr = 'hello'
return err
},
expectedAttrs: { aStr: 'hello' }
},
{
name: 'Invalid Date attr',
err: () => {
const err = new Error('boom')
err.aDate = new Date('invalid')
return err
},
expectedAttrs: { aDate: 'Invalid Date' }
}
]

cases.forEach(function (opts) {
suite.test(opts.name, function (t) {
const err = typeof (opts.err) === 'function' ? opts.err() : opts.err
const attrs = attributesFromErr(err)
t.deepEqual(attrs, opts.expectedAttrs, 'got expected attrs')
t.end()
})
})

suite.end()
})