@@ -6,17 +6,23 @@ import { Job } from 'bullmq';
66import { BackendConfigV1Dto } from '../_dto/backend-config-v1.dto' ;
77import { BackendResultInfoInterface , BackendResultInterface } from '../_interfaces/backend-result.interface' ;
88import { BackendCodesEnum } from '../_interfaces/backend-codes.enum' ;
9- import { validateOrReject } from 'class-validator' ;
9+ import { ValidationError , validateOrReject } from 'class-validator' ;
1010import { BackendResultInfoDto } from '../_dto/backend-result-info.dto' ;
1111import { plainToInstance } from 'class-transformer' ;
1212
13+ interface ValidationRecursive {
14+ [ key : string ] : string ;
15+ }
16+
1317export class CatchAllExecutor implements ExecutorInterface {
14- public constructor ( public service : BackendRunnerService ) { }
18+ public constructor ( public service : BackendRunnerService ) { }
1519
1620 public async execute ( { job } ) : Promise < ExecutorExecuteResponseInterface > {
1721 let status = 0 ;
1822 const data = [ ] ;
1923
24+ const numberOfBackends = this . service . backendsConfig . backendsConfigData . length ;
25+
2026 for await ( const backend of this . service . backendsConfig . backendsConfigData ) {
2127 if ( ! backend . active ) {
2228 this . service . logger . warn ( `backend ${ backend . name } is not active` ) ;
@@ -32,6 +38,8 @@ export class CatchAllExecutor implements ExecutorInterface {
3238 this . service . logger . log ( 'stop on Error' ) ;
3339 break ;
3440 }
41+
42+ await job . updateProgress ( 100 / numberOfBackends ) ;
3543 }
3644
3745 return {
@@ -49,7 +57,7 @@ export class CatchAllExecutor implements ExecutorInterface {
4957 try {
5058 if ( process . status !== 0 ) {
5159 this . service . logger . error ( `Error executing backend ${ backend . name } ` ) ;
52- const error = this . extractLastJsonImproved ( process . error ) ;
60+ const error = this . extractLastJsonImproved ( process . error || process . output ) ;
5361 const errorSchema = plainToInstance ( BackendResultInfoDto , error ) ;
5462 await validateOrReject ( errorSchema ) ;
5563
@@ -70,18 +78,34 @@ export class CatchAllExecutor implements ExecutorInterface {
7078 status : process . status ,
7179 output,
7280 } ;
73- } catch ( e ) {
74- this . service . logger . error ( `Error parsing JSON output from backend ${ backend . name } ` ) ;
81+ } catch ( errors ) {
82+ console . log ( 'errors' , errors )
83+ let validations : ValidationRecursive = { } ;
84+ for ( const error of errors ) {
85+ validations = { ...validations , ...this . validationRecursive ( error ) } ;
86+ }
87+
88+ this . service . logger . error ( `Invalid JSON response from backend ${ backend . name } , erreur de validation : ${ Object . keys ( validations ) . join ( ', ' ) } ` . trim ( ) ) ;
7589
7690 return {
7791 backend : backend . name ,
7892 status : BackendCodesEnum . INVALID_JSON_RESPONSE ,
79- message : `Invalid JSON response from backend: ${ process . error || process . output } ` ,
93+ message : `Erreur de validation : ${ Object . keys ( validations ) . join ( ', ' ) } ` . trim ( ) ,
94+ error : {
95+ status : BackendCodesEnum . INVALID_JSON_RESPONSE ,
96+ message : `Erreur de validation : ${ Object . keys ( validations ) . join ( ', ' ) } ` . trim ( ) ,
97+ data : validations ,
98+ } ,
8099 } ;
81100 }
82101 }
83102
84103 private extractLastJsonImproved ( stdout : string ) : BackendResultInfoInterface {
104+ if ( ! stdout ) return {
105+ status : BackendCodesEnum . INVALID_JSON_RESPONSE ,
106+ message : 'No output' ,
107+ }
108+
85109 const jsonCandidates = [ ] ;
86110 let braceCount = 0 ,
87111 inString = false ,
@@ -113,6 +137,32 @@ export class CatchAllExecutor implements ExecutorInterface {
113137 }
114138 }
115139
140+ if ( jsonCandidates . length === 0 ) return {
141+ status : BackendCodesEnum . INVALID_JSON_RESPONSE ,
142+ message : 'No JSON output' ,
143+ }
144+
116145 return JSON . parse ( jsonCandidates [ jsonCandidates . length - 1 ] ) ;
117146 }
147+
148+ private validationRecursive ( error : ValidationError , prefix = '' ) : ValidationRecursive {
149+ let validations = { } ;
150+ if ( error . constraints ) {
151+ validations [ `${ prefix + error . property } ` ] = Object . values ( error . constraints ) [ 0 ] ;
152+ }
153+ if ( error . children . length > 0 ) {
154+ for ( const errorChild of error . children ) {
155+ if ( errorChild . constraints ) {
156+ validations [ `${ prefix + error . property } .${ errorChild . property } ` ] = Object . values ( errorChild . constraints ) [ 0 ] ;
157+ }
158+ if ( errorChild . children . length > 0 ) {
159+ validations = {
160+ ...validations ,
161+ ...this . validationRecursive ( errorChild , `${ prefix + error . property } .${ errorChild . property } .` ) ,
162+ } ;
163+ }
164+ }
165+ }
166+ return validations ;
167+ }
118168}
0 commit comments