Repository: iluwatar/java-design-patterns
Issue: "Aggregator microservices"  design pattern doesn't show exception handling 
Labels: type: enhancement
info: good first issue
epic: code quality

"Aggregator microservices"  design pattern doesn't show how exception handling would be done if the one or multiple services used are down and how client calling the aggregator microservice would cope in this situation.

Issue comments:
Can't we consider 

`    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
      HttpGet httpGet = new HttpGet("http://localhost:51515/information");
      try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
        response = EntityUtils.toString(httpResponse.getEntity());
      }
    } catch (IOException e) {
      LOGGER.error("Exception caught.", e);
    }`

in com.iluwatar.aggregator.microservices.ProductInformationClientImpl as example of fallback strategy ([https://blogs.oracle.com/developers/getting-started-with-microservices-part-three](url))?
Or do i not understand the task?
Has this issue been resolved?
@ddunig2 I don't see a PR against this. If you want to resolve, you can go ahead.
Hi, I am raising a PR for this, where ClientProtocolException is caught and  the error log statements are updated.
https://github.com/iluwatar/java-design-patterns/pull/958
Exception handling was improved, but the original issue still remains unsolved
Under investigation by @jarpit96 
@iluwatar @ddunig2 To implement fallback pattern in Aggregator Service, is returning default literals/constants sufficient ?
I would probably display an error message to the user in case one of the microservices does not respond or some other fault occurs
#971
I have added fall backs in Aggregator Service when other microservices fail. Default error messages (Product Title and Inventory) are returned to the user.

Pull request review comments:
`ProductInventoryClientImpl` does not return null but 0 when exception occurs. Perhaps returning null would be the correct way, so the change should be implemented in the client..

Patch that resolved the issue:
diff --git a/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/Aggregator.java b/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/Aggregator.java
index e96ac9d43c2c..dbed50fc6610 100644
--- a/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/Aggregator.java
+++ b/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/Aggregator.java
@@ -51,9 +51,23 @@ public class Aggregator {
    */
   @RequestMapping(path = "/product", method = RequestMethod.GET)
   public Product getProduct() {
+
     var product = new Product();
-    product.setTitle(informationClient.getProductTitle());
-    product.setProductInventories(inventoryClient.getProductInventories());
+    String productTitle = informationClient.getProductTitle();
+    Integer productInventory = inventoryClient.getProductInventories();
+
+    if (productTitle != null) {
+      product.setTitle(productTitle);
+    } else {
+      product.setTitle("Error: Fetching Product Title Failed"); //Fallback to error message
+    }
+
+    if (productInventory != null) {
+      product.setProductInventories(productInventory);
+    } else {
+      product.setProductInventories(-1); //Fallback to default error inventory
+    }
+
     return product;
   }
 
diff --git a/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInventoryClient.java b/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInventoryClient.java
index ed325be0070f..22369350a203 100644
--- a/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInventoryClient.java
+++ b/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInventoryClient.java
@@ -28,5 +28,5 @@
  */
 public interface ProductInventoryClient {
 
-  int getProductInventories();
+  Integer getProductInventories();
 }
diff --git a/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInventoryClientImpl.java b/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInventoryClientImpl.java
index c43fe84c61ca..fcd824de7aca 100644
--- a/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInventoryClientImpl.java
+++ b/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInventoryClientImpl.java
@@ -42,8 +42,8 @@ public class ProductInventoryClientImpl implements ProductInventoryClient {
   private static final Logger LOGGER = LoggerFactory.getLogger(ProductInventoryClientImpl.class);
 
   @Override
-  public int getProductInventories() {
-    var response = "0";
+  public Integer getProductInventories() {
+    var response = "";
 
     var request = HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:51516/inventories")).build();
     var client = HttpClient.newHttpClient();
@@ -55,6 +55,10 @@ public int getProductInventories() {
     } catch (InterruptedException ie) {
       LOGGER.error("InterruptedException Occurred", ie);
     }
-    return Integer.parseInt(response);
+    if("".equalsIgnoreCase(response)) {
+        return null;
+    } else {
+        return Integer.parseInt(response);
+    }
   }
 }


Test patch: