File tree 5 files changed +39
-12
lines changed 5 files changed +39
-12
lines changed Original file line number Diff line number Diff line change @@ -148,10 +148,10 @@ export class Hub implements HubInterface {
148
148
/**
149
149
* @inheritDoc
150
150
*/
151
- public withScope ( callback : ( scope : Scope ) => void ) : void {
151
+ public withScope < T > ( fn : ( scope : Scope ) => T ) : T {
152
152
const scope = this . pushScope ( ) ;
153
153
try {
154
- callback ( scope ) ;
154
+ return fn ( scope ) ;
155
155
} finally {
156
156
this . popScope ( ) ;
157
157
}
Original file line number Diff line number Diff line change @@ -121,16 +121,20 @@ describe('Hub', () => {
121
121
} ) ;
122
122
123
123
describe ( 'withScope' , ( ) => {
124
+ let hub : Hub ;
125
+
126
+ beforeEach ( ( ) => {
127
+ hub = new Hub ( ) ;
128
+ } ) ;
129
+
124
130
test ( 'simple' , ( ) => {
125
- const hub = new Hub ( ) ;
126
131
hub . withScope ( ( ) => {
127
132
expect ( hub . getStack ( ) ) . toHaveLength ( 2 ) ;
128
133
} ) ;
129
134
expect ( hub . getStack ( ) ) . toHaveLength ( 1 ) ;
130
135
} ) ;
131
136
132
137
test ( 'bindClient' , ( ) => {
133
- const hub = new Hub ( ) ;
134
138
const testClient : any = { bla : 'a' } ;
135
139
hub . withScope ( ( ) => {
136
140
hub . bindClient ( testClient ) ;
@@ -139,6 +143,27 @@ describe('Hub', () => {
139
143
} ) ;
140
144
expect ( hub . getStack ( ) ) . toHaveLength ( 1 ) ;
141
145
} ) ;
146
+
147
+ test ( 'should bubble up exceptions' , ( ) => {
148
+ const error = new Error ( 'test' ) ;
149
+ expect ( ( ) => {
150
+ hub . withScope ( ( ) => {
151
+ throw error ;
152
+ } ) ;
153
+ } ) . toThrow ( error ) ;
154
+ } ) ;
155
+
156
+ test ( 'should return return value from wrapped function' , ( ) => {
157
+ // someFn represents an existing function
158
+ const someFn = ( ) => {
159
+ const hub = getCurrentHub ( ) ;
160
+ hub . setTag ( 'key' , 'value' ) ;
161
+ hub . captureMessage ( 'test' ) ;
162
+ return 'ok' ;
163
+ } ;
164
+ const value = hub . withScope ( someFn ) ; // runs someFn in a new scope
165
+ expect ( value ) . toBe ( 'ok' ) ;
166
+ } ) ;
142
167
} ) ;
143
168
144
169
test ( 'getCurrentClient' , ( ) => {
Original file line number Diff line number Diff line change @@ -170,13 +170,13 @@ export function setUser(user: User | null): void {
170
170
* This is essentially a convenience function for:
171
171
*
172
172
* pushScope();
173
- * callback ();
173
+ * fn ();
174
174
* popScope();
175
175
*
176
- * @param callback that will be enclosed into push/popScope .
176
+ * @param fn wrapped function .
177
177
*/
178
- export function withScope ( callback : ( scope : Scope ) => void ) : void {
179
- callOnHub < void > ( 'withScope' , callback ) ;
178
+ export function withScope < T > ( fn : ( scope : Scope ) => T ) : T {
179
+ return callOnHub < T > ( 'withScope' , fn ) ;
180
180
}
181
181
182
182
/**
Original file line number Diff line number Diff line change @@ -244,7 +244,7 @@ describe('Minimal', () => {
244
244
} ) ;
245
245
246
246
test ( 'withScope' , ( ) => {
247
- withScope ( scope => {
247
+ const value = withScope ( scope => {
248
248
scope . setLevel ( Severity . Warning ) ;
249
249
scope . setFingerprint ( [ '1' ] ) ;
250
250
withScope ( scope2 => {
@@ -261,8 +261,10 @@ describe('Minimal', () => {
261
261
expect ( global . __SENTRY__ . hub . _stack ) . toHaveLength ( 3 ) ;
262
262
} ) ;
263
263
expect ( global . __SENTRY__ . hub . _stack ) . toHaveLength ( 2 ) ;
264
+ return 'ok' ;
264
265
} ) ;
265
266
expect ( global . __SENTRY__ . hub . _stack ) . toHaveLength ( 1 ) ;
267
+ expect ( value ) . toBe ( 'ok' ) ;
266
268
} ) ;
267
269
268
270
test ( 'setExtras' , ( ) => {
Original file line number Diff line number Diff line change @@ -61,12 +61,12 @@ export interface Hub {
61
61
* This is essentially a convenience function for:
62
62
*
63
63
* pushScope();
64
- * callback ();
64
+ * fn ();
65
65
* popScope();
66
66
*
67
- * @param callback that will be enclosed into push/popScope .
67
+ * @param fn wrapped function .
68
68
*/
69
- withScope ( callback : ( scope : Scope ) => void ) : void ;
69
+ withScope < T > ( fn : ( scope : Scope ) => T ) : T ;
70
70
71
71
/** Returns the client of the top stack. */
72
72
getClient ( ) : Client | undefined ;
You can’t perform that action at this time.
0 commit comments