Properly handle InvalidEndpointRequestException

This commit makes sure that `InvalidEndpointRequestException` is wrapped
in a `ReflectionException` when invoked via JMX.

Closes gh-12857
pull/12890/head
Stephane Nicoll 7 years ago
parent 2002115637
commit 0bc7bef5e5

@ -93,27 +93,22 @@ public class EndpointMBean implements DynamicMBean {
return invoke(operation, params); return invoke(operation, params);
} }
private Object invoke(JmxOperation operation, Object[] params) throws MBeanException { private Object invoke(JmxOperation operation, Object[] params)
throws MBeanException, ReflectionException {
try { try {
String[] parameterNames = operation.getParameters().stream() String[] parameterNames = operation.getParameters().stream()
.map(JmxOperationParameter::getName).toArray(String[]::new); .map(JmxOperationParameter::getName).toArray(String[]::new);
Map<String, Object> arguments = getArguments(parameterNames, params); Map<String, Object> arguments = getArguments(parameterNames, params);
Object result = invokeOperation(operation, arguments); Object result = operation
.invoke(new InvocationContext(SecurityContext.NONE, arguments));
if (REACTOR_PRESENT) { if (REACTOR_PRESENT) {
result = ReactiveHandler.handle(result); result = ReactiveHandler.handle(result);
} }
return this.responseMapper.mapResponse(result); return this.responseMapper.mapResponse(result);
} }
catch (InvalidEndpointRequestException ex) { catch (InvalidEndpointRequestException ex) {
throw new IllegalArgumentException(ex.getMessage(), ex); throw new ReflectionException(new IllegalArgumentException(
} ex.getMessage()), ex.getMessage());
}
private Object invokeOperation(JmxOperation operation,
Map<String, Object> arguments) throws MBeanException {
try {
return operation.invoke(new InvocationContext(SecurityContext.NONE,
arguments));
} }
catch (Exception ex) { catch (Exception ex) {
throw new MBeanException(translateIfNecessary(ex), ex.getMessage()); throw new MBeanException(translateIfNecessary(ex), ex.getMessage());

@ -30,6 +30,8 @@ import org.junit.rules.ExpectedException;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import org.springframework.beans.FatalBeanException; import org.springframework.beans.FatalBeanException;
import org.springframework.boot.actuate.endpoint.InvalidEndpointRequestException;
import org.springframework.boot.actuate.endpoint.InvocationContext;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.instanceOf;
@ -124,6 +126,23 @@ public class EndpointMBeanTests {
bean.invoke("missingOperation", NO_PARAMS, NO_SIGNATURE); bean.invoke("missingOperation", NO_PARAMS, NO_SIGNATURE);
} }
@Test
public void invokeWhenOperationIsInvalidShouldThrowException()
throws MBeanException, ReflectionException {
TestJmxOperation operation = new TestJmxOperation() {
@Override
public Object invoke(InvocationContext context) {
throw new InvalidEndpointRequestException("test failure", "test");
}
};
TestExposableJmxEndpoint endpoint = new TestExposableJmxEndpoint(operation);
EndpointMBean bean = new EndpointMBean(this.responseMapper, endpoint);
this.thrown.expect(ReflectionException.class);
this.thrown.expectCause(instanceOf(IllegalArgumentException.class));
this.thrown.expectMessage("test failure");
bean.invoke("testOperation", NO_PARAMS, NO_SIGNATURE);
}
@Test @Test
public void invokeWhenMonoResultShouldBlockOnMono() public void invokeWhenMonoResultShouldBlockOnMono()
throws MBeanException, ReflectionException { throws MBeanException, ReflectionException {

Loading…
Cancel
Save