@@ -4,7 +4,7 @@ import { applyAggregateErrorsToEvent, createStackParser } from '../src/index';
44
55const stackParser = createStackParser ( [ 0 , line => ( { filename : line } ) ] ) ;
66const exceptionFromError = ( _stackParser : StackParser , ex : Error ) : Exception => {
7- return { value : ex . message } ;
7+ return { value : ex . message , mechanism : { type : 'instrument' , handled : true } } ;
88} ;
99
1010describe ( 'applyAggregateErrorsToEvent()' , ( ) => {
@@ -46,28 +46,63 @@ describe('applyAggregateErrorsToEvent()', () => {
4646 test ( 'should recursively walk the original exception based on the `key` option and add them as exceptions to the event' , ( ) => {
4747 const key = 'cause' ;
4848 const originalException : ExtendedError = new Error ( 'Root Error' ) ;
49- const event : Event = { exception : { values : [ exceptionFromError ( stackParser , originalException ) ] } } ;
5049 originalException [ key ] = new Error ( 'Nested Error 1' ) ;
5150 originalException [ key ] [ key ] = new Error ( 'Nested Error 2' ) ;
51+
52+ const event : Event = { exception : { values : [ exceptionFromError ( stackParser , originalException ) ] } } ;
5253 const eventHint : EventHint = { originalException } ;
54+
5355 applyAggregateErrorsToEvent ( exceptionFromError , stackParser , key , 100 , event , eventHint ) ;
5456 expect ( event ) . toStrictEqual ( {
5557 exception : {
5658 values : [
5759 {
5860 value : 'Nested Error 2' ,
61+ mechanism : {
62+ exception_id : 2 ,
63+ handled : true ,
64+ parent_id : 1 ,
65+ source : 'cause' ,
66+ type : 'chained' ,
67+ } ,
5968 } ,
6069 {
6170 value : 'Nested Error 1' ,
71+ mechanism : {
72+ exception_id : 1 ,
73+ handled : true ,
74+ parent_id : 0 ,
75+ is_exception_group : true ,
76+ source : 'cause' ,
77+ type : 'chained' ,
78+ } ,
6279 } ,
6380 {
6481 value : 'Root Error' ,
82+ mechanism : {
83+ exception_id : 0 ,
84+ handled : true ,
85+ is_exception_group : true ,
86+ type : 'instrument' ,
87+ } ,
6588 } ,
6689 ] ,
6790 } ,
6891 } ) ;
6992 } ) ;
7093
94+ test ( 'should not modify event if there are no attached errors' , ( ) => {
95+ const originalException : ExtendedError = new Error ( 'Some Error' ) ;
96+
97+ const event : Event = { exception : { values : [ exceptionFromError ( stackParser , originalException ) ] } } ;
98+ const eventHint : EventHint = { originalException } ;
99+
100+ applyAggregateErrorsToEvent ( exceptionFromError , stackParser , 'cause' , 100 , event , eventHint ) ;
101+
102+ // no changes
103+ expect ( event ) . toStrictEqual ( { exception : { values : [ exceptionFromError ( stackParser , originalException ) ] } } ) ;
104+ } ) ;
105+
71106 test ( 'should allow to limit number of attached errors' , ( ) => {
72107 const key = 'cause' ;
73108 const originalException : ExtendedError = new Error ( 'Root Error' ) ;
@@ -89,9 +124,152 @@ describe('applyAggregateErrorsToEvent()', () => {
89124 // Last exception in list should be the root exception
90125 expect ( event . exception ?. values ?. [ event . exception ?. values . length - 1 ] ) . toStrictEqual ( {
91126 value : 'Root Error' ,
127+ mechanism : {
128+ exception_id : 0 ,
129+ handled : true ,
130+ is_exception_group : true ,
131+ type : 'instrument' ,
132+ } ,
92133 } ) ;
93134 } ) ;
94135
95- test . todo ( 'should recursively walk AggregateErrors and add them as exceptions to the event' ) ;
96- test . todo ( 'should recursively walk mixed errors (Aggregate errors and based on `key`)' ) ;
136+ test ( 'should keep the original mechanism type for the root exception' , ( ) => {
137+ const fakeAggregateError : ExtendedError = new Error ( 'Root Error' ) ;
138+ fakeAggregateError . errors = [ new Error ( 'Nested Error 1' ) , new Error ( 'Nested Error 2' ) ] ;
139+
140+ const event : Event = { exception : { values : [ exceptionFromError ( stackParser , fakeAggregateError ) ] } } ;
141+ const eventHint : EventHint = { originalException : fakeAggregateError } ;
142+
143+ applyAggregateErrorsToEvent ( exceptionFromError , stackParser , 'cause' , 100 , event , eventHint ) ;
144+ expect ( event . exception ?. values ?. [ event . exception . values . length - 1 ] . mechanism ?. type ) . toBe ( 'instrument' ) ;
145+ } ) ;
146+
147+ test ( 'should recursively walk mixed errors (Aggregate errors and based on `key`)' , ( ) => {
148+ const chainedError : ExtendedError = new Error ( 'Nested Error 3' ) ;
149+ chainedError . cause = new Error ( 'Nested Error 4' ) ;
150+ const fakeAggregateError2 : ExtendedError = new Error ( 'AggregateError2' ) ;
151+ fakeAggregateError2 . errors = [ new Error ( 'Nested Error 2' ) , chainedError ] ;
152+ const fakeAggregateError1 : ExtendedError = new Error ( 'AggregateError1' ) ;
153+ fakeAggregateError1 . errors = [ new Error ( 'Nested Error 1' ) , fakeAggregateError2 ] ;
154+
155+ const event : Event = { exception : { values : [ exceptionFromError ( stackParser , fakeAggregateError1 ) ] } } ;
156+ const eventHint : EventHint = { originalException : fakeAggregateError1 } ;
157+
158+ applyAggregateErrorsToEvent ( exceptionFromError , stackParser , 'cause' , 100 , event , eventHint ) ;
159+ expect ( event ) . toStrictEqual ( {
160+ exception : {
161+ values : [
162+ {
163+ mechanism : {
164+ exception_id : 5 ,
165+ handled : true ,
166+ parent_id : 4 ,
167+ source : 'cause' ,
168+ type : 'chained' ,
169+ } ,
170+ value : 'Nested Error 4' ,
171+ } ,
172+ {
173+ mechanism : {
174+ exception_id : 4 ,
175+ handled : true ,
176+ is_exception_group : true ,
177+ parent_id : 2 ,
178+ source : 'errors[1]' ,
179+ type : 'chained' ,
180+ } ,
181+ value : 'Nested Error 3' ,
182+ } ,
183+ {
184+ mechanism : {
185+ exception_id : 3 ,
186+ handled : true ,
187+ parent_id : 2 ,
188+ source : 'errors[0]' ,
189+ type : 'chained' ,
190+ } ,
191+ value : 'Nested Error 2' ,
192+ } ,
193+ {
194+ mechanism : {
195+ exception_id : 2 ,
196+ handled : true ,
197+ is_exception_group : true ,
198+ parent_id : 0 ,
199+ source : 'errors[1]' ,
200+ type : 'chained' ,
201+ } ,
202+ value : 'AggregateError2' ,
203+ } ,
204+ {
205+ mechanism : {
206+ exception_id : 1 ,
207+ handled : true ,
208+ parent_id : 0 ,
209+ source : 'errors[0]' ,
210+ type : 'chained' ,
211+ } ,
212+ value : 'Nested Error 1' ,
213+ } ,
214+ {
215+ mechanism : {
216+ exception_id : 0 ,
217+ handled : true ,
218+ is_exception_group : true ,
219+ type : 'instrument' ,
220+ } ,
221+ value : 'AggregateError1' ,
222+ } ,
223+ ] ,
224+ } ,
225+ } ) ;
226+ } ) ;
227+
228+ test ( 'should keep the original mechanism type for the root exception' , ( ) => {
229+ const key = 'cause' ;
230+ const originalException : ExtendedError = new Error ( 'Root Error' ) ;
231+ originalException [ key ] = new Error ( 'Nested Error 1' ) ;
232+ originalException [ key ] [ key ] = new Error ( 'Nested Error 2' ) ;
233+
234+ const event : Event = { exception : { values : [ exceptionFromError ( stackParser , originalException ) ] } } ;
235+ const eventHint : EventHint = { originalException } ;
236+
237+ applyAggregateErrorsToEvent ( exceptionFromError , stackParser , key , 100 , event , eventHint ) ;
238+ expect ( event ) . toStrictEqual ( {
239+ exception : {
240+ values : [
241+ {
242+ value : 'Nested Error 2' ,
243+ mechanism : {
244+ exception_id : 2 ,
245+ handled : true ,
246+ parent_id : 1 ,
247+ source : 'cause' ,
248+ type : 'chained' ,
249+ } ,
250+ } ,
251+ {
252+ value : 'Nested Error 1' ,
253+ mechanism : {
254+ exception_id : 1 ,
255+ handled : true ,
256+ parent_id : 0 ,
257+ is_exception_group : true ,
258+ source : 'cause' ,
259+ type : 'chained' ,
260+ } ,
261+ } ,
262+ {
263+ value : 'Root Error' ,
264+ mechanism : {
265+ exception_id : 0 ,
266+ handled : true ,
267+ is_exception_group : true ,
268+ type : 'instrument' ,
269+ } ,
270+ } ,
271+ ] ,
272+ } ,
273+ } ) ;
274+ } ) ;
97275} ) ;
0 commit comments