|
10 | 10 | import org.jetbrains.annotations.Nullable; |
11 | 11 | import org.slf4j.Logger; |
12 | 12 | import org.slf4j.LoggerFactory; |
| 13 | +import org.springframework.amqp.AmqpTimeoutException; |
| 14 | +import org.springframework.amqp.core.AmqpMessageReturnedException; |
| 15 | +import org.springframework.amqp.core.AmqpReplyTimeoutException; |
13 | 16 | import org.springframework.beans.factory.annotation.Value; |
14 | 17 | import org.springframework.http.HttpStatus; |
15 | 18 | import org.springframework.http.HttpStatusCode; |
|
19 | 22 | import java.util.Collections; |
20 | 23 | import java.util.Map; |
21 | 24 | import java.util.concurrent.CompletableFuture; |
| 25 | +import java.util.concurrent.CompletionException; |
| 26 | +import java.util.concurrent.TimeoutException; |
22 | 27 |
|
23 | 28 | /** |
24 | 29 | * Matthew Horridge |
@@ -76,9 +81,19 @@ private CompletableFuture<RpcResponse> sendMessage(RpcRequest request, String ac |
76 | 81 | payload, userId); |
77 | 82 | return reply |
78 | 83 | .exceptionally(e -> { |
79 | | - // Convert all exceptions to a ResponseStatusException |
| 84 | + if(e instanceof CompletionException completionException) { |
| 85 | + if(completionException.getCause() instanceof AmqpReplyTimeoutException timeoutException) { |
| 86 | + logger.error("Timeout while waiting for reply to message on channel {}: {}", request.methodName(), timeoutException.getMessage(), e); |
| 87 | + throw new ResponseStatusException(HttpStatus.GATEWAY_TIMEOUT, "Timed out while waiting for reply to message on channel " + request.methodName(), timeoutException); |
| 88 | + } |
| 89 | + else if(completionException.getCause() instanceof AmqpMessageReturnedException messageReturnedException) { |
| 90 | + logger.error("Message returned: {}", messageReturnedException.getMessage(), e); |
| 91 | + throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Message to channel " + request.methodName() + " was returned"); |
| 92 | + } |
| 93 | + } |
80 | 94 | logger.error("Error during send and receive: {}. Returning failed future with ResponseStatusException HTTP 500 Internal Server Error", e.getMessage(), e); |
81 | 95 | throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage(), e); |
| 96 | + |
82 | 97 | }) |
83 | 98 | .thenCompose(msg -> { |
84 | 99 | try { |
|
0 commit comments