Improved error printouts
[portal/nonrtric-controlpanel.git] / webapp-backend / src / main / java / org / oransc / portal / nonrtric / controlpanel / policyagentapi / PolicyAgentApiImpl.java
index 86eb81e..ef90b5d 100644 (file)
@@ -50,6 +50,8 @@ import org.springframework.http.HttpStatus;
 import org.springframework.http.MediaType;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Component;
+import org.springframework.web.client.HttpClientErrorException;
+import org.springframework.web.client.HttpServerErrorException;
 import org.springframework.web.client.RestTemplate;
 
 @Component("PolicyAgentApi")
@@ -110,7 +112,7 @@ public class PolicyAgentApiImpl implements PolicyAgentApi {
             }
             return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode());
         } catch (Exception e) {
-            return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+            return handleException(e);
         }
     }
 
@@ -132,13 +134,13 @@ public class PolicyAgentApiImpl implements PolicyAgentApi {
             }
             return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode());
         } catch (Exception e) {
-            return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+            return handleException(e);
         }
     }
 
     @Override
     public ResponseEntity<Object> getPolicyInstance(String id) {
-        String url = baseUrl() + "/policy?instance={id}";
+        String url = baseUrl() + "/policy?id={id}";
         Map<String, ?> uriVariables = Map.of("id", id);
 
         return this.restTemplate.getForEntity(url, Object.class, uriVariables);
@@ -147,10 +149,10 @@ public class PolicyAgentApiImpl implements PolicyAgentApi {
     @Override
     public ResponseEntity<String> putPolicy(String policyTypeIdString, String policyInstanceId, Object json,
         String ric) {
-        String url = baseUrl() + "/policy?type={type}&instance={instance}&ric={ric}&service={service}";
+        String url = baseUrl() + "/policy?type={type}&id={id}&ric={ric}&service={service}";
         Map<String, ?> uriVariables = Map.of( //
             "type", policyTypeIdString, //
-            "instance", policyInstanceId, //
+            "id", policyInstanceId, //
             "ric", ric, //
             "service", "controlpanel");
 
@@ -158,19 +160,19 @@ public class PolicyAgentApiImpl implements PolicyAgentApi {
             this.restTemplate.put(url, createJsonHttpEntity(json), uriVariables);
             return new ResponseEntity<>(HttpStatus.OK);
         } catch (Exception e) {
-            return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+            return handleException(e);
         }
     }
 
     @Override
     public ResponseEntity<String> deletePolicy(String policyInstanceId) {
-        String url = baseUrl() + "/policy?instance={instance}";
-        Map<String, ?> uriVariables = Map.of("instance", policyInstanceId);
+        String url = baseUrl() + "/policy?id={id}";
+        Map<String, ?> uriVariables = Map.of("id", policyInstanceId);
         try {
             this.restTemplate.delete(url, uriVariables);
             return new ResponseEntity<>(HttpStatus.OK);
         } catch (Exception e) {
-            return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
+            return handleException(e);
         }
 
     }
@@ -200,7 +202,7 @@ public class PolicyAgentApiImpl implements PolicyAgentApi {
             }
             return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
         } catch (Exception e) {
-            return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+            return handleException(e);
         }
     }
 
@@ -210,4 +212,15 @@ public class PolicyAgentApiImpl implements PolicyAgentApi {
         return new HttpEntity<>(content, headers);
     }
 
+    private ResponseEntity<String> handleException(Exception throwable) {
+        if (throwable instanceof HttpClientErrorException) {
+            HttpClientErrorException e = (HttpClientErrorException) throwable;
+            return new ResponseEntity<>(e.getMessage(), e.getStatusCode());
+        } else if (throwable instanceof HttpServerErrorException) {
+            HttpServerErrorException e = (HttpServerErrorException) throwable;
+            return new ResponseEntity<>(e.getResponseBodyAsString(), e.getStatusCode());
+        }
+        return new ResponseEntity<>(throwable.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+    }
+
 }