Properly handle InvalidEndpointRequestException
This commit makes sure that `InvalidEndpointRequestException` is wrapped in a `ReflectionException` when invoked via JMX. Closes gh-12857
This commit is contained in:
parent
2002115637
commit
0bc7bef5e5
|
@ -93,27 +93,22 @@ public class EndpointMBean implements DynamicMBean {
|
|||
return invoke(operation, params);
|
||||
}
|
||||
|
||||
private Object invoke(JmxOperation operation, Object[] params) throws MBeanException {
|
||||
private Object invoke(JmxOperation operation, Object[] params)
|
||||
throws MBeanException, ReflectionException {
|
||||
try {
|
||||
String[] parameterNames = operation.getParameters().stream()
|
||||
.map(JmxOperationParameter::getName).toArray(String[]::new);
|
||||
Map<String, Object> arguments = getArguments(parameterNames, params);
|
||||
Object result = invokeOperation(operation, arguments);
|
||||
Object result = operation
|
||||
.invoke(new InvocationContext(SecurityContext.NONE, arguments));
|
||||
if (REACTOR_PRESENT) {
|
||||
result = ReactiveHandler.handle(result);
|
||||
}
|
||||
return this.responseMapper.mapResponse(result);
|
||||
}
|
||||
catch (InvalidEndpointRequestException ex) {
|
||||
throw new IllegalArgumentException(ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
private Object invokeOperation(JmxOperation operation,
|
||||
Map<String, Object> arguments) throws MBeanException {
|
||||
try {
|
||||
return operation.invoke(new InvocationContext(SecurityContext.NONE,
|
||||
arguments));
|
||||
throw new ReflectionException(new IllegalArgumentException(
|
||||
ex.getMessage()), ex.getMessage());
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new MBeanException(translateIfNecessary(ex), ex.getMessage());
|
||||
|
|
|
@ -30,6 +30,8 @@ import org.junit.rules.ExpectedException;
|
|||
import reactor.core.publisher.Mono;
|
||||
|
||||
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.hamcrest.CoreMatchers.instanceOf;
|
||||
|
@ -124,6 +126,23 @@ public class EndpointMBeanTests {
|
|||
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
|
||||
public void invokeWhenMonoResultShouldBlockOnMono()
|
||||
throws MBeanException, ReflectionException {
|
||||
|
|
Loading…
Reference in New Issue