@@ -4,8 +4,11 @@ import { ExecutorExecuteResponseInterface, ExecutorInterface } from '../executor
44import { join } from 'path' ;
55import { Job } from 'bullmq' ;
66import { BackendConfigV1Dto } from '../_dto/backend-config-v1.dto' ;
7- import { BackendResultInterface } from '../_interfaces/backend-result.interface' ;
7+ import { BackendResultInfoInterface , BackendResultInterface } from '../_interfaces/backend-result.interface' ;
88import { BackendCodesEnum } from '../_interfaces/backend-codes.enum' ;
9+ import { validateOrReject } from 'class-validator' ;
10+ import { BackendResultInfoDto } from '../_dto/backend-result-info.dto' ;
11+ import { plainToInstance } from 'class-transformer' ;
912
1013export class CatchAllExecutor implements ExecutorInterface {
1114 public constructor ( public service : BackendRunnerService ) { }
@@ -46,29 +49,70 @@ export class CatchAllExecutor implements ExecutorInterface {
4649 try {
4750 if ( process . status !== 0 ) {
4851 this . service . logger . error ( `Error executing backend ${ backend . name } ` ) ;
52+ const error = this . extractLastJsonImproved ( process . error ) ;
53+ const errorSchema = plainToInstance ( BackendResultInfoDto , error ) ;
54+ await validateOrReject ( errorSchema ) ;
55+
4956 return {
5057 backend : backend . name ,
5158 status : process . status ,
52- error : JSON . parse ( process . output ) ,
59+ error,
5360 } ;
5461 }
5562
5663 this . service . logger . log ( `Backend ${ backend . name } executed successfully` ) ;
64+ const output = this . extractLastJsonImproved ( process . output ) ;
65+ const outputSchema = plainToInstance ( BackendResultInfoDto , output ) ;
66+ await validateOrReject ( outputSchema ) ;
67+
5768 return {
5869 backend : backend . name ,
5970 status : process . status ,
60- output : JSON . parse ( process . output ) ,
71+ output,
6172 } ;
6273 } catch ( e ) {
6374 this . service . logger . error ( `Error parsing JSON output from backend ${ backend . name } ` ) ;
75+
6476 return {
6577 backend : backend . name ,
6678 status : BackendCodesEnum . INVALID_JSON_RESPONSE ,
67- error : {
68- status : BackendCodesEnum . INVALID_JSON_RESPONSE ,
69- message : process . error ,
70- } ,
79+ message : `Invalid JSON response from backend: ${ process . error || process . output } ` ,
7180 } ;
7281 }
7382 }
83+
84+ private extractLastJsonImproved ( stdout : string ) : BackendResultInfoInterface {
85+ const jsonCandidates = [ ] ;
86+ let braceCount = 0 ,
87+ inString = false ,
88+ escape = false ,
89+ currentJson = '' ;
90+
91+ for ( const char of stdout ) {
92+ if ( escape ) {
93+ escape = false ;
94+ } else if ( char === '\\' ) {
95+ escape = true ;
96+ } else if ( char === '"' ) {
97+ inString = ! inString ;
98+ } else if ( ! inString && char === '{' ) {
99+ braceCount ++ ;
100+ if ( braceCount === 1 ) {
101+ currentJson = char ;
102+ continue ;
103+ }
104+ } else if ( ! inString && char === '}' ) {
105+ braceCount -- ;
106+ }
107+
108+ if ( braceCount > 0 ) currentJson += char ;
109+ if ( braceCount === 0 && currentJson !== '' ) {
110+ currentJson += char ;
111+ jsonCandidates . push ( currentJson ) ;
112+ currentJson = '' ;
113+ }
114+ }
115+
116+ return JSON . parse ( jsonCandidates [ jsonCandidates . length - 1 ] ) ;
117+ }
74118}
0 commit comments