-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[fix] Annotations on exception handler methods do not work
- Loading branch information
Showing
5 changed files
with
389 additions
and
205 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
...ava/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxExceptionHandlerExceptionResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.github.wimdeblauwe.htmx.spring.boot.mvc; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.web.method.HandlerMethod; | ||
import org.springframework.web.servlet.ModelAndView; | ||
import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver; | ||
import org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* A custom {@link ExceptionHandlerExceptionResolver} that handles htmx annotations | ||
* present on exception handler methods. | ||
* | ||
* @since 3.6.2 | ||
*/ | ||
public class HtmxExceptionHandlerExceptionResolver extends ExceptionHandlerExceptionResolver { | ||
|
||
private final HtmxHandlerMethodAnnotationHandler handlerMethodAnnotationHandler; | ||
|
||
public HtmxExceptionHandlerExceptionResolver(HtmxHandlerMethodAnnotationHandler handlerMethodAnnotationHandler) { | ||
this.handlerMethodAnnotationHandler = handlerMethodAnnotationHandler; | ||
} | ||
|
||
@Override | ||
protected ModelAndView doResolveHandlerMethodException(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod, Exception exception) { | ||
|
||
ServletInvocableHandlerMethod exceptionHandlerMethod = getExceptionHandlerMethod(handlerMethod, exception); | ||
if (exceptionHandlerMethod != null) { | ||
Method method = exceptionHandlerMethod.getMethod(); | ||
handlerMethodAnnotationHandler.handleMethod(method, request, response); | ||
} | ||
|
||
return super.doResolveHandlerMethodException(request, response, handlerMethod, exception); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.