Skip to content

Commit 01cefd8

Browse files
committed
ref: Return function call result from wrap method
1 parent 4a726ae commit 01cefd8

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- [core] fix: When using enabled:false integrations shouldnt be installed (#2181)
66
- [browser] feat: Add support for custom schemes to Tracekit
7+
- [browser] ref: Return function call result from `wrap` method
78
- [browser] ref: Better UnhandledRejection messages (#2185)
89
- [browser] test: Complete rewrite of Browser Integration Tests (#2176)
910
- [node] feat: Add cookies as an optional property in the request handler (#2167)

packages/browser/src/sdk.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,10 @@ export function close(timeout?: number): Promise<boolean> {
159159
* Wrap code within a try/catch block so the SDK is able to capture errors.
160160
*
161161
* @param fn A function to wrap.
162+
*
163+
* @returns The result of wrapped function call.
162164
*/
163-
export function wrap(fn: Function): void {
165+
export function wrap<T>(fn: () => T): ReturnType<typeof fn> {
164166
// tslint:disable-next-line: no-unsafe-any
165-
internalWrap(fn)();
167+
return internalWrap(fn)();
166168
}

packages/browser/test/unit/index.test.ts

+46
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
init,
1515
Integrations,
1616
Scope,
17+
wrap,
1718
} from '../../src';
1819

1920
import { SimpleTransport } from './mocks/simpletransport';
@@ -180,3 +181,48 @@ describe('SentryBrowser initialization', () => {
180181
expect(global.__SENTRY__.hub._stack[0].client.getOptions().release).to.be.undefined;
181182
});
182183
});
184+
185+
describe('wrap()', () => {
186+
it('should wrap and call function while capturing error', done => {
187+
getCurrentHub().bindClient(
188+
new BrowserClient({
189+
beforeSend: (event: Event) => {
190+
expect(event.exception!.values![0].type).to.equal('TypeError');
191+
expect(event.exception!.values![0].value).to.equal('mkey');
192+
done();
193+
return null;
194+
},
195+
dsn,
196+
}),
197+
);
198+
199+
wrap(() => {
200+
throw new TypeError('mkey');
201+
});
202+
});
203+
204+
it('should return result of a function call', () => {
205+
const result = wrap(() => 2);
206+
expect(result).to.equal(2);
207+
});
208+
209+
it('should allow for passing this and arguments through binding', () => {
210+
const result = wrap(
211+
function(this: any, a: string, b: number): any[] {
212+
return [this, a, b];
213+
}.bind({ context: 'this' }, 'b', 42),
214+
);
215+
216+
expect((result as any[])[0]).to.deep.equal({ context: 'this' });
217+
expect((result as any[])[1]).to.equal('b');
218+
expect((result as any[])[2]).to.equal(42);
219+
220+
const result2 = wrap(
221+
function(this: { x: number }): number {
222+
return this.x;
223+
}.bind({ x: 42 }),
224+
);
225+
226+
expect(result2).to.equal(42);
227+
});
228+
});

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { SinonSpy, spy } from 'sinon';
44

55
import { wrap } from '../../../src/helpers';
66

7-
describe('wrap()', () => {
7+
describe('internal wrap()', () => {
88
it('should wrap only functions', () => {
99
const fn = () => 1337;
1010
const obj = { pickle: 'Rick' };

0 commit comments

Comments
 (0)