-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Use SyncPromise internally & Remove deprecated API & Trim Public API #1858
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
Changes from 14 commits
200e046
5a82777
8cdee67
3ada698
afcdb4c
68ff201
cfcf366
53b4744
70dafe3
cb00da2
2b84aed
df0c3d7
8ea2ec7
0b95f34
5ca41a8
7875c0b
14ffee1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { BaseBackend, Options, SentryError } from '@sentry/core'; | ||
import { BaseBackend, Options } from '@sentry/core'; | ||
import { SentryEvent, SentryEventHint, Severity, Transport } from '@sentry/types'; | ||
import { SentryError } from '@sentry/utils/error'; | ||
import { isDOMError, isDOMException, isError, isErrorEvent, isPlainObject } from '@sentry/utils/is'; | ||
import { supportsBeacon, supportsFetch } from '@sentry/utils/supports'; | ||
import { SyncPromise } from '@sentry/utils/syncpromise'; | ||
import { addExceptionTypeValue, eventFromPlainObject, eventFromStacktrace, prepareFramesForEvent } from './parsers'; | ||
import { computeStackTrace } from './tracekit'; | ||
import { BeaconTransport, FetchTransport, XHRTransport } from './transports'; | ||
|
@@ -26,7 +28,10 @@ export interface BrowserOptions extends Options { | |
whitelistUrls?: Array<string | RegExp>; | ||
} | ||
|
||
/** The Sentry Browser SDK Backend. */ | ||
/** | ||
* The Sentry Browser SDK Backend. | ||
* @hidden | ||
*/ | ||
export class BrowserBackend extends BaseBackend<BrowserOptions> { | ||
/** | ||
* @inheritDoc | ||
|
@@ -69,48 +74,60 @@ export class BrowserBackend extends BaseBackend<BrowserOptions> { | |
/** | ||
* @inheritDoc | ||
*/ | ||
public async eventFromException(exception: any, hint?: SentryEventHint): Promise<SentryEvent> { | ||
let event; | ||
|
||
if (isErrorEvent(exception as ErrorEvent) && (exception as ErrorEvent).error) { | ||
// If it is an ErrorEvent with `error` property, extract it to get actual Error | ||
const ex = exception as ErrorEvent; | ||
exception = ex.error; // tslint:disable-line:no-parameter-reassignment | ||
event = eventFromStacktrace(computeStackTrace(exception as Error)); | ||
} else if (isDOMError(exception as DOMError) || isDOMException(exception as DOMException)) { | ||
// If it is a DOMError or DOMException (which are legacy APIs, but still supported in some browsers) | ||
// then we just extract the name and message, as they don't provide anything else | ||
// https://developer.mozilla.org/en-US/docs/Web/API/DOMError | ||
// https://developer.mozilla.org/en-US/docs/Web/API/DOMException | ||
const ex = exception as DOMException; | ||
const name = ex.name || (isDOMError(ex) ? 'DOMError' : 'DOMException'); | ||
const message = ex.message ? `${name}: ${ex.message}` : name; | ||
|
||
event = await this.eventFromMessage(message, undefined, hint); | ||
addExceptionTypeValue(event, message); | ||
} else if (isError(exception as Error)) { | ||
// we have a real Error object, do nothing | ||
event = eventFromStacktrace(computeStackTrace(exception as Error)); | ||
} else if (isPlainObject(exception as {}) && hint && hint.syntheticException) { | ||
// If it is plain Object, serialize it manually and extract options | ||
// This will allow us to group events based on top-level keys | ||
// which is much better than creating new group when any key/value change | ||
const ex = exception as {}; | ||
event = eventFromPlainObject(ex, hint.syntheticException); | ||
addExceptionTypeValue(event, 'Custom Object'); | ||
} else { | ||
// If none of previous checks were valid, then it means that | ||
// it's not a DOMError/DOMException | ||
// it's not a plain Object | ||
// it's not a valid ErrorEvent (one with an error property) | ||
// it's not an Error | ||
// So bail out and capture it as a simple message: | ||
const ex = exception as string; | ||
event = await this.eventFromMessage(ex, undefined, hint); | ||
addExceptionTypeValue(event, `${ex}`); | ||
} | ||
public eventFromException(exception: any, hint?: SentryEventHint): SyncPromise<SentryEvent> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not fully convinced about this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is correct, problem is that in node this needs to be "async" meaning that we can do file io in node. The function is defined in the interface of Backend, so even though this fully sync in browser, it isn't in node. |
||
return new SyncPromise<SentryEvent>(resolve => { | ||
let event: SentryEvent; | ||
|
||
if (isErrorEvent(exception as ErrorEvent) && (exception as ErrorEvent).error) { | ||
// If it is an ErrorEvent with `error` property, extract it to get actual Error | ||
const ex = exception as ErrorEvent; | ||
exception = ex.error; // tslint:disable-line:no-parameter-reassignment | ||
event = eventFromStacktrace(computeStackTrace(exception as Error)); | ||
resolve(this.buildEvent(event)); | ||
} else if (isDOMError(exception as DOMError) || isDOMException(exception as DOMException)) { | ||
// If it is a DOMError or DOMException (which are legacy APIs, but still supported in some browsers) | ||
// then we just extract the name and message, as they don't provide anything else | ||
// https://developer.mozilla.org/en-US/docs/Web/API/DOMError | ||
// https://developer.mozilla.org/en-US/docs/Web/API/DOMException | ||
const ex = exception as DOMException; | ||
const name = ex.name || (isDOMError(ex) ? 'DOMError' : 'DOMException'); | ||
const message = ex.message ? `${name}: ${ex.message}` : name; | ||
|
||
this.eventFromMessage(message, undefined, hint).then(messageEvent => { | ||
addExceptionTypeValue(messageEvent, message); | ||
resolve(this.buildEvent(messageEvent)); | ||
}); | ||
} else if (isError(exception as Error)) { | ||
// we have a real Error object, do nothing | ||
event = eventFromStacktrace(computeStackTrace(exception as Error)); | ||
resolve(this.buildEvent(event)); | ||
} else if (isPlainObject(exception as {}) && hint && hint.syntheticException) { | ||
// If it is plain Object, serialize it manually and extract options | ||
// This will allow us to group events based on top-level keys | ||
// which is much better than creating new group when any key/value change | ||
const ex = exception as {}; | ||
event = eventFromPlainObject(ex, hint.syntheticException); | ||
addExceptionTypeValue(event, 'Custom Object'); | ||
resolve(this.buildEvent(event)); | ||
} else { | ||
// If none of previous checks were valid, then it means that | ||
// it's not a DOMError/DOMException | ||
// it's not a plain Object | ||
// it's not a valid ErrorEvent (one with an error property) | ||
// it's not an Error | ||
// So bail out and capture it as a simple message: | ||
const ex = exception as string; | ||
this.eventFromMessage(ex, undefined, hint).then(messageEvent => { | ||
addExceptionTypeValue(messageEvent, `${ex}`); | ||
resolve(this.buildEvent(messageEvent)); | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
event = { | ||
/** JSDOC */ | ||
private buildEvent(event: SentryEvent, hint?: SentryEventHint): SentryEvent { | ||
return { | ||
...event, | ||
event_id: hint && hint.event_id, | ||
exception: { | ||
|
@@ -121,18 +138,16 @@ export class BrowserBackend extends BaseBackend<BrowserOptions> { | |
}, | ||
}, | ||
}; | ||
|
||
return event; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public async eventFromMessage( | ||
public eventFromMessage( | ||
message: string, | ||
level: Severity = Severity.Info, | ||
hint?: SentryEventHint, | ||
): Promise<SentryEvent> { | ||
): SyncPromise<SentryEvent> { | ||
const event: SentryEvent = { | ||
event_id: hint && hint.event_id, | ||
level, | ||
|
@@ -147,6 +162,6 @@ export class BrowserBackend extends BaseBackend<BrowserOptions> { | |
}; | ||
} | ||
|
||
return event; | ||
return SyncPromise.resolve(event); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.