Reputation: 13
How to propagate exceptions from one microservice to another in the spring Boot application.
Ex: when we make a get rest call then we would expect some valid response. Let's say we are expecting a response of type Long. But if there is An exception thrown from the other microservice then how it should be caught and handled in calling the microservice?
Currently, we are getting a deserialization issue in the token objects.
Upvotes: 1
Views: 4601
Reputation: 12767
What we do is:
ErrorInfo
and an exception handler class. Also, we have an ErrorCode
which relates exception with status code. And an exception ServiceException
using ErrorCode
. Isolate these classes into a shared library for all services.ErrorCode
with a certain status code for it and attach it to the ServiceException
, and throw.ErrorCode
and other related fields in ServiceException
, we define an ErrorInfo
with error cause, details, messages and so on, and return ResponseEntity
with body of ErrorInfo
and status code 4xx/5xx, as per ErrorCode
.ErrorInfo
and construct the same exception. And throw it for the same exception handler in the library to return as ErrorInfo
to Front End.ErrorInfo
as a standard template when anything goes wrong. It can contain fields like message
/cause
/... so i18n can be done easily as well.Upvotes: 0
Reputation: 322
I would use @ControllerAdvice
to catch different types of exception which would occur in one microservice, and would send 5XX response code to inform other service that it was not able to process the request correctly, asit is considered a good practise to talk in response codes. PFB an example code.
@ControllerAdvice
public class MicroserviceExceptionHandler {
@ExceptionHandler({ UserNotFoundException.class, ContentNotAllowedException.class })
public final ResponseEntity handleException(Exception ex, WebRequest request) {
String errorMessage = ex.getMessage();
return ResponseEntity(HttpStatus.NOT_FOUND)
.status(HttpStatus.FORBIDDEN)
.body(errorMessage);
But if it required for your microservices to send the whole stack trace instead of just the message, then use below code to convert the stacktrace as a string and send it instead of ex.getMessage()
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String exceptionAsString = sw.toString();
Upvotes: 3
Reputation: 16505
How to propagate exception from one microservice to another in spring Boot application.
Microservices are talking to each other over the network and usually it is HTTP(s). So, at the boundaries of the microservices, the exceptions will get converted as standard HTTP error codes (for client error 4XX
, for server errors 5XX
and so on) and optional error message(s). When you invoke an upstream service, if the response is not a success (HTTP2XX
), your consumer service just need to look for the agreed up on error codes / messages and translate it into meaningful actions(meaningful for the consumer service).
I would suggest you to read https://martinfowler.com/articles/microservices.html
Upvotes: 1