[WARNING] Could not send response error 500: org.springframework.orm.hibernate4.HibernateOptimisticLockingFailureException
I'm writing exception handlers using #ControllerAdvice & #ExceptionHandler in spring. I have a generic exception handler as below:
#ControllerAdvice
public class GlobalExceptionHandler {
final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
//other ExceptionHandlers to catch specific exceptions
#ExceptionHandler(Exception.class)
#ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
#ResponseBody
public ErrorMessage handleException(Exception ex) {
//other code
logger.error("Exception in API", ex);
//other code
}
}
Why i'm not able to catch exception in 'handleException'?, why spring say it's a warning not as an error?
Update:
In DAO layer because of wrong-input for an operation, hibernate throws HibernateOptimisticLockingFailureException. Yet, why my GlobalExceptionHandler(Global Exception Class) is not able to catch this ?
Related
I'm pretty new to Spring Boot. In a project I'd like to send an email asyncronously. Below, you can see what I have so far.
A problem I have is the following: An external system sends a POST request to the controller. If some exception occurs while building or sending the mail, then the GlobalExceptionHandler does not get called. As a consequence, the controller always returns an HTTP 201, so the caller assumes that everything went fine.
How would I integrate my exception handlers with #ControllerAdvice in such async methods?
Controller
#PostMapping(value = "/mail", consumes = MediaType.APPLICATION_JSON_VALUE)
public void send(#Validated #RequestBody EmailNotificationRequest emailNotificationRequest) throws MessagingException {
emailService.sendMessage(emailNotificationRequest);
}
Service
#Async
public void sendMessage(EmailNotificationRequest emailNotificationRequest) throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
// build the message
javaMailSender.send(mimeMessage);
}
ExceptionHandler
#RestControllerAdvice
#Slf4j
public class GlobalExceptionHandler extends AbstractExceptionHandler {
/**
* Handles any exception which is not handled by a specific {#link ExceptionHandler}.
*/
#ExceptionHandler(value = {Throwable.class})
#ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ApplicationResponse handleThrowable(Throwable ex) {
log.error("An unhandled error occurred: {}", ex.getMessage());
return buildErrorResponse();
}
}
How about moving the #Async into a lower level, so only the
javaMailSender.send(mimeMessage);
Will be called in an async way?
Extract it to a different bean with a public async method that wraps the javaMailSender and remove the Async from the mothod of sendMessage
Below is my ExceptionHandler method to handle different exceptions that can be thrown from service class.
#ExceptionHandler({ExceptionA.class, ExceptionB.class, ExceptionC.class})
#ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
#ResponseBody
public void handle(ExceptionA e) {
log.error("Exception occurred: {} {}", e.getMessage(), e);
}
When ExceptionA is thrown, everything works as expected. But when ExceptionB is thrown, it gives the error No suitable resolver for argument 0 of type 'ExceptionA'. I guess its because the method argument for handle method is Exception A (still I would expect the error message should say No resolver for Exception B).
What should be the method argument for handle method so that it can handle any of the 3 exceptions?
Note: All Exceptions are created by extending from RuntimeException.
You need a common base class as parameter type, e.g RuntimeException:
#ExceptionHandler({ExceptionA.class, ExceptionB.class, ExceptionC.class})
#ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
#ResponseBody
public void handle(RuntimeException e) {
log.error(e.getMessage(), e);
}
Make your own base class extending RuntimeException
public class HandledRuntimeException extends RuntimeException{
}
and extend this class from your exception class like,
public class ExceptionA extends HandledRuntimeException{
}
and finally, in your handle method, you can change as below
#ExceptionHandler({HandledRuntimeException.class})
#ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
#ResponseBody
public void handle(HandledRuntimeException e) {
log.error("Exception occurred: {} {}", e.getMessage(), e);
}
My application is made by Spring Boot 2 webflux and thymeleaf, I want to catch all the exception and render the error to a customized error page.
I use #ControllerAdvice and #ExceptionHandler to catch exceptions and handle errors in a central place, I can only handle all the exceptions which are thrown in my controller, but I cannot catch
those mapping errors (content negotiation and HTTP mapping errors) such as UnsupportedMediaTypeStatusException.
I searched and find this is a known issue (https://github.com/spring-projects/sprienter code hereng-framework/issues/21097#issuecomment-453468295).
If I use WebMvc, there is no this kind of problem, all exceptions can be caught. My question is how to catch all the exceptions and show my own error page in webflux.
Here is the brief code:
#ControllerAdvice
#Slf4j
public class DefaultExceptionHandlers {
// works OK for my own thrown exception
#ResponseStatus(HttpStatus.FORBIDDEN)
#ExceptionHandler(value = CustomException1.class)
public Mono<String> handleCustom1Exceptions(CustomException1 e) {
return Mono.just("error1");
}
// works OK for my own thrown exception
#ResponseStatus(HttpStatus.BAD_REQUEST)
#ExceptionHandler(value = CustomException2.class)
public Mono<String> handleCustom2Exceptions(CustomException2 e) {
return Mono.just("error2);
}
// This exception handler is not called at all for the exceptions which are thrown by spring framework
#ResponseStatus(HttpStatus.FORBIDDEN)
#ExceptionHandler(value = Exception.class)
public Mono<String> handleAllExceptions(Exception e) {
return Mono.just("error3);
}
}
I'm very new to spring mvc sorry if I'm asking a basic questions, I need to maintain Global Exception Handling in my spring 4 MVC, Jersey project and return JSON response to IOS mobile app. Now by using #ControllerAdvice and #ExceptionHandler, I have created a class like below
#ControllerAdvice
public class GlobalExceptionHandlerController {
#ExceptionHandler(Exception.class)
public #ResponseBody handleException(HttpServletRequest reqException ex) {
ErrorInfo response=new ErrorInfo();
if(ex.getMessage.contains("java.io")){
response.setMessage("FileNotFound exception occur");
return response;
}else if ...
}
Please advice if above approach is correct or is there any alternative way to handle all exceptions occur in controller,service and DAO layer.
what you use is correct, all exceptions just be handled.In service or Dao layer,you just need to throw your business exception.The class you have created will catch the exception.But you should handle the exception in different ways,and define your own business exception.
here are some example codes.
#ExceptionHandler(RuntimeException.class)
#ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
#ResponseBody
public ErrorResponse handleUnexpectedServerError(RuntimeException ex) {
ex.printStackTrace();
return new ErrorResponse("012", ex.getMessage());
}
handle the business exception,the BusinessErrorException is custom exception.
#ExceptionHandler(BusinessErrorException.class)
#ResponseStatus(value = HttpStatus.BAD_REQUEST)
#ResponseBody
public ErrorResponse handleBusinessErrorException(BusinessErrorExceptionex) {
return new ErrorResponse(ex.getCode(), ex.getMessage());
}
I am developing a SOAP web service with CXF 2.3.3. I want to throw a custom exception when user has submitted wrong data. My exception class looks like this.
#WebFault(name="InvalidUserDataException", targetNamespace="http://foo.bar.com/")
#XmlAccessorType(XmlAccessType.FIELD)
public class InvalidUserDataException extends RuntimeException {
public InvalidUserDataException(){
super();
}
String validationErrors;
}
In my method Impl class I purposely throw this exception to see whether SoapFault exception is returned by CXF to client, but whatever I do to any of these classes results in same error: createNewUser has thrown exception, unwinding now.
Note that WSUserRegistration interface also declares itself that createNewUser method throws InvalidUserDataException.
public class WSUserRegistrationImpl implements WSUserRegistration {
#Autowired
IWSUserValidationProxy validationProxyImpl;
#Override
public int createNewUser(RegistrationInputProperty registrationInfo) throws InvalidUserDataException {
if (true) {
throw new InvalidUserDataException();
}
return 1;
}
}
My goal is to catch this exception from a SOAPFaultOutInterceptor and return a customized faultDetail.
How do I make CXF catch this error and return a SOAPFault object? Any ideas?
this would help you.
http://willemjiang.blogspot.com/2011/01/how-to-map-soap-fault-message-with.html