Skip to content

Commit

Permalink
Remove ErrorAttributes.ERROR_ATTRIBUTE
Browse files Browse the repository at this point in the history
This commit removes the now defunkt `ErrorAttributes.ERROR_ATTRIBUTE`
that was introduce to register handled errors as metrics. This has been
replaced since 3.0 by a direct support in Spring Framework and had no
effect whatsoever since that release.

This also updates the documentation to point to the Framework mechanism
that replaced it.

Fixes gh-33731
  • Loading branch information
bclozel committed Dec 14, 2023
1 parent 8c5b7a8 commit e44e0c8
Show file tree
Hide file tree
Showing 12 changed files with 6 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,8 @@ include::code:MyErrorWebExceptionHandler[]

For a more complete picture, you can also subclass `DefaultErrorWebExceptionHandler` directly and override specific methods.

In some cases, errors handled at the controller or handler function level are not recorded by the <<actuator#actuator.metrics.supported.spring-webflux, metrics infrastructure>>.
Applications can ensure that such exceptions are recorded with the request metrics by setting the handled exception as a request attribute:

include::code:MyExceptionHandlingController[]
In some cases, errors handled at the controller level are not recorded by web observations or the <<actuator#actuator.metrics.supported.spring-webflux, metrics infrastructure>>.
Applications can ensure that such exceptions are recorded with the observations by {spring-framework-docs}/integration/observability.html#observability.http-server.reactive[setting the handled exception on the observation context].



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,8 @@ include::code:MyControllerAdvice[]

In the preceding example, if `MyException` is thrown by a controller defined in the same package as `SomeController`, a JSON representation of the `MyErrorBody` POJO is used instead of the `ErrorAttributes` representation.

In some cases, errors handled at the controller level are not recorded by the <<actuator#actuator.metrics.supported.spring-mvc, metrics infrastructure>>.
Applications can ensure that such exceptions are recorded with the request metrics by setting the handled exception as a request attribute:

