Search in sources :

Example 6 with InvocationException

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());
}
Also used : Status(javax.ws.rs.core.Response.Status) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException)

Example 7 with InvocationException

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);
}
Also used : Expectations(mockit.Expectations) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) ArrayList(java.util.ArrayList) RestParam(org.apache.servicecomb.common.rest.definition.RestParam) Test(org.junit.Test)

Example 8 with InvocationException

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);
}
Also used : InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException)

Example 9 with InvocationException

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");
}
Also used : InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData)

Example 10 with InvocationException

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());
}
Also used : InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) MockUp(mockit.MockUp) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) Test(org.junit.Test)

Aggregations

InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)24 Test (org.junit.Test)16 Response (org.apache.servicecomb.swagger.invocation.Response)8 CommonExceptionData (org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData)8 MockUp (mockit.MockUp)5 Invocation (org.apache.servicecomb.core.Invocation)5 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)5 Expectations (mockit.Expectations)3 RestParam (org.apache.servicecomb.common.rest.definition.RestParam)3 AsyncResponse (org.apache.servicecomb.swagger.invocation.AsyncResponse)3 HystrixCommandProperties (com.netflix.hystrix.HystrixCommandProperties)2 ArrayList (java.util.ArrayList)2 Holder (javax.xml.ws.Holder)2 AbstractTcpClientPackage (org.apache.servicecomb.foundation.vertx.client.tcp.AbstractTcpClientPackage)2 TcpResponseCallback (org.apache.servicecomb.foundation.vertx.client.tcp.TcpResponseCallback)2 Buffer (io.vertx.core.buffer.Buffer)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Status (javax.ws.rs.core.Response.Status)1 Mock (mockit.Mock)1 WrapSchema (org.apache.servicecomb.codec.protobuf.utils.WrapSchema)1