1
- import { getCurrentHub , Hub , Scope } from '@sentry/hub' ;
2
1
import {
3
2
Breadcrumb ,
4
3
CaptureContext ,
@@ -9,25 +8,19 @@ import {
9
8
Primitive ,
10
9
Severity ,
11
10
SeverityLevel ,
12
- Transaction ,
13
11
TransactionContext ,
14
12
User ,
15
13
} from '@sentry/types' ;
16
14
17
- /**
18
- * This calls a function on the current hub.
19
- * @param method function to call on hub.
20
- * @param args to pass to function.
21
- */
22
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
23
- function callOnHub < T > ( method : string , ...args : any [ ] ) : T {
24
- const hub = getCurrentHub ( ) ;
25
- if ( hub && hub [ method as keyof Hub ] ) {
26
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
27
- return ( hub [ method as keyof Hub ] as any ) ( ...args ) ;
28
- }
29
- throw new Error ( `No hub defined or ${ method } was not found on the hub, please open a bug report.` ) ;
30
- }
15
+ import { getCurrentHub , Hub } from './hub' ;
16
+ import { Scope } from './scope' ;
17
+
18
+ // Note: All functions in this file are typed with a return value of `ReturnType<Hub[HUB_FUNCTION]>`,
19
+ // where HUB_FUNCTION is some method on the Hub class.
20
+ //
21
+ // This is done to make sure the top level SDK methods stay in sync with the hub methods.
22
+ // Although every method here has an explicit return type, some of them (that map to void returns) do not
23
+ // contain `return` keywords. This is done to save on bundle size, as `return` is not minifiable.
31
24
32
25
/**
33
26
* Captures an exception event and sends it to Sentry.
@@ -36,14 +29,8 @@ function callOnHub<T>(method: string, ...args: any[]): T {
36
29
* @returns The generated eventId.
37
30
*/
38
31
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
39
- export function captureException ( exception : any , captureContext ?: CaptureContext ) : string {
40
- const syntheticException = new Error ( 'Sentry syntheticException' ) ;
41
-
42
- return callOnHub ( 'captureException' , exception , {
43
- captureContext,
44
- originalException : exception ,
45
- syntheticException,
46
- } ) ;
32
+ export function captureException ( exception : any , captureContext ?: CaptureContext ) : ReturnType < Hub [ 'captureException' ] > {
33
+ return getCurrentHub ( ) . captureException ( exception , { captureContext } ) ;
47
34
}
48
35
49
36
/**
@@ -57,19 +44,12 @@ export function captureMessage(
57
44
message : string ,
58
45
// eslint-disable-next-line deprecation/deprecation
59
46
captureContext ?: CaptureContext | Severity | SeverityLevel ,
60
- ) : string {
61
- const syntheticException = new Error ( message ) ;
62
-
47
+ ) : ReturnType < Hub [ 'captureMessage' ] > {
63
48
// This is necessary to provide explicit scopes upgrade, without changing the original
64
49
// arity of the `captureMessage(message, level)` method.
65
50
const level = typeof captureContext === 'string' ? captureContext : undefined ;
66
51
const context = typeof captureContext !== 'string' ? { captureContext } : undefined ;
67
-
68
- return callOnHub ( 'captureMessage' , message , level , {
69
- originalException : message ,
70
- syntheticException,
71
- ...context ,
72
- } ) ;
52
+ return getCurrentHub ( ) . captureMessage ( message , level , context ) ;
73
53
}
74
54
75
55
/**
@@ -78,16 +58,16 @@ export function captureMessage(
78
58
* @param event The event to send to Sentry.
79
59
* @returns The generated eventId.
80
60
*/
81
- export function captureEvent ( event : Event ) : string {
82
- return callOnHub ( ' captureEvent' , event ) ;
61
+ export function captureEvent ( event : Event ) : ReturnType < Hub [ 'captureEvent' ] > {
62
+ return getCurrentHub ( ) . captureEvent ( event ) ;
83
63
}
84
64
85
65
/**
86
66
* Callback to set context information onto the scope.
87
67
* @param callback Callback function that receives Scope.
88
68
*/
89
- export function configureScope ( callback : ( scope : Scope ) => void ) : void {
90
- callOnHub < void > ( ' configureScope' , callback ) ;
69
+ export function configureScope ( callback : ( scope : Scope ) => void ) : ReturnType < Hub [ 'configureScope' ] > {
70
+ getCurrentHub ( ) . configureScope ( callback ) ;
91
71
}
92
72
93
73
/**
@@ -98,8 +78,8 @@ export function configureScope(callback: (scope: Scope) => void): void {
98
78
*
99
79
* @param breadcrumb The breadcrumb to record.
100
80
*/
101
- export function addBreadcrumb ( breadcrumb : Breadcrumb ) : void {
102
- callOnHub < void > ( ' addBreadcrumb' , breadcrumb ) ;
81
+ export function addBreadcrumb ( breadcrumb : Breadcrumb ) : ReturnType < Hub [ 'addBreadcrumb' ] > {
82
+ getCurrentHub ( ) . addBreadcrumb ( breadcrumb ) ;
103
83
}
104
84
105
85
/**
@@ -108,33 +88,33 @@ export function addBreadcrumb(breadcrumb: Breadcrumb): void {
108
88
* @param context Any kind of data. This data will be normalized.
109
89
*/
110
90
// eslint-disable-next-line @typescript-eslint/no-explicit-any
111
- export function setContext ( name : string , context : { [ key : string ] : any } | null ) : void {
112
- callOnHub < void > ( ' setContext' , name , context ) ;
91
+ export function setContext ( name : string , context : { [ key : string ] : any } | null ) : ReturnType < Hub [ 'setContext' ] > {
92
+ getCurrentHub ( ) . setContext ( name , context ) ;
113
93
}
114
94
115
95
/**
116
96
* Set an object that will be merged sent as extra data with the event.
117
97
* @param extras Extras object to merge into current context.
118
98
*/
119
- export function setExtras ( extras : Extras ) : void {
120
- callOnHub < void > ( ' setExtras' , extras ) ;
99
+ export function setExtras ( extras : Extras ) : ReturnType < Hub [ 'setExtras' ] > {
100
+ getCurrentHub ( ) . setExtras ( extras ) ;
121
101
}
122
102
123
103
/**
124
- * Set an object that will be merged sent as tags data with the event.
125
- * @param tags Tags context object to merge into current context.
104
+ * Set key:value that will be sent as extra data with the event.
105
+ * @param key String of extra
106
+ * @param extra Any kind of data. This data will be normalized.
126
107
*/
127
- export function setTags ( tags : { [ key : string ] : Primitive } ) : void {
128
- callOnHub < void > ( 'setTags' , tags ) ;
108
+ export function setExtra ( key : string , extra : Extra ) : ReturnType < Hub [ 'setExtra' ] > {
109
+ getCurrentHub ( ) . setExtra ( key , extra ) ;
129
110
}
130
111
131
112
/**
132
- * Set key:value that will be sent as extra data with the event.
133
- * @param key String of extra
134
- * @param extra Any kind of data. This data will be normalized.
113
+ * Set an object that will be merged sent as tags data with the event.
114
+ * @param tags Tags context object to merge into current context.
135
115
*/
136
- export function setExtra ( key : string , extra : Extra ) : void {
137
- callOnHub < void > ( 'setExtra' , key , extra ) ;
116
+ export function setTags ( tags : { [ key : string ] : Primitive } ) : ReturnType < Hub [ 'setTags' ] > {
117
+ getCurrentHub ( ) . setTags ( tags ) ;
138
118
}
139
119
140
120
/**
@@ -145,17 +125,17 @@ export function setExtra(key: string, extra: Extra): void {
145
125
* @param key String key of tag
146
126
* @param value Value of tag
147
127
*/
148
- export function setTag ( key : string , value : Primitive ) : void {
149
- callOnHub < void > ( ' setTag' , key , value ) ;
128
+ export function setTag ( key : string , value : Primitive ) : ReturnType < Hub [ 'setTag' ] > {
129
+ getCurrentHub ( ) . setTag ( key , value ) ;
150
130
}
151
131
152
132
/**
153
133
* Updates user context information for future events.
154
134
*
155
135
* @param user User context object to be set in the current context. Pass `null` to unset the user.
156
136
*/
157
- export function setUser ( user : User | null ) : void {
158
- callOnHub < void > ( ' setUser' , user ) ;
137
+ export function setUser ( user : User | null ) : ReturnType < Hub [ 'setUser' ] > {
138
+ getCurrentHub ( ) . setUser ( user ) ;
159
139
}
160
140
161
141
/**
@@ -171,23 +151,8 @@ export function setUser(user: User | null): void {
171
151
*
172
152
* @param callback that will be enclosed into push/popScope.
173
153
*/
174
- export function withScope ( callback : ( scope : Scope ) => void ) : void {
175
- callOnHub < void > ( 'withScope' , callback ) ;
176
- }
177
-
178
- /**
179
- * Calls a function on the latest client. Use this with caution, it's meant as
180
- * in "internal" helper so we don't need to expose every possible function in
181
- * the shim. It is not guaranteed that the client actually implements the
182
- * function.
183
- *
184
- * @param method The method to call on the client/client.
185
- * @param args Arguments to pass to the client/fontend.
186
- * @hidden
187
- */
188
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
189
- export function _callOnClient ( method : string , ...args : any [ ] ) : void {
190
- callOnHub < void > ( '_invokeClient' , method , ...args ) ;
154
+ export function withScope ( callback : ( scope : Scope ) => void ) : ReturnType < Hub [ 'withScope' ] > {
155
+ getCurrentHub ( ) . withScope ( callback ) ;
191
156
}
192
157
193
158
/**
@@ -210,6 +175,6 @@ export function _callOnClient(method: string, ...args: any[]): void {
210
175
export function startTransaction (
211
176
context : TransactionContext ,
212
177
customSamplingContext ?: CustomSamplingContext ,
213
- ) : Transaction {
214
- return callOnHub ( ' startTransaction' , { ...context } , customSamplingContext ) ;
178
+ ) : ReturnType < Hub [ 'startTransaction' ] > {
179
+ return getCurrentHub ( ) . startTransaction ( { ...context } , customSamplingContext ) ;
215
180
}
0 commit comments