SpringBoot #ExceptionHandler bubbles to Exception - spring-boot

I'm using Kotlin and SpringBoot 2.3. While testing I realized that #ExceptionHandler(MissingKotlinParameterException::class) in my controller doesn't work if I have an generic handler for uncaught exceptions with #ExceptionHandler(Exception::class).
The exception always bubbles to generic exception handler. It's not the case with other exceptions like IllegalArgumentsException which the correct handler handles.
Anyone knows what am I missing?
Thanks

Use
#ExceptionHandler(HttpMessageNotReadableException::class)
instead of
#ExceptionHandler(MissingKotlinParameterException::class)
I had the same issue but with this above I solved the problem.
It is something to do with wrapper.

Related

Difference between try catch and #ExceptionHandler in Spring REST

I was going through a video tutorial where the instructor focussed on handling any Runtime exceptions through the #ExceptionHandler in Spring.
What is the difference between the approach of handling it via try catch or via #ExceptionHandler?Which is the better approach and when to use either of them?
bro we use #ExceptionHandler above a method which we want to handle all the exception that occurs in your code while in case of try and catch you just handle the exception where it occurs only. But if we want that every exception that occurs in our program to be handled by one method, so that it will look clean and professional, we use #ExceptionHandler. Here is the link that might help you understand this.
https://howtodoinjava.com/spring-core/spring-exceptionhandler-annotation/
Let me know if this helps.

Custom Exception handling in spring boot

I have 2 filters running ,Not using controller Advice for exception handling
Catching exceptions in Filter but when I throw customized exception for ex ,UserNotAllowedException ,Filter catches NestedServletException ,Let me know If anybody has any idea on why is it happening.

Lumen Reportable & Renderable Exception doesn't work

I am trying to define a custom renderable exception exactly as shown in the documentation example but when I throw my custom exception, the render and report functions inside the exception class are totally ignored.
I am aware that I can handle my custom exceptions inside the exception handler but this is something I would prefer to do inside the exception class.
Is it something more that I should do to register the exception in order for the render and report functions to be called? What am I missing?
Thank you!
EDIT: I'm using Lumen 5.7.
Turns out that this feature is not available in Lumen framework, only in Laravel. Lumen's documentation is very misleading as it lets one to imagine that the features concerning errors are common in Laravel and Lumen.
In any case, currently in Lumen only exception handling inside the exception handler is available.
Looks like it feature will be added in one of next 5.8.* releases.
PR: https://github.com/laravel/lumen-framework/pull/958

How can I handle Tomcat's MaxUploadSizeExceededException in Spring?

I have done some research around this with conflicting results. To handle this error, some say that I need to implement HandlerExceptionResolver in one of my controllers.
Here are some links for that:
How to handle MaxUploadSizeExceededException
Handling MaxUploadSizeExceededException with Spring MVC
http://www.raistudies.com/spring/spring-mvc/file-upload-spring-mvc-annotation/
On the other hand, other people are saying that this approach is futile such that the Exception is occuring outside the request handling flow:
http://forum.spring.io/forum/spring-projects/web/124409-handling-maxuploadsizeexceededexception-in-spring (The second poster in the thread)
MaxUploadSizeExceededException doesn't invoke the exception handling method in Spring
I have tried the above solutions but they do not work for me. It appears that the Exception occurs outside of Spring, as expected. I am unable to catch this even with HandlerExceptionResolver.
Trying following the approach specified in the link below. Basically, you configure an error page for any un handled exception and then define a handler for the error page. Looks like a decent workaround.
Here is the link http://www.javacodegeeks.com/2013/11/how-to-custom-error-pages-in-tomcat-with-spring-mvc.html
Hope this helps.

How to configure spring HandlerExceptionResolver to handle NullPointerException thrown in jsp?

From a jsp is thrown a NullPointerException for example using <% null.toString(); %>
This exception is not handled by the HandlerExceptionResolver, but thrown to the web container(tomcat) and converted into a code 500 error.
How can I configure spring to get that error in my HandlerExceptionResolver ?
Details:
Spring can be configured to handle exceptions thrown inside Controllers, but not exceptions thrown by view.
Of course i can resolve the NullPointerException, but i want to design a solution that will gracefully resolve any possible problem on the web application in order to display a user friendly message to the user.
See the HandlerInterceptor interface instead. You'll want the afterCompletion method. You can then intercept the response and then set the appropriate header information to redirect to a container-configured error web page. You're right that Spring doesn't have this functionality, this is going to have to be specified by the web.xml which determines which codes map to which pages.
I have not worked with this particular bit of the spring framework, but the docs say
"Interface to be implemented by objects than can resolve exceptions thrown during handler mapping or execution, in the typical case to error views. Implementors are typically registered as beans in the application context.
Error views are analogous to the error page JSPs, but can be used with any kind of exception including any checked exception, with potentially fine-granular mappings for specific handlers."
so I'd imagine that given that NullPointer extends RuntimeException the framework isn't designed to catch it. Is there a reason the exception(s) can't be handled in the controller directly?

Resources