Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow HtmxResponse to be used as return type in error handlers #95

Merged
merged 2 commits into from
Dec 14, 2023

Conversation

wimdeblauwe
Copy link
Owner

This PR adds an HandlerMethodReturnValueHandler implementation to handle a HtmxResponse object as a return type from an error handler. A lot of code is the same as in HtmxViewHandlerInterceptor. For that reason, I extracted HtmxResponseHelper.

@dsyer and @odrotbohm Would love you feedback on this PR. Especially doubting if the method calls I am doing in the HtmxMvcAutoConfiguration are correct given that @AutoConfiguration has proxyBeanMethods set to false, but I don't know of another way to make it work.

Fixes #94

@wimdeblauwe wimdeblauwe added this to the 3.2.0 milestone Dec 10, 2023
@dsyer
Copy link
Contributor

dsyer commented Dec 10, 2023

Agree about the internal method call. We either need to remove the proxyBeanMethods flag or ship them out to a separate @Configuration (and wire them back into the constructor).

@dsyer
Copy link
Contributor

dsyer commented Dec 10, 2023

Isn’t the interceptor mainly doing exactly the same work as the return value handler? I’m not sure it needs to be done twice. (So all the code from the helper could just be migrated to the return value handler?)

The HtmxResponseHandlerMethodReturnValueHandler alone is enough to cover the HtmxResponse return value from normal controller methods and exception handlers

Fixes #94
@wimdeblauwe
Copy link
Owner Author

Thanks for the input @dsyer. You are correct that there is no more need for the intercepter, the return value handler works for all cases. I have updated the code accordingly.

@dsyer
Copy link
Contributor

dsyer commented Dec 13, 2023

LGTM

@wimdeblauwe wimdeblauwe merged commit d2ece55 into main Dec 14, 2023
2 checks passed
@wimdeblauwe wimdeblauwe deleted the feature/gh-94 branch December 14, 2023 19:33
@wimdeblauwe
Copy link
Owner Author

This is now released and I blogged to showcase the feature: https://www.wimdeblauwe.com/blog/2023/12/14/htmx-global-error-handler/

@xhaggi
Copy link
Contributor

xhaggi commented Jan 18, 2024

@wimdeblauwe is there a way to restrict the @ExceptionHandler to htmx requests only?

@wimdeblauwe
Copy link
Owner Author

According to https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc/, you can define a HttpServletRequest parameter in your exception handler. You could read the headers there to check if it is an htmx request or not.

@xhaggi
Copy link
Contributor

xhaggi commented Jan 19, 2024

Yeah, I am aware of that. I meant an annotation like the one we have for the controller methods.

@wimdeblauwe
Copy link
Owner Author

That would be nice if that is possible, but no clue if there are extension points to implement that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support HtmxResponse in error handlers
3 participants