0

I have an application developed with a microservices architecture. Each microservice is a spring-boot application that communicates with others via FeignClient interface.

Let A, a microservice (RestAPI) that calls microservice B. In normal conditions, B replies with an Object X, that is the JSON-response that A serves to client.

But, if B throws an exception, I obtain a chinese-box exception to the client like this:

{
    "timestamp": 1511965051071,
    "status": 500,
    "error": "Internal Server Error",
    "exception": "Exception",
    "message": { "\"timestamp\":1511965051052,\"status\":422,\"error\":\"Unprocessable Entity\",\"exception\":\"java.lang.MyException\",\"message\":\"Error message from B\",\"path\":\"PATH-OF-B-SERVICE\"}",
    "path": "PATH-OF-A-SERVICE"
}

In other words, MyException (status 422) is "embedded" in A Exception (status 500).

I would like to reply the client with the inner JSON, that is:

{
    "timestamp": 1511965051052,
    "status": 422,
    "error": "Unprocessable Entity",
    "exception": "java.lang.MyException",
    "message": "ErrormessagefromB",
    "path": "PATH-OF-B-SERVICE"
}

How can I do that?

1 Answers1

0

What about this solution based on the combination of a feign ErrorDecoder (extracting what you need from feign exception) and a spring @ExceptionHandler (dumps what you need in a ResponseEntity<Object>)?

I chose to translate the error payload to one that is generic to my API, but actually, you could keep the same.

ch4mp
  • 6,622
  • 6
  • 29
  • 49