From 130cc5b0362eca9d6f2cd192a6ecd68b8a347d86 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 24 Mar 2021 12:18:26 -0700 Subject: [PATCH] Chain caught InvalidEndpointRequestExceptions Update `AbstractWebMvcEndpointHandlerMapping` to chain any caught InvalidEndpointRequestExceptions so that a more complete stacktrace is available. The exception has also been updated to a `ResponseStatusException` so that the reason can be propagated. Fixes gh-25642 --- .../AbstractWebMvcEndpointHandlerMapping.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/servlet/AbstractWebMvcEndpointHandlerMapping.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/servlet/AbstractWebMvcEndpointHandlerMapping.java index 1c30155fb8b..f973877780c 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/servlet/AbstractWebMvcEndpointHandlerMapping.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/servlet/AbstractWebMvcEndpointHandlerMapping.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 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. @@ -53,9 +53,9 @@ import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.method.HandlerMethod; +import org.springframework.web.server.ResponseStatusException; import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.handler.MatchableHandlerMapping; import org.springframework.web.servlet.handler.RequestMatchResult; @@ -305,7 +305,7 @@ public abstract class AbstractWebMvcEndpointHandlerMapping extends RequestMappin return handleResult(this.operation.invoke(invocationContext), HttpMethod.resolve(request.getMethod())); } catch (InvalidEndpointRequestException ex) { - throw new BadOperationRequestException(ex.getReason()); + throw new InvalidEndpointBadRequestException(ex); } } @@ -416,11 +416,14 @@ public abstract class AbstractWebMvcEndpointHandlerMapping extends RequestMappin } - @ResponseStatus(code = HttpStatus.BAD_REQUEST) - private static class BadOperationRequestException extends RuntimeException { + /** + * Nested exception used to wrap a {@link InvalidEndpointRequestException} and provide + * a {@link HttpStatus#BAD_REQUEST} status. + */ + private static class InvalidEndpointBadRequestException extends ResponseStatusException { - BadOperationRequestException(String message) { - super(message); + InvalidEndpointBadRequestException(InvalidEndpointRequestException cause) { + super(HttpStatus.BAD_REQUEST, cause.getReason(), cause); } }