Search in sources :

Example 11 with InvocationException

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

the class AbstractRestServer method findRestOperation.

protected RestOperationMeta findRestOperation(RestServerRequestInternal restRequest) {
    String selfName = RegistryUtils.getMicroservice().getServiceName();
    MicroserviceMeta selfMicroserviceMeta = CseContext.getInstance().getMicroserviceMetaManager().ensureFindValue(selfName);
    ServicePathManager servicePathManager = ServicePathManager.getServicePathManager(selfMicroserviceMeta);
    if (servicePathManager == null) {
        LOGGER.error("No schema in microservice");
        throw new InvocationException(Status.NOT_FOUND, Status.NOT_FOUND.getReasonPhrase());
    }
    OperationLocator locator = servicePathManager.locateOperation(restRequest.getPath(), restRequest.getMethod());
    restRequest.setPathParamMap(locator.getPathVarMap());
    return locator.getOperation();
}
Also used : OperationLocator(io.servicecomb.common.rest.locator.OperationLocator) InvocationException(io.servicecomb.core.exception.InvocationException) MicroserviceMeta(io.servicecomb.core.definition.MicroserviceMeta) ServicePathManager(io.servicecomb.common.rest.locator.ServicePathManager)

Example 12 with InvocationException

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

the class RestCodec method restToArgs.

public static Object[] restToArgs(RestServerRequest request, RestOperationMeta restOperation) throws InvocationException {
    List<RestParam> paramList = restOperation.getParamList();
    try {
        Object[] paramValues = new Object[paramList.size()];
        for (int idx = 0; idx < paramList.size(); idx++) {
            RestParam param = paramList.get(idx);
            paramValues[idx] = param.getParamProcessor().getValue(request);
        }
        return paramValues;
    } catch (Exception e) {
        LOG.error("Parameter is not valid, cause " + e.getMessage());
        throw ExceptionFactory.convertProducerException(e, "Parameter is not valid.");
    }
}
Also used : InvocationException(io.servicecomb.core.exception.InvocationException) RestParam(io.servicecomb.common.rest.definition.RestParam)

Example 13 with InvocationException

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

the class AbstractRestServer method locateProduceProcessor.

protected ProduceProcessor locateProduceProcessor(RestServerRequestInternal restRequest, HTTP_RESPONSE httpResponse, RestOperationMeta restOperation, String acceptType) {
    ProduceProcessor produceProcessor = restOperation.ensureFindProduceProcessor(acceptType);
    if (produceProcessor != null) {
        return produceProcessor;
    }
    String msg = String.format("Accept %s is not supported", acceptType);
    InvocationException exception = new InvocationException(Status.NOT_ACCEPTABLE, msg);
    sendFailResponse(restRequest, httpResponse, exception);
    return null;
}
Also used : ProduceProcessor(io.servicecomb.common.rest.codec.produce.ProduceProcessor) InvocationException(io.servicecomb.core.exception.InvocationException)

Example 14 with InvocationException

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

the class TestRestCodec method testRestToArgsInstanceExcetpion.

@Test
public void testRestToArgsInstanceExcetpion(@Mocked RestServerRequest 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(io.servicecomb.core.exception.InvocationException) ArrayList(java.util.ArrayList) RestParam(io.servicecomb.common.rest.definition.RestParam) Test(org.junit.Test)

Example 15 with InvocationException

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

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