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")
}
return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode());
} catch (Exception e) {
- return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+ return handleException(e);
}
}
}
return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode());
} catch (Exception e) {
- return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+ return handleException(e);
}
}
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);
}
}
this.restTemplate.delete(url, uriVariables);
return new ResponseEntity<>(HttpStatus.OK);
} catch (Exception e) {
- return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
+ return handleException(e);
}
}
}
return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
} catch (Exception e) {
- return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+ return handleException(e);
}
}
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);
+ }
+
}