@@ -35,6 +35,35 @@ import MemberService from './member.service'
3535
3636/* eslint-disable @typescript-eslint/no-explicit-any */
3737
38+ /**
39+ * Extracts the first app-code frame from an error's stack trace as the location.
40+ * Uses process.cwd() (the service root) to match only this service's own source
41+ * files, skipping node_modules and shared library frames.
42+ */
43+ function extractLocationFromError ( error : unknown ) : string {
44+ const stack = ( error as any ) ?. stack
45+ if ( ! stack ) return 'unknown'
46+
47+ const serviceDir = process . cwd ( )
48+ for ( const line of ( stack as string ) . split ( '\n' ) ) {
49+ if ( line . includes ( serviceDir ) && ! line . includes ( 'node_modules' ) ) {
50+ // Named frame: "at [async] FunctionName (file:line:col)"
51+ const named = line . match ( / a t \s + ( .+ ) \s + \( ( .+ ) : ( \d + ) : \d + \) / )
52+ if ( named ) {
53+ const file = named [ 2 ] . replace ( `${ serviceDir } /` , '' )
54+ return `${ file } :${ named [ 3 ] } (${ named [ 1 ] . trim ( ) } )`
55+ }
56+ // Anonymous frame: "at file:line:col"
57+ const anon = line . match ( / a t \s + \( ? ( .+ ) : ( \d + ) : \d + \) ? / )
58+ if ( anon ) {
59+ return `${ anon [ 1 ] . replace ( `${ serviceDir } /` , '' ) } :${ anon [ 2 ] } `
60+ }
61+ }
62+ }
63+
64+ return 'unknown'
65+ }
66+
3867export default class DataSinkService extends LoggerBase {
3968 private readonly repo : DataSinkRepository
4069
@@ -55,15 +84,12 @@ export default class DataSinkService extends LoggerBase {
5584 private async triggerResultError (
5685 resultInfo : IResultData ,
5786 resultExists : boolean ,
58- location : string ,
59- message : string ,
6087 error : any ,
6188 metadata ?: Record < string , unknown > ,
6289 ) : Promise < void > {
6390 // eslint-disable-next-line @typescript-eslint/no-explicit-any
6491 const errorData : any = {
65- location,
66- message,
92+ location : extractLocationFromError ( error ) ,
6793
6894 metadata,
6995
@@ -463,14 +489,7 @@ export default class DataSinkService extends LoggerBase {
463489 if ( ! result . success ) {
464490 const resultInfo = single ( results , ( r ) => r . id === resultId )
465491
466- await this . triggerResultError (
467- resultInfo ,
468- batchEntry . created ,
469- 'process-result' ,
470- 'Error processing result.' ,
471- result . err ,
472- result . metadata ,
473- )
492+ await this . triggerResultError ( resultInfo , batchEntry . created , result . err , result . metadata )
474493
475494 errors ++
476495 } else {
0 commit comments