1
1
/* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { Event , Mechanism , StackFrame } from '@sentry/types' ;
2
+ import { Event , Exception , Mechanism , StackFrame } from '@sentry/types' ;
3
3
4
4
import { getGlobalObject } from './global' ;
5
5
import { snipLine } from './string' ;
@@ -90,23 +90,28 @@ export function parseUrl(
90
90
} ;
91
91
}
92
92
93
+ function getFirstException ( event : Event ) : Exception | undefined {
94
+ return event . exception && event . exception . values ? event . exception . values [ 0 ] : undefined ;
95
+ }
96
+
93
97
/**
94
98
* Extracts either message or type+value from an event that can be used for user-facing logs
95
99
* @returns event's description
96
100
*/
97
101
export function getEventDescription ( event : Event ) : string {
98
- if ( event . message ) {
99
- return event . message ;
102
+ const { message, event_id : eventId } = event ;
103
+ if ( message ) {
104
+ return message ;
100
105
}
101
- if ( event . exception && event . exception . values && event . exception . values [ 0 ] ) {
102
- const exception = event . exception . values [ 0 ] ;
103
106
104
- if ( exception . type && exception . value ) {
105
- return `${ exception . type } : ${ exception . value } ` ;
107
+ const firstException = getFirstException ( event ) ;
108
+ if ( firstException ) {
109
+ if ( firstException . type && firstException . value ) {
110
+ return `${ firstException . type } : ${ firstException . value } ` ;
106
111
}
107
- return exception . type || exception . value || event . event_id || '<unknown>' ;
112
+ return firstException . type || firstException . value || eventId || '<unknown>' ;
108
113
}
109
- return event . event_id || '<unknown>' ;
114
+ return eventId || '<unknown>' ;
110
115
}
111
116
112
117
/**
@@ -117,11 +122,15 @@ export function getEventDescription(event: Event): string {
117
122
* @hidden
118
123
*/
119
124
export function addExceptionTypeValue ( event : Event , value ?: string , type ?: string ) : void {
120
- event . exception = event . exception || { } ;
121
- event . exception . values = event . exception . values || [ ] ;
122
- event . exception . values [ 0 ] = event . exception . values [ 0 ] || { } ;
123
- event . exception . values [ 0 ] . value = event . exception . values [ 0 ] . value || value || '' ;
124
- event . exception . values [ 0 ] . type = event . exception . values [ 0 ] . type || type || 'Error' ;
125
+ const exception = ( event . exception = event . exception || { } ) ;
126
+ const values = ( exception . values = exception . values || [ ] ) ;
127
+ const firstException = ( values [ 0 ] = values [ 0 ] || { } ) ;
128
+ if ( ! firstException . value ) {
129
+ firstException . value = value || '' ;
130
+ }
131
+ if ( ! firstException . type ) {
132
+ firstException . type = type || 'Error' ;
133
+ }
125
134
}
126
135
127
136
/**
@@ -132,18 +141,18 @@ export function addExceptionTypeValue(event: Event, value?: string, type?: strin
132
141
* @hidden
133
142
*/
134
143
export function addExceptionMechanism ( event : Event , newMechanism ?: Partial < Mechanism > ) : void {
135
- if ( ! event . exception || ! event . exception . values ) {
144
+ const firstException = getFirstException ( event ) ;
145
+ if ( ! firstException ) {
136
146
return ;
137
147
}
138
- const exceptionValue0 = event . exception . values [ 0 ] ;
139
148
140
149
const defaultMechanism = { type : 'generic' , handled : true } ;
141
- const currentMechanism = exceptionValue0 . mechanism ;
142
- exceptionValue0 . mechanism = { ...defaultMechanism , ...currentMechanism , ...newMechanism } ;
150
+ const currentMechanism = firstException . mechanism ;
151
+ firstException . mechanism = { ...defaultMechanism , ...currentMechanism , ...newMechanism } ;
143
152
144
153
if ( newMechanism && 'data' in newMechanism ) {
145
154
const mergedData = { ...( currentMechanism && currentMechanism . data ) , ...newMechanism . data } ;
146
- exceptionValue0 . mechanism . data = mergedData ;
155
+ firstException . mechanism . data = mergedData ;
147
156
}
148
157
}
149
158
0 commit comments