Search in sources :

Example 6 with InvocationException

use of io.servicecomb.core.exception.InvocationException 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 7 with InvocationException

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

the class HighwayServerInvoke method sendResponse.

private void sendResponse(Map<String, String> context, Response response) {
    ResponseHeader header = new ResponseHeader();
    header.setStatusCode(response.getStatusCode());
    header.setReasonPhrase(response.getReasonPhrase());
    header.setContext(context);
    header.setHeaders(response.getHeaders());
    WrapSchema bodySchema = operationProtobuf.findResponseSchema(response.getStatusCode());
    Object body = response.getResult();
    if (response.isFailed()) {
        body = ((InvocationException) body).getErrorData();
    }
    try {
        Buffer respBuffer = HighwayCodec.encodeResponse(msgId, header, bodySchema, body);
        netSocket.write(respBuffer);
    } catch (Exception e) {
        // 没招了,直接打日志
        String msg = String.format("encode response failed, %s, msgId=%d", operationProtobuf.getOperationMeta().getMicroserviceQualifiedName(), msgId);
        LOGGER.error(msg, e);
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ResponseHeader(io.servicecomb.transport.highway.message.ResponseHeader) WrapSchema(io.servicecomb.codec.protobuf.utils.WrapSchema) InvocationException(io.servicecomb.core.exception.InvocationException)

Example 8 with InvocationException

use of io.servicecomb.core.exception.InvocationException 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)

Example 9 with InvocationException

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

the class TestConfig method testResponse.

@Test
public void testResponse() {
    Response response = Response.create(400, "test", null);
    InvocationException exception = response.getResult();
    Assert.assertEquals(null, exception.getErrorData());
    response = Response.create(400, "test", "errorData");
    exception = response.getResult();
    Assert.assertEquals("errorData", exception.getErrorData());
}
Also used : InvocationException(io.servicecomb.core.exception.InvocationException) Test(org.junit.Test)

Example 10 with InvocationException

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

the class OperationLocator method locate.

public void locate(ServicePathManager servicePathManager, String path, String httpMethod) {
    // 在静态路径中查找
    operation = locateStaticPathOperation(path, httpMethod, servicePathManager.getStaticPathOperationMap());
    if (operation != null) {
        // 全部定位完成
        return;
    }
    // 在动态路径中查找
    operation = locateDynamicPathOperation(path, servicePathManager.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, servicePathManager.getMicroserviceMeta().getName());
    throw new InvocationException(status, status.getReasonPhrase());
}
Also used : Status(javax.ws.rs.core.Response.Status) InvocationException(io.servicecomb.core.exception.InvocationException)

Aggregations

InvocationException (io.servicecomb.core.exception.InvocationException)20 Test (org.junit.Test)13 CommonExceptionData (io.servicecomb.core.exception.CommonExceptionData)8 Invocation (io.servicecomb.core.Invocation)6 OperationMeta (io.servicecomb.core.definition.OperationMeta)6 Response (io.servicecomb.core.Response)4 RestParam (io.servicecomb.common.rest.definition.RestParam)3 MicroserviceMeta (io.servicecomb.core.definition.MicroserviceMeta)3 HystrixCommandProperties (com.netflix.hystrix.HystrixCommandProperties)2 RestOperationMeta (io.servicecomb.common.rest.definition.RestOperationMeta)2 ArrayList (java.util.ArrayList)2 Holder (javax.xml.ws.Holder)2 Expectations (mockit.Expectations)2 MockUp (mockit.MockUp)2 WrapSchema (io.servicecomb.codec.protobuf.utils.WrapSchema)1 ProduceProcessor (io.servicecomb.common.rest.codec.produce.ProduceProcessor)1 OperationLocator (io.servicecomb.common.rest.locator.OperationLocator)1 ServicePathManager (io.servicecomb.common.rest.locator.ServicePathManager)1 AsyncResponse (io.servicecomb.core.AsyncResponse)1 Endpoint (io.servicecomb.core.Endpoint)1