Skip to content

Eukolos/microservices-and-exception-handling

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Microservices and Exception Handling

This is a simple project to demonstrate how to handle exceptions with Google Error Format and expose exceptions which is thrown by other microservice.

image

image2

Exception Before Custom Exception Handler

{
  "timestamp": "2021-08-08T15:03:05.000+00:00",
  "status": 500,
  "error": "Internal Server Error",
  "path": "/api/v1/employees/1"
}

What is this Google Format Exception

{
  "apiVersion": "2.0",
  "error": {
    "code": 404,
    "message": "File Not Found",
    "errors": [{
      "domain": "Calendar",
      "reason": "ResourceNotFoundException",
      "message": "File Not Found"
    }]
  }
}

Indicates that an error has occurred, with details about the error. The error format supports either one or more errors returned from the service. A JSON response should contain either a data object or an error object, but not both. If both data and error are present, the error object takes precedence.

Other problem is text/html response

When someone try to access the endpoint with browser, it will return text/html response. If thrown exception is not handled, it will return White Label Error Page with 500 status code. This is not good for API. We need to return JSON response with correct status code. We have to customize AbstractErrorController to handle this problem.

Outputs

User Exception image3
Book Exception from User Service image4
Exception from Browser image4

References

thepracticaldeveloper baeldung - Custom Error Page bealdung - Feign Client Exception Handling

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages