How to catch a lot of Jackson Exceptions in Spring? - spring-mvc

I have a Spring Boot REST application that uses ControllerAdvice and ExceptionHandlers. I'm using Jackson as my serialization/deserialization. I'm using PostMan as my client and when I send in different errors such as invalid inputs, bad JSON syntax etc... Jackson throws certain exceptions. Currently, I have an (1) ExceptionHandler that explicitly states each type of exception such as MismatchedInputException, InvalidFormatException, InvalidDentinitionException...these are all forms of JsonProcsessingException.
Is there a way to just catch JsonProcessingException and all its children? I return different messages/status codes depending on the types of exceptions. So if exception related to serialization is thrown I want a certain error message sent back.

You should create this method in #ControllerAdvice class and verify what exceptions you want to manage in order to return different messages/status codes .
#ExceptionHandler({InvalidFormatException.class, MismatchedInputException.class})
public void handlerIllegalArgumentException(JsonProcessingException exception,
ServletWebRequest webRequest) throws IOException {
if(exception instanceof InvalidFormatException) {
LOGGER.error(exception.getMessage(), exception);
webRequest.getResponse().sendError(HttpStatus.CONFLICT.value(), exception.getMessage());
} else if (exception instanceof MismatchedInputException) {
LOGGER.error(exception.getMessage(), exception);
webRequest.getResponse().sendError(HttpStatus.BAD_REQUEST.value(), exception.getMessage());
}
}

Related

How to handle Internal server error (500) on spring rest API to custom the message?

I am working on spring rest api and I would like to sure everything is working fine. I would like to log abnormal behaviors database connection error among others, I'm working with couchbase database and I'm getting in the endpoint response for example for this kind of exception: CouchbaseQueryExecutionException the next message: Unable to execute query due to the following n1ql errors: \n{\"msg\":\"No index available on keyspace kids_club that matches your query. Use CREATE INDEX or CREATE PRIMARY INDEX to create an index, or check that your expected index is online.\",\"code\":4000} and a very long trace.
For this i found a solution on internet that is extend ResponseEntityExceptionHandler and override handleExceptionInternal method like this:
#ControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler {
private static String DEFAULT_VALIDATION_ERROR_CODE = "KC-0020";
#ExceptionHandler(MiddlewareException.class)
protected ResponseEntity<ResponseBody> handleKidsClubException(MiddlewareException ex) {
return buildErrorResponse(HttpStatus.valueOf(ex.getHttpStatus()), ex.toError());
}
#ExceptionHandler(ServiceUnavailableException.class)
protected ResponseEntity<ResponseBody> handleServiceUnavailable(ServiceUnavailableException ex) {
return buildErrorResponse(INTERNAL_SERVER_ERROR, ex);
}
#ExceptionHandler(NoSuchElementException.class)
protected ResponseEntity<ResponseBody> handleNoFoundElement(NoSuchElementException ex) {
return buildErrorResponse(NOT_FOUND, ex);
}
#ExceptionHandler(CouchbaseQueryExecutionException.class)
protected ResponseEntity<ResponseBody> handleCouchbaseQueryException(ConstraintViolationException ex) {
return buildErrorResponse(BAD_REQUEST, ex);
}
}
But I'm not able to catch any kind of Internal Server Error in this way.
It seems like spring is handle and building the final message to the user.
Any ideas to resolve this?
Thanks.
#ExceptionHandler(NullPointerException.class)
public final ResponseEntity<Object> handleNullPointerException(NullPointerException ex, WebRequest request) {
LOGGER.info("Entering into the handleAllException method");
System.out.println("Exception is : " + ex.getClass());
ResponseData error = new ResponseData();
error.setRespCode(HttpStatus.NOT_FOUND.toString());
error.setRespMessage(ex.getLocalizedMessage());
error.setTimestamp(LocalDateTime.now());
return new ResponseEntity(error, HttpStatus.INTERNAL_SERVER_ERROR);
}
please try this way below to check whether you are able to catch exception or not.. From the sysout you will get the exact exception. Then you can use that exception to catch any particular exception from that business logic..

Exception Handling in DAO layer of Spring Project with JPA

I am using Spring & JPA with Hiberate as vendor.
In DAO layer I am trying to persist entity. If duplicate exists, it throws ConstraintViolationException of Hibernate. I have written try-catch in DAO for catching the exception but it does't go to catch block at all and throws Exception to service layer.
Does JPA allow to catch JDBCException in DAO layer or it'll directly throw it to upper layer?
My code looks like as follows :
public void saveEntity(SomeEntity entity) throws CustomException {
try {
... do something...
entityManager.persist(entity);
}catch(Exception e) {
throw new CustomException(e.getMessage());
}
}
Here If something happens before persist it catches but if something goes wrong while persisting and JDBCException comes it doesn't.
I know that I could have avoided this situation by checking first that if record exists and if not, only then I'll save. But I want to know why JDBCException (or any database related exceptions) exceptions are not getting caught here.
Any help appreciated.
I have found the reason for this.
The catch block in the DAO class is not able to catch exception because transaction commit is happening after method getting executed and so the method on which I have put #Transactional (service layer method) is catching the exception instead.
Here if I use flush() right after persist, it tries to commit right there and throws Exception which will get caught.
public void saveEntity(SomeEntity entity) throws CustomException {
try {
... do something...
entityManager.persist(entity);
entityManager.flush(); //tries to commit here & throws ConstraintViolationException if already exists
}catch(Exception e) {
throw new CustomException(e.getMessage());
}
}

Spring MVC #ExceptionHandler chaining

