@@ -168,4 +168,145 @@ describe('ScopedTelemetry', () => {
168168 expect ( scopedTelemetry [ 'histograms' ] . size ) . toBe ( 0 ) ;
169169 } ) ;
170170 } ) ;
171+
172+ describe ( 'error' , ( ) => {
173+ let mockAdd : ReturnType < typeof vi . fn > ;
174+
175+ beforeEach ( ( ) => {
176+ mockAdd = vi . fn ( ) ;
177+ mockMeter . createCounter = vi . fn ( ( ) => ( { add : mockAdd } ) ) ;
178+ } ) ;
179+
180+ it ( 'should record error with default origin Unknown when not provided' , ( ) => {
181+ const error = new Error ( 'test error' ) ;
182+ error . stack = 'Error: test error\n at func (file.ts:10:5)' ;
183+
184+ scopedTelemetry . error ( 'test.error' , error ) ;
185+
186+ expect ( mockMeter . createCounter ) . toHaveBeenCalledWith ( 'test.error' , {
187+ unit : '1' ,
188+ valueType : 1 ,
189+ description : undefined ,
190+ advice : undefined ,
191+ } ) ;
192+ expect ( mockAdd ) . toHaveBeenCalledWith ( 1 , {
193+ HandlerSource : 'Unknown' ,
194+ 'aws.emf.storage_resolution' : 1 ,
195+ 'error.type' : 'Error' ,
196+ 'error.origin' : 'Unknown' ,
197+ 'error.message' : 'Error: test error' ,
198+ 'error.stack' : 'at func (file.ts:10:5)' ,
199+ } ) ;
200+ } ) ;
201+
202+ it ( 'should record error with uncaughtException origin' , ( ) => {
203+ const error = new TypeError ( 'type error' ) ;
204+ error . stack = 'TypeError: type error\n at test (test.ts:1:1)' ;
205+
206+ scopedTelemetry . error ( 'test.error' , error , 'uncaughtException' ) ;
207+
208+ expect ( mockAdd ) . toHaveBeenCalledWith ( 1 , {
209+ HandlerSource : 'Unknown' ,
210+ 'aws.emf.storage_resolution' : 1 ,
211+ 'error.type' : 'TypeError' ,
212+ 'error.origin' : 'uncaughtException' ,
213+ 'error.message' : 'TypeError: type error' ,
214+ 'error.stack' : 'at test (test.ts:1:1)' ,
215+ } ) ;
216+ } ) ;
217+
218+ it ( 'should record error with unhandledRejection origin' , ( ) => {
219+ const error = new Error ( 'rejection' ) ;
220+ error . stack = 'Error: rejection\n at promise (p.ts:5:10)' ;
221+
222+ scopedTelemetry . error ( 'test.error' , error , 'unhandledRejection' ) ;
223+
224+ expect ( mockAdd ) . toHaveBeenCalledWith ( 1 , {
225+ HandlerSource : 'Unknown' ,
226+ 'aws.emf.storage_resolution' : 1 ,
227+ 'error.type' : 'Error' ,
228+ 'error.origin' : 'unhandledRejection' ,
229+ 'error.message' : 'Error: rejection' ,
230+ 'error.stack' : 'at promise (p.ts:5:10)' ,
231+ } ) ;
232+ } ) ;
233+
234+ it ( 'should merge config attributes with error attributes' , ( ) => {
235+ const error = new Error ( 'test' ) ;
236+ error . stack = 'Error: test\n at x (x.ts:1:1)' ;
237+
238+ scopedTelemetry . error ( 'test.error' , error , undefined , {
239+ attributes : { custom : 'value' , region : 'us-east-1' } ,
240+ } ) ;
241+
242+ expect ( mockAdd ) . toHaveBeenCalledWith ( 1 , {
243+ HandlerSource : 'Unknown' ,
244+ 'aws.emf.storage_resolution' : 1 ,
245+ custom : 'value' ,
246+ region : 'us-east-1' ,
247+ 'error.type' : 'Error' ,
248+ 'error.origin' : 'Unknown' ,
249+ 'error.message' : 'Error: test' ,
250+ 'error.stack' : 'at x (x.ts:1:1)' ,
251+ } ) ;
252+ } ) ;
253+
254+ it ( 'should pass config options to counter' , ( ) => {
255+ const error = new Error ( 'test' ) ;
256+ error . stack = 'Error: test\n at x (x.ts:1:1)' ;
257+
258+ scopedTelemetry . error ( 'test.error' , error , undefined , {
259+ unit : 'errors' ,
260+ description : 'Error counter' ,
261+ } ) ;
262+
263+ expect ( mockMeter . createCounter ) . toHaveBeenCalledWith ( 'test.error' , {
264+ unit : 'errors' ,
265+ valueType : 1 ,
266+ description : 'Error counter' ,
267+ advice : undefined ,
268+ } ) ;
269+ expect ( mockAdd ) . toHaveBeenCalledWith ( 1 , {
270+ HandlerSource : 'Unknown' ,
271+ 'aws.emf.storage_resolution' : 1 ,
272+ 'error.type' : 'Error' ,
273+ 'error.origin' : 'Unknown' ,
274+ 'error.message' : 'Error: test' ,
275+ 'error.stack' : 'at x (x.ts:1:1)' ,
276+ } ) ;
277+ } ) ;
278+
279+ it ( 'should handle non-Error string value' , ( ) => {
280+ scopedTelemetry . error ( 'test.error' , 'string error' ) ;
281+
282+ expect ( mockAdd ) . toHaveBeenCalledWith ( 1 , {
283+ HandlerSource : 'Unknown' ,
284+ 'aws.emf.storage_resolution' : 1 ,
285+ 'error.type' : 'string' ,
286+ 'error.origin' : 'Unknown' ,
287+ } ) ;
288+ } ) ;
289+
290+ it ( 'should handle non-Error null value' , ( ) => {
291+ scopedTelemetry . error ( 'test.error' , null ) ;
292+
293+ expect ( mockAdd ) . toHaveBeenCalledWith ( 1 , {
294+ HandlerSource : 'Unknown' ,
295+ 'aws.emf.storage_resolution' : 1 ,
296+ 'error.type' : 'object' ,
297+ 'error.origin' : 'Unknown' ,
298+ } ) ;
299+ } ) ;
300+
301+ it ( 'should handle non-Error undefined value' , ( ) => {
302+ scopedTelemetry . error ( 'test.error' , undefined ) ;
303+
304+ expect ( mockAdd ) . toHaveBeenCalledWith ( 1 , {
305+ HandlerSource : 'Unknown' ,
306+ 'aws.emf.storage_resolution' : 1 ,
307+ 'error.type' : 'undefined' ,
308+ 'error.origin' : 'Unknown' ,
309+ } ) ;
310+ } ) ;
311+ } ) ;
171312} ) ;
0 commit comments