Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ [RUMF-863] Enable console error with stack traces #781

Merged
merged 5 commits into from
Apr 6, 2021
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
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ module.exports = {
'no-trailing-spaces': 'off',
'no-undef-init': 'error',
'no-underscore-dangle': 'error',
'no-unreachable': 'error',
'no-unsafe-finally': 'error',
'no-unused-labels': 'error',
'no-unused-vars': 'off',
Expand Down
59 changes: 2 additions & 57 deletions packages/core/src/domain/automaticErrorCollection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from './automaticErrorCollection'
import { Configuration } from './configuration'

describe('console tracker (console-stack disabled)', () => {
describe('console tracker', () => {
let consoleErrorStub: jasmine.Spy
let notifyError: jasmine.Spy
const CONSOLE_CONTEXT = {
Expand All @@ -24,63 +24,8 @@ describe('console tracker (console-stack disabled)', () => {
consoleErrorStub = spyOn(console, 'error')
notifyError = jasmine.createSpy('notifyError')
const errorObservable = new Observable<RawError>()
const configuration: Partial<Configuration> = { isEnabled: () => false }
errorObservable.subscribe(notifyError)
startConsoleTracking(configuration as Configuration, errorObservable)
})

afterEach(() => {
stopConsoleTracking()
})

it('should keep original behavior', () => {
console.error('foo', 'bar')
expect(consoleErrorStub).toHaveBeenCalledWith('foo', 'bar')
})

it('should notify error', () => {
console.error('foo', 'bar')
expect(notifyError).toHaveBeenCalledWith({
...CONSOLE_CONTEXT,
message: 'console error: foo bar',
startTime: jasmine.any(Number),
})
})

it('should stringify object parameters', () => {
console.error('Hello', { foo: 'bar' })
expect(notifyError).toHaveBeenCalledWith({
...CONSOLE_CONTEXT,
message: 'console error: Hello {\n "foo": "bar"\n}',
startTime: jasmine.any(Number),
})
})

it('should format error instance', () => {
console.error(new TypeError('hello'))
const message = (notifyError.calls.mostRecent().args[0] as RawError).message
if (!isIE()) {
expect(message).toMatch(/^console error: TypeError: hello\s+at/)
} else {
expect(message).toContain('console error: TypeError: hello')
}
})
})

describe('console tracker (console-stack enabled)', () => {
let consoleErrorStub: jasmine.Spy
let notifyError: jasmine.Spy
const CONSOLE_CONTEXT = {
source: ErrorSource.CONSOLE,
}

beforeEach(() => {
consoleErrorStub = spyOn(console, 'error')
notifyError = jasmine.createSpy('notifyError')
const errorObservable = new Observable<RawError>()
const configuration: Partial<Configuration> = { isEnabled: () => true }
errorObservable.subscribe(notifyError)
startConsoleTracking(configuration as Configuration, errorObservable)
startConsoleTracking(errorObservable)
})

afterEach(() => {
Expand Down
25 changes: 9 additions & 16 deletions packages/core/src/domain/automaticErrorCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function startAutomaticErrorCollection(configuration: Configuration) {
if (!filteredErrorsObservable) {
const errorObservable = new Observable<RawError>()
trackNetworkError(configuration, errorObservable)
startConsoleTracking(configuration, errorObservable)
startConsoleTracking(errorObservable)
startRuntimeErrorTracking(errorObservable)
filteredErrorsObservable = filterErrors(configuration, errorObservable)
}
Expand Down Expand Up @@ -44,43 +44,36 @@ export function filterErrors(configuration: Configuration, errorObservable: Obse

let originalConsoleError: (...params: unknown[]) => void

export function startConsoleTracking(configuration: Configuration, errorObservable: ErrorObservable) {
export function startConsoleTracking(errorObservable: ErrorObservable) {
originalConsoleError = console.error
console.error = monitor((...params: unknown[]) => {
originalConsoleError.apply(console, params)
errorObservable.notify({
...buildErrorFromParams(configuration, params),
...buildErrorFromParams(params),
source: ErrorSource.CONSOLE,
startTime: relativeNow(),
})
})
}

function buildErrorFromParams(configuration: Configuration, params: unknown[]) {
if (configuration.isEnabled('console-stack')) {
const firstErrorParam = find(params, (param: unknown): param is Error => param instanceof Error)
return {
message: ['console error:', ...params]
.map((param) => formatConsoleParameters(formatErrorMessage, param))
.join(' '),
stack: firstErrorParam ? toStackTraceString(computeStackTrace(firstErrorParam)) : undefined,
}
}
function buildErrorFromParams(params: unknown[]) {
const firstErrorParam = find(params, (param: unknown): param is Error => param instanceof Error)
return {
message: ['console error:', ...params].map((param) => formatConsoleParameters(toStackTraceString, param)).join(' '),
message: ['console error:', ...params].map((param) => formatConsoleParameters(param)).join(' '),
stack: firstErrorParam ? toStackTraceString(computeStackTrace(firstErrorParam)) : undefined,
}
}

export function stopConsoleTracking() {
console.error = originalConsoleError
}

function formatConsoleParameters(stackTraceFormatter: (stack: StackTrace) => string, param: unknown) {
function formatConsoleParameters(param: unknown) {
if (typeof param === 'string') {
return param
}
if (param instanceof Error) {
return stackTraceFormatter(computeStackTrace(param))
return formatErrorMessage(computeStackTrace(param))
}
return jsonStringify(param, undefined, 2)
}
Expand Down