Search in sources :

Example 6 with CommonExceptionData

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

the class TestRestCodec method testRestToArgsExcetpion.

@Test
public void testRestToArgsExcetpion(@Mocked RestServerRequest request, @Mocked RestOperationMeta restOperation, @Mocked RestParam restParam, @Mocked ParamValueProcessor processer) throws Exception {
    List<RestParam> params = new ArrayList<>();
    params.add(restParam);
    new Expectations() {

        {
            restOperation.getParamList();
            result = params;
            restParam.getParamProcessor();
            result = processer;
            processer.getValue(request);
            result = new Exception("bad request parame");
        }
    };
    boolean success = false;
    try {
        RestCodec.restToArgs(request, restOperation);
        success = true;
    } catch (InvocationException e) {
        Assert.assertEquals(590, e.getStatusCode());
        Assert.assertEquals("Parameter is not valid.", ((CommonExceptionData) e.getErrorData()).getMessage());
    }
    Assert.assertEquals(success, false);
}
Also used : Expectations(mockit.Expectations) InvocationException(io.servicecomb.core.exception.InvocationException) ArrayList(java.util.ArrayList) CommonExceptionData(io.servicecomb.core.exception.CommonExceptionData) InvocationException(io.servicecomb.core.exception.InvocationException) RestParam(io.servicecomb.common.rest.definition.RestParam) Test(org.junit.Test)

Example 7 with CommonExceptionData

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

the class TestLocator method testLocateDynamic.

@Test
public void testLocateDynamic() {
    MicroserviceMeta msm = new MicroserviceMeta("app:ms");
    ServicePathManager spm = new ServicePathManager(msm);
    RestOperationMeta rom = createRestOperatonMeta("GET", "abc/{id}");
    spm.addResource(rom);
    try {
        spm.locateOperation("abc/10", "PUT");
    } catch (InvocationException e) {
        Assert.assertEquals("Method Not Allowed", ((CommonExceptionData) e.getErrorData()).getMessage());
    }
    OperationLocator locator = spm.locateOperation("abc/10", "GET");
    Assert.assertEquals("10", locator.getPathVarMap().get("id"));
}
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 8 with CommonExceptionData

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

the class TestProducerSchemaFactory method testGetOrCreateProducer.

@Test
public void testGetOrCreateProducer() throws Exception {
    OperationMeta operationMeta = schemaMeta.ensureFindOperation("add");
    Assert.assertEquals("add", operationMeta.getOperationId());
    ProducerOperation producerOperation = operationMeta.getExtData(Const.PRODUCER_OPERATION);
    Object addBody = Class.forName("cse.gen.app.ms.schema.addBody").newInstance();
    ReflectUtils.setField(addBody, "x", 1);
    ReflectUtils.setField(addBody, "y", 2);
    Invocation invocation = new Invocation((Endpoint) null, operationMeta, new Object[] { addBody });
    Holder<Response> holder = new Holder<>();
    producerOperation.invoke(invocation, resp -> {
        holder.value = resp;
    });
    Assert.assertEquals(3, (int) holder.value.getResult());
    invocation = new Invocation((Endpoint) null, operationMeta, new Object[] { 1, 2 });
    producerOperation.invoke(invocation, resp -> {
        holder.value = resp;
    });
    Assert.assertEquals(true, holder.value.isFailed());
    InvocationException exception = (InvocationException) holder.value.getResult();
    CommonExceptionData data = (CommonExceptionData) exception.getErrorData();
    Assert.assertEquals("Cse Internal Server Error", data.getMessage());
}
Also used : Response(io.servicecomb.core.Response) Invocation(io.servicecomb.core.Invocation) Endpoint(io.servicecomb.core.Endpoint) ProducerOperation(io.servicecomb.core.provider.producer.ProducerOperation) InvocationException(io.servicecomb.core.exception.InvocationException) Holder(javax.xml.ws.Holder) CommonExceptionData(io.servicecomb.core.exception.CommonExceptionData) OperationMeta(io.servicecomb.core.definition.OperationMeta) Test(org.junit.Test)

Example 9 with CommonExceptionData

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

the class VertxHttpMethod method handleResponse.

protected void handleResponse(Invocation invocation, HttpClientResponse httpResponse, RestOperationMeta restOperation, AsyncResponse asyncResp) {
    ProduceProcessor produceProcessor = null;
    String contentType = httpResponse.getHeader("Content-Type");
    if (contentType != null) {
        String contentTypeForFind = contentType;
        int idx = contentType.indexOf(';');
        if (idx != -1) {
            contentTypeForFind = contentType.substring(0, idx);
        }
        produceProcessor = restOperation.findProduceProcessor(contentTypeForFind);
    }
    if (produceProcessor == null) {
        String msg = String.format("path %s, statusCode %d, reasonPhrase %s, response content-type %s is not supported", restOperation.getAbsolutePath(), httpResponse.statusCode(), httpResponse.statusMessage(), contentType);
        Exception exception = ExceptionFactory.createConsumerException(new CommonExceptionData(msg));
        asyncResp.fail(invocation.getInvocationType(), exception);
        return;
    }
    ProduceProcessor finalProduceProcessor = produceProcessor;
    httpResponse.bodyHandler(responseBuf -> {
        invocation.getResponseExecutor().execute(() -> {
            try {
                ResponseMeta responseMeta = restOperation.getOperationMeta().findResponseMeta(httpResponse.statusCode());
                Object result = finalProduceProcessor.decodeResponse(responseBuf, responseMeta.getJavaType());
                Response response = Response.create(httpResponse.statusCode(), httpResponse.statusMessage(), result);
                for (String headerName : responseMeta.getHeaders().keySet()) {
                    List<String> headerValues = httpResponse.headers().getAll(headerName);
                    for (String headerValue : headerValues) {
                        response.getHeaders().addHeader(headerName, headerValue);
                    }
                }
                asyncResp.complete(response);
            } catch (Throwable e) {
                asyncResp.fail(invocation.getInvocationType(), e);
            }
        });
    });
}
Also used : AsyncResponse(io.servicecomb.core.AsyncResponse) HttpClientResponse(io.vertx.core.http.HttpClientResponse) Response(io.servicecomb.core.Response) ProduceProcessor(io.servicecomb.common.rest.codec.produce.ProduceProcessor) ResponseMeta(io.servicecomb.swagger.invocation.response.ResponseMeta) CommonExceptionData(io.servicecomb.core.exception.CommonExceptionData)

Example 10 with CommonExceptionData

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

the class TestException method testCommonExceptionData.

@Test
public void testCommonExceptionData() {
    CommonExceptionData oData = new CommonExceptionData();
    oData.setMessage("this is Common exception message");
    Assert.assertEquals("this is Common exception message", oData.getMessage());
    oData = new CommonExceptionData("this is a test");
    Assert.assertEquals("this is a test", oData.getMessage());
    Assert.assertEquals("CommonExceptionData [message=this is a test]", oData.toString());
}
Also used : 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