Skip to content

Commit

Permalink
feat(rum-core): stringify rejected obj
Browse files Browse the repository at this point in the history
  • Loading branch information
devcorpio committed Sep 18, 2023
1 parent 77067e7 commit 0ef7b80
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
40 changes: 29 additions & 11 deletions packages/rum-core/src/error-logging/error-logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import stackParser from 'error-stack-parser'
* List of keys to be ignored from getting added to custom error properties
*/
const IGNORE_KEYS = ['stack', 'message']
const PROMISE_REJECTION_PREFIX = 'Unhandled promise rejection: '

function getErrorProperties(error) {
/**
Expand Down Expand Up @@ -172,7 +173,6 @@ class ErrorLogging {
}

logPromiseEvent(promiseRejectionEvent) {
const prefix = 'Unhandled promise rejection: '
let { reason } = promiseRejectionEvent
if (reason == null) {
reason = '<no reason specified>'
Expand All @@ -186,18 +186,10 @@ class ErrorLogging {
const name = reason.name ? reason.name + ': ' : ''
errorEvent = {
error: reason,
message: prefix + name + reason.message
message: PROMISE_REJECTION_PREFIX + name + reason.message
}
} else {
reason =
typeof reason === 'object'
? '<object>'
: typeof reason === 'function'
? '<function>'
: reason
errorEvent = {
message: prefix + reason
}
errorEvent = this._parseRejectReason(reason)
}
this.logErrorEvent(errorEvent)
}
Expand All @@ -211,6 +203,32 @@ class ErrorLogging {
}
return this.logErrorEvent(errorEvent)
}

_parseRejectReason(reason) {
const errorEvent = {
message: PROMISE_REJECTION_PREFIX
}

if (Array.isArray(reason)) {
errorEvent.message += '<object>'
} else if (typeof reason === 'object') {
try {
errorEvent.message += JSON.stringify(reason)
errorEvent.error = reason
} catch (error) {
// fallback. JSON.stringify can throw exceptions in different circumstances.
// please, see this link for more info:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#exceptions
errorEvent.message += '<object>'
}
} else if (typeof reason === 'function') {
errorEvent.message += '<function>'
} else {
errorEvent.message += reason
}

return errorEvent
}
}

export default ErrorLogging
26 changes: 23 additions & 3 deletions packages/rum-core/test/error-logging/error-logging.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,24 +393,44 @@ describe('ErrorLogging', function () {
)

errorLogging.logPromiseEvent({
reason: [{ a: '1' }]
reason: ['array-value']
})
expect(getEvents()[7][ERRORS].exception.message).toBe(
'Unhandled promise rejection: <object>'
)

errorLogging.logPromiseEvent({
reason: { a: '1' }
})
expect(getEvents()[8][ERRORS].exception.message).toBe(
'Unhandled promise rejection: {"a":"1"}'
)

// Make sure that the object fallback case works
// Circular objects causes JSON.stringify to fail
const circularObj = {
foo: 'bar'
}
circularObj.self = circularObj
errorLogging.logPromiseEvent({
reason: circularObj
})
expect(getEvents()[9][ERRORS].exception.message).toBe(
'Unhandled promise rejection: <object>'
)

const noop = function () {}
errorLogging.logPromiseEvent({
reason: noop
})
expect(getEvents()[8][ERRORS].exception.message).toBe(
expect(getEvents()[10][ERRORS].exception.message).toBe(
'Unhandled promise rejection: <function>'
)

errorLogging.logPromiseEvent({
reason: null
})
expect(getEvents()[9][ERRORS].exception.message).toBe(
expect(getEvents()[11][ERRORS].exception.message).toBe(
'Unhandled promise rejection: <no reason specified>'
)

Expand Down
1 change: 1 addition & 0 deletions packages/rum/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function getApmBase() {
}

const apmBase = getApmBase()

const init = apmBase.init.bind(apmBase)

export default init
Expand Down

0 comments on commit 0ef7b80

Please sign in to comment.