@@ -133,12 +133,18 @@ export async function createBackendRouter(
133
133
throw new Error ( 'next is undefined' ) ;
134
134
}
135
135
136
- routerApi . openApiBackend . handleRequest ( req as Request , req , res , next ) ;
136
+ return routerApi . openApiBackend . handleRequest (
137
+ req as Request ,
138
+ req ,
139
+ res ,
140
+ next ,
141
+ ) ;
137
142
} ) ;
138
143
139
144
const middleware = MiddlewareFactory . create ( { logger, config } ) ;
140
145
141
146
router . use ( middleware . error ( ) ) ;
147
+
142
148
return router ;
143
149
}
144
150
@@ -297,15 +303,12 @@ function setupInternalRoutes(
297
303
if ( decision . result === AuthorizeResult . DENY ) {
298
304
manageDenyAuthorization ( endpointName , endpoint , req ) ;
299
305
}
300
- await routerApi . v2
306
+ return routerApi . v2
301
307
. getWorkflowsOverview ( buildPagination ( req ) , getRequestFilters ( req ) )
302
308
. then ( result => res . json ( result ) )
303
309
. catch ( error => {
304
310
auditLogRequestError ( error , endpointName , endpoint , req ) ;
305
- res
306
- . status ( 500 )
307
- . json ( { message : error . message || INTERNAL_SERVER_ERROR_MESSAGE } ) ;
308
- next ( ) ;
311
+ next ( error ) ;
309
312
} ) ;
310
313
} ,
311
314
) ;
@@ -342,19 +345,15 @@ function setupInternalRoutes(
342
345
res . status ( 200 ) . contentType ( 'text/plain' ) . send ( result ) ;
343
346
} catch ( error ) {
344
347
auditLogRequestError ( error , endpointName , endpoint , _req ) ;
345
- res
346
- . status ( 500 )
347
- . contentType ( 'text/plain' )
348
- . send ( ( error as Error ) ?. message || INTERNAL_SERVER_ERROR_MESSAGE ) ;
349
- next ( ) ;
348
+ next ( error ) ;
350
349
}
351
350
} ,
352
351
) ;
353
352
354
353
// v2
355
354
routerApi . openApiBackend . register (
356
355
'executeWorkflow' ,
357
- async ( c , req : express . Request , res : express . Response ) => {
356
+ async ( c , req : express . Request , res : express . Response , next ) => {
358
357
const workflowId = c . request . params . workflowId as string ;
359
358
const endpointName = 'executeWorkflow' ;
360
359
const endpoint = `/v2/workflows/${ workflowId } /execute` ;
@@ -385,14 +384,12 @@ function setupInternalRoutes(
385
384
386
385
const executeWorkflowRequestDTO = req . body ;
387
386
388
- await routerApi . v2
387
+ return routerApi . v2
389
388
. executeWorkflow ( executeWorkflowRequestDTO , workflowId , businessKey )
390
389
. then ( result => res . status ( 200 ) . json ( result ) )
391
390
. catch ( error => {
392
391
auditLogRequestError ( error , endpointName , endpoint , req ) ;
393
- res
394
- . status ( 500 )
395
- . json ( { message : error . message || INTERNAL_SERVER_ERROR_MESSAGE } ) ;
392
+ next ( error ) ;
396
393
} ) ;
397
394
} ,
398
395
) ;
@@ -423,18 +420,20 @@ function setupInternalRoutes(
423
420
if ( decision . result === AuthorizeResult . DENY ) {
424
421
manageDenyAuthorization ( endpointName , endpoint , _req ) ;
425
422
}
426
-
427
- await routerApi . v2
423
+ return routerApi . v2
428
424
. getWorkflowOverviewById ( workflowId )
429
425
. then ( result => res . json ( result ) )
430
- . catch ( next ) ;
426
+ . catch ( error => {
427
+ auditLogRequestError ( error , endpointName , endpoint , _req ) ;
428
+ next ( error ) ;
429
+ } ) ;
431
430
} ,
432
431
) ;
433
432
434
433
// v2
435
434
routerApi . openApiBackend . register (
436
435
'getWorkflowStatuses' ,
437
- async ( _c , _req : express . Request , res : express . Response ) => {
436
+ async ( _c , _req : express . Request , res : express . Response , next ) => {
438
437
const endpointName = 'getWorkflowStatuses' ;
439
438
const endpoint = '/v2/workflows/instances/statuses' ;
440
439
@@ -455,77 +454,59 @@ function setupInternalRoutes(
455
454
if ( decision . result === AuthorizeResult . DENY ) {
456
455
manageDenyAuthorization ( endpointName , endpoint , _req ) ;
457
456
}
458
- await routerApi . v2
457
+ return routerApi . v2
459
458
. getWorkflowStatuses ( )
460
459
. then ( result => res . status ( 200 ) . json ( result ) )
461
- . catch ( ( error : { message : string } ) => {
460
+ . catch ( error => {
462
461
auditLogRequestError ( error , endpointName , endpoint , _req ) ;
463
- res
464
- . status ( 500 )
465
- . json ( { message : error . message || INTERNAL_SERVER_ERROR_MESSAGE } ) ;
462
+ next ( error ) ;
466
463
} ) ;
467
464
} ,
468
465
) ;
469
466
470
467
// v2
471
468
routerApi . openApiBackend . register (
472
469
'getWorkflowInputSchemaById' ,
473
- async ( c , req : express . Request , res : express . Response ) => {
470
+ async ( c , req : express . Request , res : express . Response , next ) => {
474
471
const workflowId = c . request . params . workflowId as string ;
475
472
const instanceId = c . request . query . instanceId as string ;
476
473
const endpointName = 'getWorkflowInputSchemaById' ;
477
474
const endpoint = `/v2/workflows/${ workflowId } /inputSchema` ;
478
-
479
- auditLogger . auditLog ( {
480
- eventName : endpointName ,
481
- stage : 'start' ,
482
- status : 'succeeded' ,
483
- level : 'debug' ,
484
- request : req ,
485
- message : `Received request to '${ endpoint } ' endpoint` ,
486
- } ) ;
487
- const decision = await authorize (
488
- req ,
489
- orchestratorWorkflowInstanceReadPermission ,
490
- permissions ,
491
- httpAuth ,
492
- ) ;
493
- if ( decision . result === AuthorizeResult . DENY ) {
494
- manageDenyAuthorization ( endpointName , endpoint , req ) ;
495
- }
496
-
497
475
try {
476
+ auditLogger . auditLog ( {
477
+ eventName : endpointName ,
478
+ stage : 'start' ,
479
+ status : 'succeeded' ,
480
+ level : 'debug' ,
481
+ request : req ,
482
+ message : `Received request to '${ endpoint } ' endpoint` ,
483
+ } ) ;
484
+ const decision = await authorize (
485
+ req ,
486
+ orchestratorWorkflowInstanceReadPermission ,
487
+ permissions ,
488
+ httpAuth ,
489
+ ) ;
490
+ if ( decision . result === AuthorizeResult . DENY ) {
491
+ manageDenyAuthorization ( endpointName , endpoint , req ) ;
492
+ }
493
+
498
494
const workflowDefinition =
499
495
await services . orchestratorService . fetchWorkflowInfo ( {
500
496
definitionId : workflowId ,
501
497
cacheHandler : 'throw' ,
502
498
} ) ;
503
499
504
500
if ( ! workflowDefinition ) {
505
- auditLogRequestError (
506
- new Error ( `Couldn't fetch workflow definition ${ workflowId } ` ) ,
507
- endpointName ,
508
- endpoint ,
509
- req ,
501
+ throw new Error (
502
+ `Failed to fetch workflow info for workflow ${ workflowId } ` ,
510
503
) ;
511
- res
512
- . status ( 500 )
513
- . send ( `Couldn't fetch workflow definition ${ workflowId } ` ) ;
514
- return ;
515
504
}
516
-
517
505
const serviceUrl = workflowDefinition . serviceUrl ;
518
506
if ( ! serviceUrl ) {
519
- auditLogRequestError (
520
- new Error ( `Service URL is not defined for workflow ${ workflowId } ` ) ,
521
- endpointName ,
522
- endpoint ,
523
- req ,
507
+ throw new Error (
508
+ `Service URL is not defined for workflow ${ workflowId } ` ,
524
509
) ;
525
- res
526
- . status ( 500 )
527
- . send ( `Service URL is not defined for workflow ${ workflowId } ` ) ;
528
- return ;
529
510
}
530
511
531
512
const definition =
@@ -535,20 +516,9 @@ function setupInternalRoutes(
535
516
} ) ;
536
517
537
518
if ( ! definition ) {
538
- auditLogRequestError (
539
- new Error (
540
- `Couldn't fetch workflow definition of workflow source ${ workflowId } ` ,
541
- ) ,
542
- endpointName ,
543
- endpoint ,
544
- req ,
519
+ throw new Error (
520
+ 'Failed to fetch workflow definition for workflow ${workflowId}' ,
545
521
) ;
546
- res
547
- . status ( 500 )
548
- . send (
549
- `Couldn't fetch workflow definition of workflow source ${ workflowId } ` ,
550
- ) ;
551
- return ;
552
522
}
553
523
554
524
if ( ! definition . dataInputSchema ) {
@@ -569,10 +539,14 @@ function setupInternalRoutes(
569
539
)
570
540
: undefined ;
571
541
572
- const workflowInfo = await routerApi . v2 . getWorkflowInputSchemaById (
573
- workflowId ,
574
- serviceUrl ,
575
- ) ;
542
+ const workflowInfo = await routerApi . v2
543
+ . getWorkflowInputSchemaById ( workflowId , serviceUrl )
544
+ . catch ( ( error : { message : string } ) => {
545
+ auditLogRequestError ( error , endpointName , endpoint , req ) ;
546
+ res . status ( 500 ) . json ( {
547
+ message : error . message || INTERNAL_SERVER_ERROR_MESSAGE ,
548
+ } ) ;
549
+ } ) ;
576
550
577
551
if (
578
552
! workflowInfo ||
@@ -602,11 +576,9 @@ function setupInternalRoutes(
602
576
inputSchema : workflowInfo . inputSchema ,
603
577
data : inputData ,
604
578
} ) ;
605
- } catch ( error : any ) {
606
- auditLogRequestError ( error , endpointName , endpoint , req ) ;
607
- res
608
- . status ( 500 )
609
- . json ( { message : error . message || INTERNAL_SERVER_ERROR_MESSAGE } ) ;
579
+ } catch ( err ) {
580
+ auditLogRequestError ( err , endpointName , endpoint , req ) ;
581
+ next ( err ) ;
610
582
}
611
583
} ,
612
584
) ;
@@ -637,10 +609,13 @@ function setupInternalRoutes(
637
609
if ( decision . result === AuthorizeResult . DENY ) {
638
610
manageDenyAuthorization ( endpointName , endpoint , req ) ;
639
611
}
640
- await routerApi . v2
612
+ return routerApi . v2
641
613
. getInstances ( buildPagination ( req ) , getRequestFilters ( req ) , workflowId )
642
614
. then ( result => res . json ( result ) )
643
- . catch ( next ) ;
615
+ . catch ( error => {
616
+ auditLogRequestError ( error , endpointName , endpoint , req ) ;
617
+ next ( error ) ;
618
+ } ) ;
644
619
} ,
645
620
) ;
646
621
@@ -669,10 +644,13 @@ function setupInternalRoutes(
669
644
if ( decision . result === AuthorizeResult . DENY ) {
670
645
manageDenyAuthorization ( endpointName , endpoint , req ) ;
671
646
}
672
- await routerApi . v2
647
+ return routerApi . v2
673
648
. getInstances ( buildPagination ( req ) , getRequestFilters ( req ) )
674
649
. then ( result => res . json ( result ) )
675
- . catch ( next ) ;
650
+ . catch ( error => {
651
+ auditLogRequestError ( error , endpointName , endpoint , req ) ;
652
+ next ( error ) ;
653
+ } ) ;
676
654
} ,
677
655
) ;
678
656
@@ -706,15 +684,12 @@ function setupInternalRoutes(
706
684
c . request ,
707
685
QUERY_PARAM_INCLUDE_ASSESSMENT ,
708
686
) ;
709
- await routerApi . v2
687
+ return routerApi . v2
710
688
. getInstanceById ( instanceId , ! ! includeAssessment )
711
689
. then ( result => res . status ( 200 ) . json ( result ) )
712
690
. catch ( error => {
713
691
auditLogRequestError ( error , endpointName , endpoint , _req ) ;
714
- res
715
- . status ( 500 )
716
- . json ( { message : error . message || INTERNAL_SERVER_ERROR_MESSAGE } ) ;
717
- next ( ) ;
692
+ next ( error ) ;
718
693
} ) ;
719
694
} ,
720
695
) ;
@@ -745,15 +720,12 @@ function setupInternalRoutes(
745
720
if ( decision . result === AuthorizeResult . DENY ) {
746
721
manageDenyAuthorization ( endpointName , endpoint , _req ) ;
747
722
}
748
- await routerApi . v2
723
+ return routerApi . v2
749
724
. abortWorkflow ( instanceId )
750
725
. then ( result => res . json ( result ) )
751
726
. catch ( error => {
752
727
auditLogRequestError ( error , endpointName , endpoint , _req ) ;
753
- res
754
- . status ( 500 )
755
- . json ( { message : error . message || INTERNAL_SERVER_ERROR_MESSAGE } ) ;
756
- next ( ) ;
728
+ next ( error ) ;
757
729
} ) ;
758
730
} ,
759
731
) ;
0 commit comments