Fix "Missing exception attribute in ServerWebExchange" error after upgrading Spring Boot 3.3.3 (#6515)

#### What type of PR is this?

/kind bug
/area core
/milestone 2.19.0

#### What this PR does / why we need it:

After merging https://github.com/halo-dev/halo/pull/6512 and https://github.com/halo-dev/halo/pull/6511, unit tests fail due to the changes of <05b73ceeec>.

This PR fixes the problem by letting `run.halo.app.infra.exception.handlers.ProblemDetailErrorAttributes` extend  `org.springframework.boot.web.reactive.error.DefaultErrorAttributes`.

#### Does this PR introduce a user-facing change?

```release-note
None
```
pull/6514/head^2
John Niang 2024-08-26 15:49:13 +08:00 committed by GitHub
parent e5bbbb3b7b
commit d68dca931b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 2 additions and 22 deletions

View File

@ -2,24 +2,18 @@ package run.halo.app.infra.exception.handlers;
import static run.halo.app.infra.exception.Exceptions.createErrorResponse; import static run.halo.app.infra.exception.Exceptions.createErrorResponse;
import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import org.springframework.boot.web.error.ErrorAttributeOptions; import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.reactive.error.DefaultErrorAttributes; import org.springframework.boot.web.reactive.error.DefaultErrorAttributes;
import org.springframework.boot.web.reactive.error.ErrorAttributes;
import org.springframework.context.MessageSource; import org.springframework.context.MessageSource;
import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.server.ServerWebExchange;
/** /**
* See {@link DefaultErrorAttributes} for more. * See {@link DefaultErrorAttributes} for more.
* *
* @author johnn * @author johnn
*/ */
public class ProblemDetailErrorAttributes implements ErrorAttributes { public class ProblemDetailErrorAttributes extends DefaultErrorAttributes {
private static final String ERROR_INTERNAL_ATTRIBUTE =
ProblemDetailErrorAttributes.class.getName() + ".ERROR";
private final MessageSource messageSource; private final MessageSource messageSource;
@ -30,25 +24,11 @@ public class ProblemDetailErrorAttributes implements ErrorAttributes {
@Override @Override
public Map<String, Object> getErrorAttributes(ServerRequest request, public Map<String, Object> getErrorAttributes(ServerRequest request,
ErrorAttributeOptions options) { ErrorAttributeOptions options) {
final var errAttributes = new LinkedHashMap<String, Object>(); final var errAttributes = super.getErrorAttributes(request, options);
var error = getError(request); var error = getError(request);
var errorResponse = createErrorResponse(error, null, request.exchange(), messageSource); var errorResponse = createErrorResponse(error, null, request.exchange(), messageSource);
errAttributes.put("error", errorResponse.getBody()); errAttributes.put("error", errorResponse.getBody());
return errAttributes; return errAttributes;
} }
@Override
public Throwable getError(ServerRequest request) {
return (Throwable) request.attribute(ERROR_INTERNAL_ATTRIBUTE).stream()
.findFirst()
.orElseThrow(() -> new IllegalStateException(
"Missing exception attribute in ServerWebExchange"));
}
@Override
public void storeErrorInformation(Throwable error, ServerWebExchange exchange) {
exchange.getAttributes().putIfAbsent(ERROR_INTERNAL_ATTRIBUTE, error);
}
} }