Make sure logger serializes errors (#2001)
parent
6758755316
commit
1dc017367e
|
@ -222,6 +222,26 @@ describe('general functionality', () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('sentryTransport serializes errors', () => {
|
||||||
|
const message = 'message'
|
||||||
|
const timestamp = Date.now()
|
||||||
|
const sentryTimestamp = timestamp / 1000
|
||||||
|
|
||||||
|
sentryTransport(
|
||||||
|
LogLevel.Debug,
|
||||||
|
message,
|
||||||
|
{error: new Error('foo')},
|
||||||
|
timestamp,
|
||||||
|
)
|
||||||
|
expect(Sentry.addBreadcrumb).toHaveBeenCalledWith({
|
||||||
|
message,
|
||||||
|
data: {error: 'Error: foo'},
|
||||||
|
type: 'default',
|
||||||
|
level: LogLevel.Debug,
|
||||||
|
timestamp: sentryTimestamp,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
test('add/remove transport', () => {
|
test('add/remove transport', () => {
|
||||||
const timestamp = Date.now()
|
const timestamp = Date.now()
|
||||||
const logger = new Logger({enabled: true})
|
const logger = new Logger({enabled: true})
|
||||||
|
|
|
@ -90,6 +90,16 @@ const enabledLogLevels: {
|
||||||
[LogLevel.Error]: [LogLevel.Error],
|
[LogLevel.Error]: [LogLevel.Error],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function prepareMetadata(metadata: Metadata): Metadata {
|
||||||
|
return Object.keys(metadata).reduce((acc, key) => {
|
||||||
|
let value = metadata[key]
|
||||||
|
if (value instanceof Error) {
|
||||||
|
value = value.toString()
|
||||||
|
}
|
||||||
|
return {...acc, [key]: value}
|
||||||
|
}, {})
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used in dev mode to nicely log to the console
|
* Used in dev mode to nicely log to the console
|
||||||
*/
|
*/
|
||||||
|
@ -100,7 +110,8 @@ export const consoleTransport: Transport = (
|
||||||
timestamp,
|
timestamp,
|
||||||
) => {
|
) => {
|
||||||
const extra = Object.keys(metadata).length
|
const extra = Object.keys(metadata).length
|
||||||
? ' ' + JSON.stringify(metadata, null, ' ')
|
? // don't prepareMetadata here, in dev we want the stack trace
|
||||||
|
' ' + JSON.stringify(metadata, null, ' ')
|
||||||
: ''
|
: ''
|
||||||
const log = {
|
const log = {
|
||||||
[LogLevel.Debug]: console.debug,
|
[LogLevel.Debug]: console.debug,
|
||||||
|
@ -119,6 +130,8 @@ export const sentryTransport: Transport = (
|
||||||
{type, tags, ...metadata},
|
{type, tags, ...metadata},
|
||||||
timestamp,
|
timestamp,
|
||||||
) => {
|
) => {
|
||||||
|
const meta = prepareMetadata(metadata)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If a string, report a breadcrumb
|
* If a string, report a breadcrumb
|
||||||
*/
|
*/
|
||||||
|
@ -135,7 +148,7 @@ export const sentryTransport: Transport = (
|
||||||
|
|
||||||
Sentry.addBreadcrumb({
|
Sentry.addBreadcrumb({
|
||||||
message,
|
message,
|
||||||
data: metadata,
|
data: meta,
|
||||||
type: type || 'default',
|
type: type || 'default',
|
||||||
level: severity,
|
level: severity,
|
||||||
timestamp: timestamp / 1000, // Sentry expects seconds
|
timestamp: timestamp / 1000, // Sentry expects seconds
|
||||||
|
@ -155,7 +168,7 @@ export const sentryTransport: Transport = (
|
||||||
Sentry.captureMessage(message, {
|
Sentry.captureMessage(message, {
|
||||||
level: messageLevel,
|
level: messageLevel,
|
||||||
tags,
|
tags,
|
||||||
extra: metadata,
|
extra: meta,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -164,7 +177,7 @@ export const sentryTransport: Transport = (
|
||||||
*/
|
*/
|
||||||
Sentry.captureException(message, {
|
Sentry.captureException(message, {
|
||||||
tags,
|
tags,
|
||||||
extra: metadata,
|
extra: meta,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue