I'm working on a SpringBootApplication. In this app I have 4 micro-services using Feign to communicate between eachothers. In a Controller I have a piece of code like this below, to catch exceptions and return it to the view, in case something is wrong.
try {
patientDTO = patientProxyFeign.getPatientById(id);
noteDTOList = historyProxyFeign.getAll(id);
assessmentDTO = assessmentProxyFeign.getPatientAssessment(id);
} catch (Exception e) {
log.error("" e.getMessage());
model.addAttribute("errorMsg", e.toString());
return "error/error";
}
If there is an exception I got a message like this to the view :
feign.FeignException$NotFound: [404] during [GET] to [http://localhost:8081/patient/12000] [PatientProxyFeign#getPatientById(Integer)]: [{"timestamp":"2021-12-16T16:21:27.790 00:00","status":404,"error":"Not Found","path":"/patient/12000"}]
What I want to do, is to get only the status code and the message "not found".
Is someone got an idea how to do it ? (Search on google, but seems to be too specific. I probably don't use the right keywords.)
CodePudding user response:
You can get the status by calling e.status()
and then you can switch
-case
the status and get a message based on that. You could also build a Map
of <Integer, String>
and get the message by status. To read about FeignException more kindly visit https://github.com/OpenFeign/feign/blob/master/core/src/main/java/feign/FeignException.java
And it is strongly advisable to be specific about what you catch
:
} catch (FeignException e) {