@@ -667,13 +667,18 @@ export async function tryTargetsRecursively(
667
667
`${ currentJsonPath } .targets[${ originalIndex } ]` ,
668
668
currentInheritedConfig
669
669
) ;
670
- if ( response ?. headers . get ( 'x-portkey-gateway-exception' ) === 'true' ) {
671
- break ;
672
- }
670
+ const codes = currentTarget . strategy ?. onStatusCodes ;
671
+ const gatewayException =
672
+ response ?. headers . get ( 'x-portkey-gateway-exception' ) === 'true' ;
673
673
if (
674
- response ?. ok &&
675
- ! currentTarget . strategy ?. onStatusCodes ?. includes ( response ?. status )
674
+ // If onStatusCodes is provided, and the response status is not in the list
675
+ ( Array . isArray ( codes ) && ! codes . includes ( response ?. status ) ) ||
676
+ // If onStatusCodes is not provided, and the response is ok
677
+ ( ! codes && response ?. ok ) ||
678
+ // If the response is a gateway exception
679
+ gatewayException
676
680
) {
681
+ // Skip the fallback
677
682
break ;
678
683
}
679
684
}
@@ -790,53 +795,35 @@ export async function tryTargetsRecursively(
790
795
// tryPost always returns a Response.
791
796
// TypeError will check for all unhandled exceptions.
792
797
// GatewayError will check for all handled exceptions which cannot allow the request to proceed.
793
- if (
794
- error instanceof TypeError ||
795
- error instanceof GatewayError ||
796
- ! error . response ||
797
- ( error . response && ! ( error . response instanceof Response ) )
798
- ) {
799
- console . error (
800
- 'tryTargetsRecursively error: ' ,
801
- error . message ,
802
- error . cause ,
803
- error . stack
804
- ) ;
805
- const errorMessage =
806
- error instanceof GatewayError
807
- ? error . message
808
- : 'Something went wrong' ;
809
- response = new Response (
810
- JSON . stringify ( {
811
- status : 'failure' ,
812
- message : errorMessage ,
813
- } ) ,
814
- {
815
- status : 500 ,
816
- headers : {
817
- 'content-type' : 'application/json' ,
818
- // Add this header so that the fallback loop can be interrupted if its an exception.
819
- 'x-portkey-gateway-exception' : 'true' ,
820
- } ,
821
- }
822
- ) ;
823
- } else {
824
- response = error . response ;
825
- if ( isHandlingCircuitBreaker ) {
826
- await c . get ( 'recordCircuitBreakerFailure' ) ?.(
827
- env ( c ) ,
828
- currentInheritedConfig . id ,
829
- currentTarget . cbConfig ,
830
- currentJsonPath ,
831
- response . status
832
- ) ;
798
+ console . error (
799
+ 'tryTargetsRecursively error: ' ,
800
+ error . message ,
801
+ error . cause ,
802
+ error . stack
803
+ ) ;
804
+ const errorMessage =
805
+ error instanceof GatewayError
806
+ ? error . message
807
+ : 'Something went wrong' ;
808
+ response = new Response (
809
+ JSON . stringify ( {
810
+ status : 'failure' ,
811
+ message : errorMessage ,
812
+ } ) ,
813
+ {
814
+ status : 500 ,
815
+ headers : {
816
+ 'content-type' : 'application/json' ,
817
+ // Add this header so that the fallback loop can be interrupted if its an exception.
818
+ 'x-portkey-gateway-exception' : 'true' ,
819
+ } ,
833
820
}
834
- }
821
+ ) ;
835
822
}
836
823
break ;
837
824
}
838
825
839
- return response ;
826
+ return response ! ;
840
827
}
841
828
842
829
export function constructConfigFromRequestHeaders (
0 commit comments