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

Support HtmxResponse in error handlers #94

Closed
wimdeblauwe opened this issue Nov 30, 2023 · 0 comments · Fixed by #95
Closed

Support HtmxResponse in error handlers #94

wimdeblauwe opened this issue Nov 30, 2023 · 0 comments · Fixed by #95

Comments

@wimdeblauwe
Copy link
Owner

I would love to be able to use HtmxResponse as the return type for error handlers. This would allow to do something like this:

@ExceptionHandler(Exception.class)
public HtmxResponse handleError(Exception ex) {
  return HtmxResponse.builder()
      .reswap(HtmxReswap.none())
      .view(new ModelAndView("fragments/flashmessage :: oob-flashmessage-info", Map.of("message", ex.getMessage())))
      .build();
}

The idea is that there is a <div/> on each page that allows to show a general error message via OOB swap.

As a workaround, I currently do this:

@ExceptionHandler(Exception.class)
public ModelAndView handleError(HttpServletResponse response, Exception ex) {
  response.setHeader("HX-Reswap", "none");
  return new ModelAndView("fragments/flashmessage :: oob-flashmessage-warning", Map.of("message", ex.getMessage()));
}

But it would be nicer if we could use HtmxResponse.

wimdeblauwe added a commit that referenced this issue Dec 10, 2023
The HtmxResponseHandlerMethodReturnValueHandler alone is enough to cover the HtmxResponse return value from normal controller methods and exception handlers

Fixes #94
wimdeblauwe added a commit that referenced this issue Dec 14, 2023
Allow HtmxResponse to be used as return type in error handlers
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 a pull request may close this issue.

1 participant