From d18292e9f48ad04686f7d7cbf410879398cdf418 Mon Sep 17 00:00:00 2001
From: Michal Badin <xbadin@fi.muni.cz>
Date: Sat, 6 May 2023 10:29:20 +0200
Subject: [PATCH] refactoring: Changed catching exception

---
 .../java/cz/muni/pa165/service/CarComponentService.java  | 9 ++++-----
 core/src/main/java/cz/muni/pa165/service/CarService.java | 3 ++-
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/core/src/main/java/cz/muni/pa165/service/CarComponentService.java b/core/src/main/java/cz/muni/pa165/service/CarComponentService.java
index 206402e..821934f 100644
--- a/core/src/main/java/cz/muni/pa165/service/CarComponentService.java
+++ b/core/src/main/java/cz/muni/pa165/service/CarComponentService.java
@@ -8,11 +8,13 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.ParameterizedTypeReference;
+import org.springframework.core.io.Resource;
 import org.springframework.http.MediaType;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.client.*;
 import org.springframework.web.reactive.function.client.WebClient;
+import org.springframework.web.reactive.function.client.WebClientException;
 
 import java.util.*;
 
@@ -39,7 +41,7 @@ public class CarComponentService extends DomainService<CarComponent> {
         CarComponent savedComponent = repository.save(component);
         try {
             sendPostRequest(savedComponent);
-        } catch (RestClientException | IllegalArgumentException e) {
+        } catch (WebClientException e) {
             log.debug(String.format("The notification module is not reachable on the URL: %s, exception %s", NOTIFICATION_MODULE_URL + NOTIFICATION_MODULE_URI, e));
         }
         return savedComponent;
@@ -79,10 +81,7 @@ public class CarComponentService extends DomainService<CarComponent> {
                 .contentType(MediaType.APPLICATION_JSON)
                 .bodyValue(notification)
                 .retrieve()
-                .bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {})
-                .doOnError(error -> {
-                    throw new RestClientException("Failed to send POST request", error);
-                })
+                .bodyToMono(Resource.class)
                 .block();
     }
 }
diff --git a/core/src/main/java/cz/muni/pa165/service/CarService.java b/core/src/main/java/cz/muni/pa165/service/CarService.java
index 085b116..b131233 100644
--- a/core/src/main/java/cz/muni/pa165/service/CarService.java
+++ b/core/src/main/java/cz/muni/pa165/service/CarService.java
@@ -20,6 +20,7 @@ import org.springframework.transaction.annotation.Transactional;
 import org.springframework.core.io.Resource;
 import org.springframework.web.client.RestClientException;
 import org.springframework.web.reactive.function.client.WebClient;
+import org.springframework.web.reactive.function.client.WebClientException;
 
 import java.io.IOException;
 import java.util.HashMap;
@@ -113,7 +114,7 @@ public class CarService extends DomainService<Car> {
 
         try {
             sendPostRequest(savedCar);
-        } catch (Exception e) {
+        } catch (WebClientException e) {
             log.debug(String.format("The visualization module is not reachable on the URL: %s, exception %s", VISUALIZATION_MODULE_URL + VISUALIZATION_MODULE_URI, e));
         }
 
-- 
GitLab