8000 feat: Add wrap (#2060) · FunnyLiu/sentry-javascript@cc08ef4 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cc08ef4

Browse files
authored
feat: Add wrap (getsentry#2060)
* feat: Add context * fix: Rename context to wrap * fix: Rename to wrap * fix: Docs * fix: Changelog * fix: Wrap function
1 parent b22e359 commit cc08ef4

File tree

9 files changed

+22
-8
lines changed

9 files changed

+22
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
## Unreleased
44

5-
- [browser] - feat: Added `onLoad` callback to `showReportDialog`
5+
- [browser] feat: Expose `wrap` function in `@sentry/browser`
6+
- [browser] feat: Added `onLoad` callback to `showReportDialog`
7+
- [browser] fix: Use 'native code' as a filename for some frames
68

79
## 5.2.0
810

packages/browser/src/client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export interface ReportDialogOptions {
2828
errorGeneric?: string;
2929
errorFormEntry?: string;
3030
successMessage?: string;
31+
/** Callback after reportDialog showed up */
3132
onLoad?(): void;
3233
}
3334

packages/browser/src/integrations/helpers.ts renamed to packages/browser/src/helpers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export function wrap(
7171

7272
const args = Array.prototype.slice.call(arguments);
7373

74+
// tslint:disable:no-unsafe-any
7475
try {
7576
// Attempt to invoke user-land function
7677
// NOTE: If you are a Sentry user, and you are seeing this stack frame, it
@@ -82,6 +83,7 @@ export function wrap(
8283
return fn.handleEvent.apply(this, wrappedArguments);
8384
}
8485
return fn.apply(this, wrappedArguments);
86+
// tslint:enable:no-unsafe-any
8587
} catch (ex) {
8688
ignoreNextOnError();
8789

packages/browser/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export {
2929

3030
export { BrowserOptions } from './backend';
3131
export { BrowserClient, ReportDialogOptions } from './client';
32-
export { defaultIntegrations, forceLoad, init, lastEventId, onLoad, showReportDialog, flush, close } from './sdk';
32+
export { defaultIntegrations, forceLoad, init, lastEventId, onLoad, showReportDialog, flush, close, wrap } from './sdk';
3333
export { SDK_NAME, SDK_VERSION } from './version';
3434

3535
import { Integrations as CoreIntegrations } from '@sentry/core';

packages/browser/src/integrations/breadcrumbs.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import {
1414
} from '@sentry/utils';
1515

1616
import { BrowserClient } from '../client';
17-
18-
import { breadcrumbEventHandler, keypressEventHandler, wrap } from './helpers';
17+
import { breadcrumbEventHandler, keypressEventHandler, wrap } from '../helpers';
1918

2019
const global = getGlobalObject<Window>();
2120
let lastHref: string | undefined;

packages/browser/src/integrations/globalhandlers.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { getCurrentHub } from '@sentry/core';
22
import { Event, Integration } from '@sentry/types';
33
import { addExceptionTypeValue, isString, logger, normalize, truncate } from '@sentry/utils';
44

5+
import { shouldIgnoreOnError } from '../helpers';
56
import { eventFromStacktrace } from '../parsers';
67
import {
78
_installGlobalHandler,
@@ -10,8 +11,6 @@ import {
1011
StackTrace as TraceKitStackTrace,
1112
} from '../tracekit';
1213

13-
import { shouldIgnoreOnError } from './helpers';
14-
1514
/** JSDoc */
1615
interface GlobalHandlersIntegrations {
1716
onerror: boolean;

packages/browser/src/integrations/trycatch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Integration, WrappedFunction } from '@sentry/types';
22
import { fill, getGlobalObject } from '@sentry/utils';
33

4-
import { wrap } from './helpers';
4+
import { wrap } from '../helpers';
55

66
/** Wrap timer functions and event targets to catch errors and provide better meta data */
77
export class TryCatch implements Integration {

packages/browser/src/sdk.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { getCurrentHub, initAndBind, Integrations as CoreIntegrations } from '@s
22

33
import { BrowserOptions } from './backend';
44
import { BrowserClient, ReportDialogOptions } from './client';
5+
import { wrap as internalWrap } from './helpers';
56
import { Breadcrumbs, GlobalHandlers, LinkedErrors, TryCatch, UserAgent } from './integrations';
67

78
export const defaultIntegrations = [
@@ -145,3 +146,13 @@ export function close(timeout?: number): Promise<boolean> {
145146
}
146147
return Promise.reject(false);
147148
}
149+
150+
/**
151+
* Wrap code within a try/catch block so the SDK is able to capture errors.
152+
*
153+
* @param fn A function to wrap.
154+
*/
155+
export function wrap(fn: Function): void {
156+
// tslint:disable-next-line: no-unsafe-any
157+
internalWrap(fn)();
158+
}

packages/browser/test/integrations/helpers.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { WrappedFunction } from '@sentry/types';
22
import { expect } from 'chai';
33
import { SinonSpy, spy } from 'sinon';
44

5-
import { wrap } from '../../src/integrations/helpers';
5+
import { wrap } from '../../src/helpers';
66

77
describe('wrap()', () => {
88
it('should wrap only functions', () => {

0 commit comments

Comments
 (0)
0