include::code:MyController[]
In some cases, errors handled at the controller level are not recorded by web observations or the <<actuator#actuator.metrics.supported.spring-mvc, metrics infrastructure>>.
Applications can ensure that such exceptions are recorded with the observations by {spring-framework-docs}/integration/observability.html#observability.http-server.servlet[setting the handled exception on the observation context].



Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ private void handleException(Map<String, Object> errorAttributes, Throwable erro
@Override
public Throwable getError(ServerRequest request) {
Optional<Object> error = request.attribute(ERROR_INTERNAL_ATTRIBUTE);
error.ifPresent((value) -> request.attributes().putIfAbsent(ErrorAttributes.ERROR_ATTRIBUTE, value));
return (Throwable) error
.orElseThrow(() -> new IllegalStateException("Missing exception attribute in ServerWebExchange"));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,13 +34,6 @@
*/
public interface ErrorAttributes {

/**
* Name of the {@link ServerRequest#attribute(String) request attribute} holding the
* error resolved by the {@code ErrorAttributes} implementation.
* @since 2.5.0
*/
String ERROR_ATTRIBUTE = ErrorAttributes.class.getName() + ".error";

/**
* Return a {@link Map} of the error attributes. The map can be used as the model of
* an error page, or returned as a {@link ServerResponse} body.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,6 @@ public Throwable getError(WebRequest webRequest) {
if (exception == null) {
exception = getAttribute(webRequest, RequestDispatcher.ERROR_EXCEPTION);
}
// store the exception in a well-known attribute to make it available to metrics
// instrumentation.
webRequest.setAttribute(ErrorAttributes.ERROR_ATTRIBUTE, exception, WebRequest.SCOPE_REQUEST);
return exception;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,14 +34,6 @@
*/
public interface ErrorAttributes {

/**
* Name of the {@link jakarta.servlet.http.HttpServletRequest#getAttribute(String)
* request attribute} holding the error resolved by the {@code ErrorAttributes}
* implementation.
* @since 2.5.0
*/
String ERROR_ATTRIBUTE = ErrorAttributes.class.getName() + ".error";

/**
* Returns a {@link Map} of the error attributes. The map can be used as the model of
* an error page {@link ModelAndView}, or returned as a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ void includeException() {
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(serverRequest,
ErrorAttributeOptions.of(Include.EXCEPTION, Include.MESSAGE));
assertThat(this.errorAttributes.getError(serverRequest)).isSameAs(error);
assertThat(serverRequest.attribute(ErrorAttributes.ERROR_ATTRIBUTE)).containsSame(error);
assertThat(attributes).containsEntry("exception", RuntimeException.class.getName());
assertThat(attributes).containsEntry("message", "Test");
}
Expand All @@ -178,7 +177,6 @@ void processResponseStatusException() {
assertThat(attributes).containsEntry("message", "invalid request");
assertThat(attributes).containsEntry("exception", RuntimeException.class.getName());
assertThat(this.errorAttributes.getError(serverRequest)).isSameAs(error);
assertThat(serverRequest.attribute(ErrorAttributes.ERROR_ATTRIBUTE)).containsSame(error);
}

@Test
Expand All @@ -194,7 +192,6 @@ void processResponseStatusExceptionWithNoNestedCause() {
assertThat(attributes).containsEntry("message", "could not process request");
assertThat(attributes).containsEntry("exception", ResponseStatusException.class.getName());
assertThat(this.errorAttributes.getError(serverRequest)).isSameAs(error);
assertThat(serverRequest.attribute(ErrorAttributes.ERROR_ATTRIBUTE)).containsSame(error);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ void mvcError() {
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(this.webRequest,
ErrorAttributeOptions.of(Include.MESSAGE));
assertThat(this.errorAttributes.getError(this.webRequest)).isSameAs(ex);
assertThat(this.webRequest.getAttribute(ErrorAttributes.ERROR_ATTRIBUTE, WebRequest.SCOPE_REQUEST))
.isSameAs(ex);
assertThat(modelAndView).isNull();
assertThat(attributes).doesNotContainKey("exception");
assertThat(attributes).containsEntry("message", "Test");
Expand All @@ -102,8 +100,6 @@ void servletErrorWithMessage() {
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(this.webRequest,
ErrorAttributeOptions.of(Include.MESSAGE));
assertThat(this.errorAttributes.getError(this.webRequest)).isSameAs(ex);
assertThat(this.webRequest.getAttribute(ErrorAttributes.ERROR_ATTRIBUTE, WebRequest.SCOPE_REQUEST))
.isSameAs(ex);
assertThat(attributes).doesNotContainKey("exception");
assertThat(attributes).containsEntry("message", "Test");
}
Expand All @@ -115,8 +111,6 @@ void servletErrorWithoutMessage() {
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(this.webRequest,
ErrorAttributeOptions.defaults());
assertThat(this.errorAttributes.getError(this.webRequest)).isSameAs(ex);
assertThat(this.webRequest.getAttribute(ErrorAttributes.ERROR_ATTRIBUTE, WebRequest.SCOPE_REQUEST))
.isSameAs(ex);
assertThat(attributes).doesNotContainKey("exception");
assertThat(attributes).doesNotContainKey("message");
}
Expand Down Expand Up @@ -166,8 +160,6 @@ void unwrapServletException() {
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(this.webRequest,
ErrorAttributeOptions.of(Include.MESSAGE));
assertThat(this.errorAttributes.getError(this.webRequest)).isSameAs(wrapped);
assertThat(this.webRequest.getAttribute(ErrorAttributes.ERROR_ATTRIBUTE, WebRequest.SCOPE_REQUEST))
.isSameAs(wrapped);
assertThat(attributes).doesNotContainKey("exception");
assertThat(attributes).containsEntry("message", "Test");
}
Expand All @@ -179,8 +171,6 @@ void getError() {
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(this.webRequest,
ErrorAttributeOptions.of(Include.MESSAGE));
assertThat(this.errorAttributes.getError(this.webRequest)).isSameAs(error);
assertThat(this.webRequest.getAttribute(ErrorAttributes.ERROR_ATTRIBUTE, WebRequest.SCOPE_REQUEST))
.isSameAs(error);
assertThat(attributes).doesNotContainKey("exception");
assertThat(attributes).containsEntry("message", "Test error");
}
Expand Down

0 comments on commit e44e0c8

Please sign in to comment.