X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=webapp-backend%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fportal%2Fnonrtric%2Fcontrolpanel%2Fpolicyagentapi%2FPolicyAgentApiImpl.java;h=ef90b5d8d228d3bfe5b59a74a3d28236644c6481;hb=2e425bc51a5e8cb865db1e35e11ebe9240f72397;hp=c08444322f43894ee60c0675a8975cbb96a9bbd0;hpb=626cd0f862a3513f9267fed66c21bf9053b7b163;p=portal%2Fnonrtric-controlpanel.git diff --git a/webapp-backend/src/main/java/org/oransc/portal/nonrtric/controlpanel/policyagentapi/PolicyAgentApiImpl.java b/webapp-backend/src/main/java/org/oransc/portal/nonrtric/controlpanel/policyagentapi/PolicyAgentApiImpl.java index c084443..ef90b5d 100644 --- a/webapp-backend/src/main/java/org/oransc/portal/nonrtric/controlpanel/policyagentapi/PolicyAgentApiImpl.java +++ b/webapp-backend/src/main/java/org/oransc/portal/nonrtric/controlpanel/policyagentapi/PolicyAgentApiImpl.java @@ -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,7 +134,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); } } @@ -158,7 +160,7 @@ 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); } } @@ -170,7 +172,7 @@ public class PolicyAgentApiImpl implements PolicyAgentApi { 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 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); + } + }