Skip to content

Commit

Permalink
🐛 AddError should support all instances of type Error (#3228)
Browse files Browse the repository at this point in the history
* AddError should support all instances of type Error

* update unit test

---------

Co-authored-by: roman.gaignault <[email protected]>
  • Loading branch information
lightswitch05 and RomanGaignault authored Dec 18, 2024
1 parent db5520f commit 166489d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/src/domain/error/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export function getFileFromStackTraceString(stack: string) {
}

export function isError(error: unknown): error is Error {
return Object.prototype.toString.call(error) === '[object Error]'
return error instanceof Error || Object.prototype.toString.call(error) === '[object Error]'
}

export function flattenErrorCauses(error: ErrorWithCause, parentSource: ErrorSource): RawErrorCause[] | undefined {
Expand Down
18 changes: 18 additions & 0 deletions packages/rum-core/src/domain/error/errorCollection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ describe('error collection', () => {
rawRumEvents = collectAndValidateRawRumEvents(lifeCycle)
}

// when calling toString on SubErrorViaPrototype, the results will be '[object Object]'
// but the value of 'error instanceof Error' will still be true.
function SubErrorViaPrototype(this: Error, _message: string) {
Error.call(this, _message)
this.name = 'Error'
this.message = _message
this.stack = `Error: ${_message}\n at <anonymous>`
}
SubErrorViaPrototype.prototype = Object.create(Error.prototype)
SubErrorViaPrototype.prototype.constructor = SubErrorViaPrototype

describe('addError', () => {
;[
{
Expand All @@ -38,6 +49,13 @@ describe('error collection', () => {
type: 'Error',
stack: jasmine.stringMatching('Error: foo'),
},
{
testCase: 'an error subclass via prototype',
error: new (SubErrorViaPrototype as unknown as { new (message: string): Error })('bar'),
message: 'bar',
type: 'Error',
stack: jasmine.stringMatching('Error: bar'),
},
{
testCase: 'a string',
error: 'foo',
Expand Down

0 comments on commit 166489d

Please sign in to comment.