I just updated to latest springboot 3.2.1 and now I have compiler errors.

cannot find symbol 1440385 [ERROR] symbol: variable ERROR_ATTRIBUTE 1440385 [ERROR] location: interface org.springframework.boot.web.servlet.error.ErrorAttributes

I'm using this in my error handler:

@Slf4j
@RestControllerAdvice
public class GlobalDefaultExceptionHandler extends ResponseEntityExceptionHandler {

  @ExceptionHandler(Throwable.class)
  void defaultErrorHandler(final Throwable e, final HttpServletRequest request, final HttpServletResponse response) throws IOException {
    log.error("An unchecked error '{}' happens with message '{}' for method '{}', uri '{}', headers '{}'",
        e.getClass(), e.getMessage(), request.getMethod(), request.getRequestURI(), new ServletServerHttpRequest(request).getHeaders(), e);
    if (!response.isCommitted()) {
      response.sendError(HttpStatus.BAD_REQUEST.value(), collectMessages(e));
    }
    request.setAttribute(ErrorAttributes.ERROR_ATTRIBUTE, e);
    //  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  }

  static String collectMessages(final Throwable e) {
    return appendMessages(e, new StringBuilder(128), 2).toString();
  }

  static StringBuilder appendMessages(@Nullable final Throwable e, final StringBuilder sb, final int goDeeperCount) {
    if (goDeeperCount <= 0 || e == null) {
      return sb;
    }
    if (e instanceof IllegalStateException || e instanceof IllegalArgumentException) {
      if (sb.length() > 0) {
        sb.append(" - caused by -> ");
      }
      sb.append(e.getMessage());
    }
    return appendMessages(e.getCause(), sb, goDeeperCount - 1);
  }

  @ExceptionHandler(ConstraintViolationException.class)
  void handleConstraintViolationException(final ConstraintViolationException e, final HttpServletRequest request,
      final HttpServletResponse response) throws IOException {
    log.error("An api validation error happens '{}'", e.getMessage(), e);
    response.sendError(HttpStatus.BAD_REQUEST.value(), e.getMessage());
    request.setAttribute(ErrorAttributes.ERROR_ATTRIBUTE, e);
    //  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  }

  @ExceptionHandler(TooManyConfigurationsException.class)
  void handleTooManyConfigurationsException(final TooManyConfigurationsException e, final HttpServletRequest request,
      final HttpServletResponse response)
      throws IOException {
    log.error("Limit reached", e);
    response.sendError(HttpStatus.TOO_MANY_REQUESTS.value());
    response.setHeader("X-RateLimit-Limit", Integer.toString(e.getMaxConfigurations()));
    request.setAttribute(ErrorAttributes.ERROR_ATTRIBUTE, e);
    //  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  }

}

May this code is obsolete or wrong ... but anyway I think such a breaking change could not be part of a patch release.

Kind regards Andreas

Comment From: quaff

It's removed by https://github.com/spring-projects/spring-boot/commit/e44e0c8f1ed10e0cfcd839fa382ff0483831fff4, you should refer to the commit message.

Comment From: ahoehma

Okay. Does this mean I don't need (anymore) to handle this request-attribute by myself?

Comment From: quaff

Okay. Does this mean I don't need (anymore) to handle this request-attribute by myself?

I think so.

Comment From: bclozel

@ahoehma just to be clear, you were using this for which purpose? To record errors in observations/metrics?

Comment From: ahoehma

@bclozel I added this line of code some time ago ... maybe with the wrong understanding ... for the purpose of having the exception object ready to return to the caller of my rest-api. As far as I remember was "metrics" not the main focus.

Comment From: bclozel

I think that what you're describing is an invalid usage. Setting this after the sendError call had no effect as far as I understand. This was only used by Spring Boot error handling and the metrics support in the past. Now our reference documentation points to the expected way to record handled exceptions as errors in observations.

I'm closing this issue as a result, sorry for the inconvenience we should have removed this a while ago.

Comment From: ahoehma

Thank you for the clarification.

Comment From: bclozel

If you want to customize the error handling, there are several ways to go about it:

  • you can fully handle the exception in an @ExceptionHandler method like you do
  • you can re-throw another exception and let Spring Boot handle it
  • you can customize the error attributes and the Spring Boot error handling
  • you can use the new problem details, which I think is easier and provides more control over the returned response. We will improve this support in future versions and it's likely to replace the current mechanisms