Search in sources :

Example 1 with CommonExceptionData

use of org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData in project incubator-servicecomb-java-chassis by apache.

the class TestProducerSchemaFactory method testGetOrCreateProducer.

@Test
public void testGetOrCreateProducer() throws Exception {
    OperationMeta operationMeta = schemaMeta.ensureFindOperation("add");
    Assert.assertEquals("add", operationMeta.getOperationId());
    SwaggerProducerOperation 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 }) {

        @Override
        public String getInvocationQualifiedName() {
            return "";
        }
    };
    Holder<Response> holder = new Holder<>();
    producerOperation.invoke(invocation, resp -> {
        holder.value = resp;
    });
    Assert.assertEquals(3, (int) holder.value.getResult());
    invocation.setSwaggerArguments(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(org.apache.servicecomb.swagger.invocation.Response) Invocation(org.apache.servicecomb.core.Invocation) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) Holder(javax.xml.ws.Holder) SwaggerProducerOperation(org.apache.servicecomb.swagger.engine.SwaggerProducerOperation) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) Test(org.junit.Test)

Example 2 with CommonExceptionData

use of org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData in project incubator-servicecomb-java-chassis by apache.

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 : AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Response(org.apache.servicecomb.swagger.invocation.Response) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) Test(org.junit.Test)

Example 3 with CommonExceptionData

use of org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData in project incubator-servicecomb-java-chassis by apache.

the class TestException method testInvocationException.

@Test
public void testInvocationException() {
    InvocationException oExceptionIn = new InvocationException(Status.OK, "I am gone now");
    oExceptionIn = ExceptionFactory.convertConsumerException(new Throwable());
    Assert.assertEquals(490, oExceptionIn.getStatusCode());
    oExceptionIn = ExceptionFactory.convertConsumerException(new Throwable(), "abc");
    Assert.assertEquals(490, oExceptionIn.getStatusCode());
    Assert.assertEquals("abc", ((CommonExceptionData) oExceptionIn.getErrorData()).getMessage());
    oExceptionIn = ExceptionFactory.convertProducerException(new Throwable());
    Assert.assertEquals(590, oExceptionIn.getStatusCode());
    oExceptionIn = ExceptionFactory.convertProducerException(new Throwable(), "abcd");
    Assert.assertEquals(590, oExceptionIn.getStatusCode());
    Assert.assertEquals("abcd", ((CommonExceptionData) oExceptionIn.getErrorData()).getMessage());
    oExceptionIn = ExceptionFactory.convertConsumerException(new InvocationException(Status.OK, new String("fake-object")));
    Assert.assertEquals(200, oExceptionIn.getStatusCode());
    oExceptionIn = ExceptionFactory.convertConsumerException(new InvocationTargetException(new Throwable()));
    Assert.assertNotEquals("java.lang.Throwable", oExceptionIn.getMessage());
    InvocationException oTemp = new InvocationException(Status.OK, new CommonExceptionData("testObject"));
    Assert.assertEquals("OK", oTemp.getReasonPhrase());
    Assert.assertEquals("CommonExceptionData [message=testObject]", (oTemp.getErrorData().toString()));
}
Also used : InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.Test)

Example 4 with CommonExceptionData

use of org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData 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 5 with CommonExceptionData

use of org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData in project incubator-servicecomb-java-chassis by apache.

the class DefaultHttpClientFilter method afterReceiveResponse.

@Override
public Response afterReceiveResponse(Invocation invocation, HttpServletResponseEx responseEx) {
    OperationMeta operationMeta = invocation.getOperationMeta();
    ResponseMeta responseMeta = operationMeta.findResponseMeta(responseEx.getStatus());
    RestOperationMeta swaggerRestOperation = operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
    ProduceProcessor produceProcessor = findProduceProcessor(swaggerRestOperation, responseEx);
    if (produceProcessor == null) {
        String msg = String.format("method %s, path %s, statusCode %d, reasonPhrase %s, response content-type %s is not supported", swaggerRestOperation.getHttpMethod(), swaggerRestOperation.getAbsolutePath(), responseEx.getStatus(), responseEx.getStatusType().getReasonPhrase(), responseEx.getHeader(HttpHeaders.CONTENT_TYPE));
        Exception exception = ExceptionFactory.createConsumerException(new CommonExceptionData(msg));
        return Response.consumerFailResp(exception);
    }
    Object result = null;
    try {
        result = produceProcessor.decodeResponse(responseEx.getBodyBuffer(), responseMeta.getJavaType());
    } catch (Exception e) {
        return Response.consumerFailResp(e);
    }
    Response response = Response.create(responseEx.getStatusType(), result);
    for (String headerName : responseEx.getHeaderNames()) {
        Collection<String> headerValues = responseEx.getHeaders(headerName);
        for (String headerValue : headerValues) {
            response.getHeaders().addHeader(headerName, headerValue);
        }
    }
    return response;
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) ProduceProcessor(org.apache.servicecomb.common.rest.codec.produce.ProduceProcessor) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) ResponseMeta(org.apache.servicecomb.swagger.invocation.response.ResponseMeta) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta)

Aggregations

CommonExceptionData (org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData)10 InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)8 Test (org.junit.Test)6 Response (org.apache.servicecomb.swagger.invocation.Response)4 Expectations (mockit.Expectations)2 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 Holder (javax.xml.ws.Holder)1 ProduceProcessor (org.apache.servicecomb.common.rest.codec.produce.ProduceProcessor)1 RestOperationMeta (org.apache.servicecomb.common.rest.definition.RestOperationMeta)1 RestParam (org.apache.servicecomb.common.rest.definition.RestParam)1 Invocation (org.apache.servicecomb.core.Invocation)1 SwaggerProducerOperation (org.apache.servicecomb.swagger.engine.SwaggerProducerOperation)1 AsyncResponse (org.apache.servicecomb.swagger.invocation.AsyncResponse)1 ResponseMeta (org.apache.servicecomb.swagger.invocation.response.ResponseMeta)1