@@ -470,7 +470,7 @@ protected Message<?> sendAndReceiveMessage(Object object) {
470470 return (Message <?>) doSendAndReceive (object , false );
471471 }
472472
473- @ Nullable
473+ @ Nullable // NOSONAR
474474 private Object doSendAndReceive (Object object , boolean shouldConvert ) {
475475 initializeIfNecessary ();
476476 Assert .notNull (object , "request must not be null" );
@@ -481,8 +481,7 @@ private Object doSendAndReceive(Object object, boolean shouldConvert) {
481481
482482 registerReplyMessageCorrelatorIfNecessary ();
483483
484- Object reply = null ;
485- Throwable error = null ;
484+ Object reply ;
486485 Message <?> requestMessage = null ;
487486 try {
488487 if (this .countsEnabled ) {
@@ -491,37 +490,31 @@ private Object doSendAndReceive(Object object, boolean shouldConvert) {
491490 if (shouldConvert ) {
492491 reply = this .messagingTemplate .convertSendAndReceive (channel , object , Object .class ,
493492 this .historyWritingPostProcessor );
494- if (reply instanceof Throwable ) {
495- error = (Throwable ) reply ;
496- }
497493 }
498494 else {
499495 requestMessage = (object instanceof Message <?>)
500496 ? (Message <?>) object : this .requestMapper .toMessage (object );
501497 Assert .state (requestMessage != null , () -> "request mapper resulted in no message for " + object );
502498 requestMessage = this .historyWritingPostProcessor .postProcessMessage (requestMessage );
503499 reply = this .messagingTemplate .sendAndReceive (channel , requestMessage );
504- if (reply instanceof ErrorMessage ) {
505- error = ((ErrorMessage ) reply ).getPayload ();
506- }
507500 }
501+
508502 if (reply == null && this .errorOnTimeout ) {
509- if (object instanceof Message ) {
510- error = new MessageTimeoutException ((Message <?>) object , "No reply received within timeout" );
511- }
512- else {
513- error = new MessageTimeoutException ("No reply received within timeout" );
514- }
503+ throwMessageTimeoutException (object , "No reply received within timeout" );
515504 }
516505 }
517506 catch (Exception ex ) {
518507 if (logger .isDebugEnabled ()) {
519508 logger .debug ("failure occurred in gateway sendAndReceive: " + ex .getMessage ());
520509 }
521- error = ex ;
510+ reply = ex ;
522511 }
523512
524- if (error != null ) {
513+ if (reply instanceof Throwable || reply instanceof ErrorMessage ) {
514+ Throwable error =
515+ reply instanceof ErrorMessage
516+ ? ((ErrorMessage ) reply ).getPayload ()
517+ : (Throwable ) reply ;
525518 return handleSendAndReceiveError (object , requestMessage , error , shouldConvert );
526519 }
527520 return reply ;
@@ -534,46 +527,54 @@ private Object handleSendAndReceiveError(Object object, @Nullable Message<?> req
534527 MessageChannel errorChan = getErrorChannel ();
535528 if (errorChan != null ) {
536529 ErrorMessage errorMessage = buildErrorMessage (requestMessage , error );
537- Message <?> errorFlowReply = null ;
538- try {
539- errorFlowReply = this .messagingTemplate .sendAndReceive (errorChan , errorMessage );
540- }
541- catch (Exception errorFlowFailure ) {
542- throw new MessagingException (errorMessage , "failure occurred in error-handling flow" ,
543- errorFlowFailure );
544- }
545- if (shouldConvert ) {
546- Object result = (errorFlowReply != null ) ? errorFlowReply .getPayload () : null ;
547- if (result instanceof Throwable ) {
548- rethrow ((Throwable ) result , "error flow returned Exception" );
549- }
550- return result ;
551- }
552- if (errorFlowReply != null && errorFlowReply .getPayload () instanceof Throwable ) {
553- rethrow ((Throwable ) errorFlowReply .getPayload (), "error flow returned an Error Message" );
554- }
530+ Message <?> errorFlowReply = sendErrorMessageAndReceive (errorChan , errorMessage );
555531 if (errorFlowReply == null && this .errorOnTimeout ) {
556- if (object instanceof Message ) {
557- throw new MessageTimeoutException (( Message <?>) object ,
558- "No reply received from error channel within timeout" );
559- }
560- else {
561- throw new MessageTimeoutException ( "No reply received from error channel within timeout" );
562- }
532+ throwMessageTimeoutException (object , "No reply received from error channel within timeout" );
533+ return null ; // unreachable
534+ }
535+ else {
536+ return shouldConvert && errorFlowReply != null
537+ ? errorFlowReply . getPayload ()
538+ : errorFlowReply ;
563539 }
564- return errorFlowReply ;
565540 }
566541 else {
542+ Throwable errorToReThrow = error ;
567543 if (error instanceof MessagingException &&
568544 requestMessage != null && requestMessage .getHeaders ().getErrorChannel () != null ) {
569545 // We are in nested flow where upstream expects errors in its own errorChannel header.
570- error = new MessageHandlingException (requestMessage , error );
546+ errorToReThrow = new MessageHandlingException (requestMessage , error );
571547 }
572- rethrow (error , "gateway received checked Exception" );
548+ rethrow (errorToReThrow , "gateway received checked Exception" );
573549 return null ; // unreachable
574550 }
575551 }
576552
553+ @ Nullable
554+ private Message <?> sendErrorMessageAndReceive (MessageChannel errorChan , ErrorMessage errorMessage ) {
555+ Message <?> errorFlowReply ;
556+ try {
557+ errorFlowReply = this .messagingTemplate .sendAndReceive (errorChan , errorMessage );
558+ }
559+ catch (Exception errorFlowFailure ) {
560+ throw new MessagingException (errorMessage , "failure occurred in error-handling flow" ,
561+ errorFlowFailure );
562+ }
563+ if (errorFlowReply != null && errorFlowReply .getPayload () instanceof Throwable ) {
564+ rethrow ((Throwable ) errorFlowReply .getPayload (), "error flow returned an Error Message" );
565+ }
566+ return errorFlowReply ;
567+ }
568+
569+ private void throwMessageTimeoutException (Object object , String exceptionMessage ) {
570+ if (object instanceof Message ) {
571+ throw new MessageTimeoutException ((Message <?>) object , exceptionMessage );
572+ }
573+ else {
574+ throw new MessageTimeoutException (exceptionMessage );
575+ }
576+ }
577+
577578 protected Mono <Message <?>> sendAndReceiveMessageReactive (Object object ) {
578579 initializeIfNecessary ();
579580 Assert .notNull (object , "request must not be null" );
0 commit comments