Is there a way to chain #ExceptionHandler with exception throwing?
I'm trying to wrap exceptions throw in request handler methods before sending a response:
#ExceptionHandler(value = JsonParseException.class)
public void handleJsonParseException(JsonParseException e) throws BadRequestException {
throw new BadRequestException(e);
}
#ResponseStatus(value = HttpStatus.BAD_REQUEST)
#ExceptionHandler(value = BadRequestException.class)
public #ResponseBody RestResponse handleBadRequest(BadRequestException e) {
log.info("Bad REST Request", e);
return constructErrorResponse(e);
}
So, I'd like to for the handleBadRequest() to catch BadRequestException re-thrown from handleJsonParseException().
With the code above, handleJsonParseException() causes server to return default error page instead of RestResponse.
Is this possible or will I have to put exception wrapping in request handlers' try - catch?
Is not possible:
Chained Annotated Exception Handling - #ExceptionHandler (SPR-13726)

Error response in json format for Spring interceptor

I am writing a REST based web service. I need to return all the responses as JSON format. I have an interceptor to validate my authentication parameters. On authentication failure scenario, I have to return the error response in JSON format.
Currently i am doing
response.setHeader("Content-Type","application/json");
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "{\"error\":\"Missing Authentication Parameters\"}");
The response body is coming as below.
JBoss Web/2.1.3.GA - Error report HTTP Status 401 - {"error":"Missing Authentication Parameters"}type Status reportmessage {"error":"Missing Authentication Parameters"}description This request requires HTTP authentication ({"error":"Missing Authentication Parameters"}).JBoss Web/2.1.3.GA
I need just the JSON string in response. Please help me.
You should probably be using spring-security for this. If you want to do it by hand, an alternative to using sendError on the response is to use spring MVC's #ExceptionHandler along with content negotiation to return JSON.
First define an error class*:
public class Error {
public message;
public exception;
public Error(String message, Exception ex) {
this.message = message;
this.exception = ex;
}
}
And an exception:
public class NotAuthenticatedException extends Exception {
// ...
}
Then in your controller you throw an exception at the appropriate time, catch it with #ExceptionHandler and return a ResponseEntity containing an Error instance and the appropriate error code.
#Controller
public class SimpleController {
#RequestMapping(...)
public String aMethod() {
// ...
throw new NotAuthenticatedException("Missing Authentication Parameters");
}
#ExceptionHandler(NotAuthenticatedException.class)
public ResponseEntity<Error> handleNotAuthenticatedException(
NotAuthenticatedException ex,
HttpServletRequest request) {
return new ResponseEntity<Error>(
new Error(ex.getMessage(), ex),
HttpStatus.UNAUTHORIZED
);
}
}
*use getters/setters to please the java convention gods

Returning an error and message from a Spring controller or service

I'm migrating some servlets over to the Spring framework, using Spring MVC. Currently in each servlet we authenticate the user and if the authentication fails we do this:
if (authfailed)
{
response.sendError(HttpServletResponse.SC_UNAUTHORIZED,
"You are not authorized.");
return;
}
On the front end is a YUI-based application, and when an error status is returned the "failure" callback displays a dialog with the error message given above.
I know in my controller I can get the response object and call sendError, but is that the best way to handle this? sendError also throws an IOException so I'd have to catch that - a bit of annoying code to insert in every method of every controller.
I have the same problem handling exceptions - the servlets have try-catch blocks that call sendError in the catch method. I know I can mark my exception handlers with
#ExceptionHandler
#ResponseStatus(value = HttpStatus.NOT_FOUND)
but doesn't the exception handling class need to be in each controller class?
Finally, if the exception happens in a service called from a controller, does the exception bubble up to the controller or should I handle the exception in the service (thus pushing these exception handling issues into the service layer)?
This seems more difficult than it should be, but as with many things in Spring it's likely I don't understand what's going on. All I want to do is to send an error status and message back in the response!
Thanks,
Paul
It looks like you have the most of the answers in your question itself :)
To reiterate,
Have the controller like this
#RequestMapping("/test")
public String verifyAuth(HttpServletRequest request) throws NotFoundException {
String id = request.getParameter("id");
if (id == null)
throw new NotFoundException("Id not found in the request");
return "success";
}
Declare the exception class in NotFoundException.java,
#ResponseStatus(value=HttpStatus.NOT_FOUND, reason="Id Not Found")
public class NotFoundException extends Exception {
public NotFoundException(String msg) {
super(msg);
}
}
This exception class need not be every controller class. Declare it as public class and import it in every required controller.
This is one way of doing it. If you like the non-spring style, declare HttpServletResponse in every controller arguments and do
#RequestMapping("/test")
public String verifyAuth(HttpServletRequest request, HttpServletResponse response) {
...
try {
response.sendError(..)
catch(..) {}
}
Or you can use views to show error message,
#RequestMapping("/test")
public String verifyAuth(HttpServletRequest request, Map<String, Object> map){
String id = request.getParameter("id");
if (id == null) {
map.put("status", HttpStatus.NOTFOUND);
map.put("reason", "Id Not Found");
return "error"
}
return "success";
}
Make sure your viewResolver is configured correctly and in the error.jsp to get the error string, you could say.
<body>
${status} ${reason}
</body>
Define error.jsp with nice css for all kind of errors you would expect.
These are not the only ways. With spring you have freedom to do anything. I have seen few ppl rendering json object for error message.
To answer your another question of if the error happens in the service called by the controller is depend on your scenario. For example you are trying to read the user store, if the user store not available error happens, I would handle there itself to read from another replica user store if one available and If I found user does not exist I would leave the exception to the controller to throw.

Resources