Skip to content

Commit

Permalink
add a function isError / delete instanceOf
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanGaignault committed Nov 19, 2024
1 parent 0a018d6 commit 2f313ff
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
6 changes: 3 additions & 3 deletions packages/core/src/domain/console/consoleObservable.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
12 changes: 7 additions & 5 deletions packages/core/src/domain/error/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/domain/error/trackRuntimeError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -35,7 +35,7 @@ export function trackRuntimeError(errorObservable: Observable<RawError>) {
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)
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion packages/rum-core/src/domain/error/errorCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Observable,
trackRuntimeError,
NonErrorPrefix,
isError,
} from '@datadog/browser-core'
import type { RumConfiguration } from '../configuration'
import type { RawRumErrorEvent } from '../../rawRumEvent.types'
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 2f313ff

Please sign in to comment.