11/* eslint-disable @typescript-eslint/no-explicit-any */ 
2- import  {  Event ,  Mechanism ,  StackFrame  }  from  '@sentry/types' ; 
2+ import  {  Event ,  Exception ,   Mechanism ,  StackFrame  }  from  '@sentry/types' ; 
33
44import  {  getGlobalObject  }  from  './global' ; 
55import  {  snipLine  }  from  './string' ; 
@@ -90,23 +90,28 @@ export function parseUrl(
9090  } ; 
9191} 
9292
93+ function  getFirstException ( event : Event ) : Exception  |  undefined  { 
94+   return  event . exception  &&  event . exception . values  ? event . exception . values [ 0 ]  : undefined ; 
95+ } 
96+ 
9397/** 
9498 * Extracts either message or type+value from an event that can be used for user-facing logs 
9599 * @returns  event's description 
96100 */ 
97101export  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 ; 
100105  } 
101-   if  ( event . exception  &&  event . exception . values  &&  event . exception . values [ 0 ] )  { 
102-     const  exception  =  event . exception . values [ 0 ] ; 
103106
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 }  ; 
106111    } 
107-     return  exception . type  ||  exception . value  ||  event . event_id  ||  '<unknown>' ; 
112+     return  firstException . type  ||  firstException . value  ||  eventId  ||  '<unknown>' ; 
108113  } 
109-   return  event . event_id  ||  '<unknown>' ; 
114+   return  eventId  ||  '<unknown>' ; 
110115} 
111116
112117/** 
@@ -117,11 +122,15 @@ export function getEventDescription(event: Event): string {
117122 * @hidden  
118123 */ 
119124export  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+   } 
125134} 
126135
127136/** 
@@ -132,18 +141,18 @@ export function addExceptionTypeValue(event: Event, value?: string, type?: strin
132141 * @hidden  
133142 */ 
134143export  function  addExceptionMechanism ( event : Event ,  newMechanism ?: Partial < Mechanism > ) : void { 
135-   if  ( ! event . exception  ||  ! event . exception . values )  { 
144+   const  firstException  =  getFirstException ( event ) ; 
145+   if  ( ! firstException )  { 
136146    return ; 
137147  } 
138-   const  exceptionValue0  =  event . exception . values [ 0 ] ; 
139148
140149  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  } ; 
143152
144153  if  ( newMechanism  &&  'data'  in  newMechanism )  { 
145154    const  mergedData  =  {  ...( currentMechanism  &&  currentMechanism . data ) ,  ...newMechanism . data  } ; 
146-     exceptionValue0 . mechanism . data  =  mergedData ; 
155+     firstException . mechanism . data  =  mergedData ; 
147156  } 
148157} 
149158
0 commit comments