use of org.apache.servicecomb.swagger.invocation.exception.InvocationException in project incubator-servicecomb-java-chassis by apache.
the class OperationLocator method locate.
// 先在静态路径operation list中查找;如果找不到,则在动态路径operation list中查找
public void locate(String microserviceName, String path, String httpMethod, MicroservicePaths microservicePaths) {
// 在静态路径中查找
operation = locateStaticPathOperation(path, httpMethod, microservicePaths.getStaticPathOperationMap());
if (operation != null) {
// 全部定位完成
return;
}
// 在动态路径中查找
operation = locateDynamicPathOperation(path, microservicePaths.getDynamicPathOperationList(), httpMethod);
if (operation != null) {
return;
}
Status status = Status.NOT_FOUND;
if (resourceFound) {
status = Status.METHOD_NOT_ALLOWED;
}
LOGGER.error("locate path failed, status:{}, http method:{}, path:{}, microserviceName:{}", status, httpMethod, path, microserviceName);
throw new InvocationException(status, status.getReasonPhrase());
}
use of org.apache.servicecomb.swagger.invocation.exception.InvocationException in project incubator-servicecomb-java-chassis by apache.
the class TestRestCodec method testRestToArgsInstanceExcetpion.
@Test
public void testRestToArgsInstanceExcetpion(@Mocked HttpServletRequest request, @Mocked RestOperationMeta restOperation, @Mocked RestParam restParam, @Mocked ParamValueProcessor processer) throws Exception {
List<RestParam> params = new ArrayList<>();
params.add(restParam);
InvocationException exception = new InvocationException(Status.BAD_REQUEST, "Parameter is not valid.");
new Expectations() {
{
restOperation.getParamList();
result = params;
restParam.getParamProcessor();
result = processer;
processer.getValue(request);
result = exception;
}
};
boolean success = false;
try {
RestCodec.restToArgs(request, restOperation);
success = true;
} catch (InvocationException e) {
Assert.assertEquals(e.getStatusCode(), Status.BAD_REQUEST.getStatusCode());
}
Assert.assertEquals(success, false);
}
use of org.apache.servicecomb.swagger.invocation.exception.InvocationException in project incubator-servicecomb-java-chassis by apache.
the class AuthHandler method handle.
@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
if (!auth.auth("")) {
asyncResp.consumerFail(new InvocationException(Status.UNAUTHORIZED, (Object) "auth failed"));
return;
}
LOGGER.debug("auth success.");
invocation.next(asyncResp);
}
use of org.apache.servicecomb.swagger.invocation.exception.InvocationException in project incubator-servicecomb-java-chassis by apache.
the class AbortFault method injectFault.
@Override
public void injectFault(Invocation invocation, FaultParam faultParam, AsyncResponse asynResponse) {
// get the config values related to abort.
int abortPercent = FaultInjectionUtil.getFaultInjectionConfig(invocation, "abort.percent");
if (abortPercent == FaultInjectionConst.FAULT_INJECTION_DEFAULT_VALUE) {
LOGGER.debug("Fault injection: Abort percentage is not configured");
asynResponse.success("success");
return;
}
// check fault abort condition.
boolean isAbort = FaultInjectionUtil.isFaultNeedToInject(faultParam.getReqCount(), abortPercent);
if (isAbort) {
// get the config values related to abort percentage.
int errorCode = FaultInjectionUtil.getFaultInjectionConfig(invocation, "abort.httpStatus");
if (errorCode == FaultInjectionConst.FAULT_INJECTION_DEFAULT_VALUE) {
LOGGER.debug("Fault injection: Abort error code is not configured");
asynResponse.success("success");
return;
}
// if request need to be abort then return failure with given error code
CommonExceptionData errorData = new CommonExceptionData("aborted by fault inject");
asynResponse.consumerFail(new InvocationException(errorCode, "aborted by fault inject", errorData));
return;
}
asynResponse.success("success");
}
use of org.apache.servicecomb.swagger.invocation.exception.InvocationException in project incubator-servicecomb-java-chassis by apache.
the class TestProviderQpsFlowControlHandler method testHandle.
@Test
public void testHandle() throws Exception {
Mockito.when(invocation.getContext(Const.SRC_MICROSERVICE)).thenReturn("test");
OperationMeta mockOperationMeta = QpsControllerManagerTest.getMockOperationMeta("pojo", "server", "opr");
Mockito.when(invocation.getOperationMeta()).thenReturn(mockOperationMeta);
new MockUp<QpsController>() {
@Mock
public boolean isLimitNewRequest() {
return true;
}
};
new MockUp<QpsControllerManager>() {
@Mock
protected QpsController create(String qualifiedNameKey) {
return new QpsController(qualifiedNameKey, 12);
}
};
handler.handle(invocation, asyncResp);
ArgumentCaptor<InvocationException> captor = ArgumentCaptor.forClass(InvocationException.class);
Mockito.verify(asyncResp).producerFail(captor.capture());
InvocationException invocationException = captor.getValue();
assertEquals(QpsConst.TOO_MANY_REQUESTS_STATUS, invocationException.getStatus());
assertEquals("rejected by qps flowcontrol", ((CommonExceptionData) invocationException.getErrorData()).getMessage());
}
Aggregations