wimdeblauwe / htmx-spring-boot

Spring Boot and Thymeleaf helpers for working with htmx
Apache License 2.0
423 stars 41 forks source link

Support HtmxResponse in error handlers #94

Closed wimdeblauwe closed 6 months ago

wimdeblauwe commented 7 months ago

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.