diff --git a/packages/core/src/domain/console/consoleObservable.ts b/packages/core/src/domain/console/consoleObservable.ts index 2738c80c53..b968a5589f 100644 --- a/packages/core/src/domain/console/consoleObservable.ts +++ b/packages/core/src/domain/console/consoleObservable.ts @@ -1,4 +1,4 @@ -import { flattenErrorCauses, tryToGetFingerprint } from '../error/error' +import { flattenErrorCauses, isError, tryToGetFingerprint } from '../error/error' import { mergeObservables, Observable } from '../../tools/observable' import { ConsoleApiName, globalConsole } from '../../tools/display' import { callMonitored } from '../../tools/monitor' @@ -74,7 +74,7 @@ function buildConsoleLog(params: unknown[], api: ConsoleApiName, handlingStack: let error: RawError | undefined if (api === ConsoleApiName.error) { - const firstErrorParam = find(params, (param: unknown): param is Error => param instanceof Error) + const firstErrorParam = find(params, (param: unknown): param is Error => isError(param)) error = { stack: firstErrorParam ? toStackTraceString(computeStackTrace(firstErrorParam)) : undefined, @@ -100,7 +100,7 @@ function formatConsoleParameters(param: unknown) { if (typeof param === 'string') { return sanitize(param) } - if (param instanceof Error) { + if (isError(param)) { return formatErrorMessage(computeStackTrace(param)) } return jsonStringify(sanitize(param), undefined, 2) diff --git a/packages/core/src/domain/error/error.ts b/packages/core/src/domain/error/error.ts index 49decc01ab..72791bc8c9 100644 --- a/packages/core/src/domain/error/error.ts +++ b/packages/core/src/domain/error/error.ts @@ -28,7 +28,7 @@ export function computeRawError({ source, handling, }: RawErrorParams): RawError { - const isErrorInstance = originalError instanceof Error + const isErrorInstance = isError(originalError) const message = computeMessage(stackTrace, isErrorInstance, nonErrorPrefix, originalError) const stack = hasUsableStack(isErrorInstance, stackTrace) @@ -80,19 +80,21 @@ function hasUsableStack(isErrorInstance: boolean, stackTrace?: StackTrace): stac } export function tryToGetFingerprint(originalError: unknown) { - return originalError instanceof Error && 'dd_fingerprint' in originalError - ? String(originalError.dd_fingerprint) - : undefined + return isError(originalError) && 'dd_fingerprint' in originalError ? String(originalError.dd_fingerprint) : undefined } export function getFileFromStackTraceString(stack: string) { return /@ (.+)/.exec(stack)?.[1] } +export function isError(error: unknown): error is Error { + return Object.prototype.toString.call(error) === '[object Error]' +} + export function flattenErrorCauses(error: ErrorWithCause, parentSource: ErrorSource): RawErrorCause[] | undefined { let currentError = error const causes: RawErrorCause[] = [] - while (currentError?.cause instanceof Error && causes.length < 10) { + while (isError(currentError?.cause) && causes.length < 10) { const stackTrace = computeStackTrace(currentError.cause) causes.push({ message: currentError.cause.message, diff --git a/packages/core/src/domain/error/trackRuntimeError.ts b/packages/core/src/domain/error/trackRuntimeError.ts index 63735a4628..74af49dab0 100644 --- a/packages/core/src/domain/error/trackRuntimeError.ts +++ b/packages/core/src/domain/error/trackRuntimeError.ts @@ -3,7 +3,7 @@ import type { Observable } from '../../tools/observable' import { clocksNow } from '../../tools/utils/timeUtils' import type { StackTrace } from '../../tools/stackTrace/computeStackTrace' import { computeStackTrace, computeStackTraceFromOnErrorMessage } from '../../tools/stackTrace/computeStackTrace' -import { computeRawError } from './error' +import { computeRawError, isError } from './error' import type { RawError } from './error.types' import { ErrorHandling, ErrorSource, NonErrorPrefix } from './error.types' @@ -35,7 +35,7 @@ export function trackRuntimeError(errorObservable: Observable) { export function instrumentOnError(callback: UnhandledErrorCallback) { return instrumentMethod(window, 'onerror', ({ parameters: [messageObj, url, line, column, errorObj] }) => { let stackTrace - if (errorObj instanceof Error) { + if (isError(errorObj)) { stackTrace = computeStackTrace(errorObj) } else { stackTrace = computeStackTraceFromOnErrorMessage(messageObj, url, line, column) diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index d2f9652ff1..d66ae435bf 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -85,7 +85,12 @@ export { sendToExtension } from './tools/sendToExtension' export { runOnReadyState } from './browser/runOnReadyState' export { getZoneJsOriginalValue } from './tools/getZoneJsOriginalValue' export { instrumentMethod, instrumentSetter, InstrumentedMethodCall } from './tools/instrumentMethod' -export { computeRawError, getFileFromStackTraceString, NO_ERROR_STACK_PRESENT_MESSAGE } from './domain/error/error' +export { + computeRawError, + getFileFromStackTraceString, + isError, + NO_ERROR_STACK_PRESENT_MESSAGE, +} from './domain/error/error' export { NonErrorPrefix } from './domain/error/error.types' export { Context, ContextArray, ContextValue } from './tools/serialisation/context' export { diff --git a/packages/rum-core/src/domain/error/errorCollection.ts b/packages/rum-core/src/domain/error/errorCollection.ts index de51099a61..d37c0e2416 100644 --- a/packages/rum-core/src/domain/error/errorCollection.ts +++ b/packages/rum-core/src/domain/error/errorCollection.ts @@ -10,6 +10,7 @@ import { Observable, trackRuntimeError, NonErrorPrefix, + isError, } from '@datadog/browser-core' import type { RumConfiguration } from '../configuration' import type { RawRumErrorEvent } from '../../rawRumEvent.types' @@ -71,7 +72,7 @@ export function doStartErrorCollection( { error, handlingStack, startClocks, context: customerContext }: ProvidedError, savedCommonContext?: CommonContext ) => { - const stackTrace = error instanceof Error ? computeStackTrace(error) : undefined + const stackTrace = isError(error) ? computeStackTrace(error) : undefined const rawError = computeRawError({ stackTrace, originalError: error,