Search in sources :

Example 1 with CommonExceptionData

use of io.servicecomb.core.exception.CommonExceptionData in project java-chassis by ServiceComb.

the class ClientVerticle method send.

protected void send(Long event) {
    User user = new User();
    TestRequest request = new TestRequest();
    request.setUser(user);
    request.setIndex(idx);
    request.setData(PojoClient.buffer);
    SchemaMeta schemaMeta = config.getMicroserviceMeta().ensureFindSchemaMeta("server");
    Object[] args = new Object[] { request };
    Invocation invocation = InvocationFactory.forConsumer(config, schemaMeta, "wrapParam", args);
    InvokerUtils.reactiveInvoke(invocation, ar -> {
        if (ar.isSuccessed()) {
            User result = ar.getResult();
            if (result.getIndex() != idx) {
                System.out.printf("error result:%s, expect idx %d\n", result, idx);
            }
        } else {
            CommonExceptionData data = (CommonExceptionData) ((InvocationException) ar.getResult()).getErrorData();
            System.out.println(data.getMessage());
        }
        send(null);
    });
}
Also used : User(io.servicecomb.demo.server.User) Invocation(io.servicecomb.core.Invocation) SchemaMeta(io.servicecomb.core.definition.SchemaMeta) CommonExceptionData(io.servicecomb.core.exception.CommonExceptionData) TestRequest(io.servicecomb.demo.server.TestRequest)

Example 2 with CommonExceptionData

use of io.servicecomb.core.exception.CommonExceptionData in project java-chassis by ServiceComb.

the class ConsumerQpsFlowControlHandler method handle.

@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
    if (!Config.INSTANCE.isConsumerEnabled()) {
        invocation.next(asyncResp);
        return;
    }
    OperationMeta operationMeta = invocation.getOperationMeta();
    QpsController qpsController = qpsControllerMgr.getOrCreate(operationMeta);
    if (qpsController.isLimitNewRequest()) {
        // 429
        CommonExceptionData errorData = new CommonExceptionData("rejected by qps flowcontrol");
        asyncResp.consumerFail(new InvocationException(QpsConst.TOO_MANY_REQUESTS_STATUS, errorData));
        return;
    }
    invocation.next(asyncResp);
}
Also used : InvocationException(io.servicecomb.core.exception.InvocationException) CommonExceptionData(io.servicecomb.core.exception.CommonExceptionData) OperationMeta(io.servicecomb.core.definition.OperationMeta)

Example 3 with CommonExceptionData

use of io.servicecomb.core.exception.CommonExceptionData in project java-chassis by ServiceComb.

the class ProviderQpsFlowControlHandler method handle.

@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
    if (!Config.INSTANCE.isProviderEnabled()) {
        invocation.next(asyncResp);
        return;
    }
    String microServiceName = (String) invocation.getContext(Const.SRC_MICROSERVICE);
    if (microServiceName != null && !microServiceName.isEmpty()) {
        QpsController qpsController = qpsControllerMgr.getOrCreate(microServiceName);
        if (qpsController.isLimitNewRequest()) {
            // 429
            CommonExceptionData errorData = new CommonExceptionData("rejected by qps flowcontrol");
            asyncResp.producerFail(new InvocationException(QpsConst.TOO_MANY_REQUESTS_STATUS, errorData));
            return;
        }
    }
    invocation.next(asyncResp);
}
Also used : InvocationException(io.servicecomb.core.exception.InvocationException) CommonExceptionData(io.servicecomb.core.exception.CommonExceptionData)

Example 4 with CommonExceptionData

use of io.servicecomb.core.exception.CommonExceptionData in project java-chassis by ServiceComb.

the class TestResponse method testAr.

@Test
public void testAr() {
    ar.success(Status.ACCEPTED, 1);
    Assert.assertEquals(true, response.isSuccessed());
    Assert.assertEquals(false, response.isFailed());
    Assert.assertEquals(1, (int) response.getResult());
    Assert.assertEquals(Status.ACCEPTED.getStatusCode(), response.getStatusCode());
    Assert.assertEquals(Status.ACCEPTED.getReasonPhrase(), response.getReasonPhrase());
    Assert.assertEquals(Status.ACCEPTED, response.getStatus());
    ar.success(2);
    Assert.assertEquals(2, (int) response.getResult());
    Assert.assertEquals(Status.OK.getStatusCode(), response.getStatusCode());
    Response r = Response.succResp(3);
    ar.complete(r);
    Assert.assertEquals(r, response);
    ar.consumerFail(new Error("abc"));
    CommonExceptionData data = (CommonExceptionData) ((InvocationException) response.getResult()).getErrorData();
    Assert.assertEquals("Cse Internal Bad Request", data.getMessage());
    Assert.assertEquals(ExceptionFactory.CONSUMER_INNER_STATUS_CODE, response.getStatusCode());
    ar.fail(InvocationType.CONSUMER, new Error("abc"));
    data = (CommonExceptionData) ((InvocationException) response.getResult()).getErrorData();
    Assert.assertEquals("Cse Internal Bad Request", data.getMessage());
    Assert.assertEquals(ExceptionFactory.CONSUMER_INNER_STATUS_CODE, response.getStatusCode());
    InvocationException consumerException = new InvocationException(300, "abc", "def");
    ar.consumerFail(consumerException);
    Assert.assertEquals("def", ((InvocationException) response.getResult()).getErrorData());
    Assert.assertEquals(300, response.getStatusCode());
    ar.fail(InvocationType.CONSUMER, consumerException);
    Assert.assertEquals("def", ((InvocationException) response.getResult()).getErrorData());
    Assert.assertEquals(300, response.getStatusCode());
    ar.producerFail(new Error("abc"));
    data = (CommonExceptionData) ((InvocationException) response.getResult()).getErrorData();
    Assert.assertEquals("Cse Internal Server Error", data.getMessage());
    Assert.assertEquals(ExceptionFactory.PRODUCER_INNER_STATUS_CODE, response.getStatusCode());
    ar.fail(InvocationType.PRODUCER, new Error("abc"));
    data = (CommonExceptionData) ((InvocationException) response.getResult()).getErrorData();
    Assert.assertEquals("Cse Internal Server Error", data.getMessage());
    Assert.assertEquals(ExceptionFactory.PRODUCER_INNER_STATUS_CODE, response.getStatusCode());
    InvocationException producerException = new InvocationException(500, "abc", "def");
    ar.producerFail(producerException);
    Assert.assertEquals("def", ((InvocationException) response.getResult()).getErrorData());
    Assert.assertEquals(500, response.getStatusCode());
    ar.fail(InvocationType.PRODUCER, producerException);
    Assert.assertEquals("def", ((InvocationException) response.getResult()).getErrorData());
    Assert.assertEquals(500, response.getStatusCode());
}
Also used : InvocationException(io.servicecomb.core.exception.InvocationException) CommonExceptionData(io.servicecomb.core.exception.CommonExceptionData) Test(org.junit.Test)

Example 5 with CommonExceptionData

use of io.servicecomb.core.exception.CommonExceptionData in project java-chassis by ServiceComb.

the class TestLocator method testLocateStatic.

@Test
public void testLocateStatic() {
    MicroserviceMeta msm = new MicroserviceMeta("app:ms");
    ServicePathManager spm = new ServicePathManager(msm);
    RestOperationMeta rom = createRestOperatonMeta("GET", "abc/");
    spm.addResource(rom);
    rom = createRestOperatonMeta("POST", "abc/");
    spm.addResource(rom);
    try {
        spm.addResource(rom);
    } catch (Throwable e) {
        Assert.assertEquals("operation with url abc/, method POST is duplicated", e.getMessage());
    }
    Assert.assertEquals(1, spm.getStaticPathOperationMap().size());
    Assert.assertEquals(2, spm.getStaticPathOperationMap().get("abc/").values().size());
    try {
        spm.locateOperation("abcd", "GET");
    } catch (InvocationException e) {
        Assert.assertEquals("Not Found", ((CommonExceptionData) e.getErrorData()).getMessage());
    }
    try {
        spm.locateOperation("abc/", "PUT");
    } catch (InvocationException e) {
        Assert.assertEquals("Method Not Allowed", ((CommonExceptionData) e.getErrorData()).getMessage());
    }
    OperationLocator locator = spm.locateOperation("abc/", "GET");
    Assert.assertEquals(Collections.emptyMap(), locator.getPathVarMap());
    locator.locate(spm, "abc/", "POST");
    Assert.assertEquals(Collections.emptyMap(), locator.getPathVarMap());
}
Also used : RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) InvocationException(io.servicecomb.core.exception.InvocationException) MicroserviceMeta(io.servicecomb.core.definition.MicroserviceMeta) CommonExceptionData(io.servicecomb.core.exception.CommonExceptionData) Test(org.junit.Test)

Aggregations

CommonExceptionData (io.servicecomb.core.exception.CommonExceptionData)11 InvocationException (io.servicecomb.core.exception.InvocationException)8 Test (org.junit.Test)7 RestOperationMeta (io.servicecomb.common.rest.definition.RestOperationMeta)2 Invocation (io.servicecomb.core.Invocation)2 Response (io.servicecomb.core.Response)2 MicroserviceMeta (io.servicecomb.core.definition.MicroserviceMeta)2 OperationMeta (io.servicecomb.core.definition.OperationMeta)2 ProduceProcessor (io.servicecomb.common.rest.codec.produce.ProduceProcessor)1 RestParam (io.servicecomb.common.rest.definition.RestParam)1 AsyncResponse (io.servicecomb.core.AsyncResponse)1 Endpoint (io.servicecomb.core.Endpoint)1 SchemaMeta (io.servicecomb.core.definition.SchemaMeta)1 ProducerOperation (io.servicecomb.core.provider.producer.ProducerOperation)1 TestRequest (io.servicecomb.demo.server.TestRequest)1 User (io.servicecomb.demo.server.User)1 ResponseMeta (io.servicecomb.swagger.invocation.response.ResponseMeta)1 HttpClientResponse (io.vertx.core.http.HttpClientResponse)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1