Search in sources :

Example 41 with SofaRpcException

use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.

the class RestClientTransport method getMethod.

@Override
protected Method getMethod(SofaRequest request) throws SofaRpcException {
    String serviceUniqueName = request.getTargetServiceUniqueName();
    String methodName = request.getMethodName();
    String[] methodSigns = request.getMethodArgSigs();
    Method method = ReflectCache.getOverloadMethodCache(serviceUniqueName, methodName, methodSigns);
    if (method == null) {
        try {
            String interfaceName = request.getInterfaceName();
            method = ClassUtils.forName(interfaceName).getMethod(methodName, ClassTypeUtils.getClasses(methodSigns));
            ReflectCache.putOverloadMethodCache(serviceUniqueName, method);
        } catch (NoSuchMethodException e) {
            throw new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, "Method not found", e);
        }
    }
    return method;
}
Also used : Method(java.lang.reflect.Method) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Example 42 with SofaRpcException

use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.

the class TripleClientTransport method convertToRpcException.

/**
 * 转换调用出现的异常为RPC异常
 *
 * @param e 异常
 * @return RPC异常
 */
protected SofaRpcException convertToRpcException(Exception e) {
    SofaRpcException exception;
    if (e instanceof SofaRpcException) {
        exception = (SofaRpcException) e;
        return exception;
    }
    Status status = Status.fromThrowable(e);
    StatusException grpcException = status.asException();
    if (status.getCode() == Status.DEADLINE_EXCEEDED.getCode()) {
        exception = new SofaTimeOutException(grpcException);
    } else if (status.getCode() == Status.NOT_FOUND.getCode()) {
        exception = new SofaRpcException(RpcErrorType.SERVER_NOT_FOUND_INVOKER, grpcException);
    } else if (status.getCode() == Status.UNAVAILABLE.getCode()) {
        exception = new SofaRpcException(RpcErrorType.CLIENT_NETWORK, grpcException);
    } else if (status.getCode() == Status.RESOURCE_EXHAUSTED.getCode()) {
        exception = new SofaRpcException(RpcErrorType.SERVER_BUSY, grpcException);
    } else {
        exception = new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, grpcException);
    }
    return exception;
}
Also used : Status(io.grpc.Status) StatusException(io.grpc.StatusException) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Example 43 with SofaRpcException

use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.

the class TripleClientTransport method syncSend.

@Override
public SofaResponse syncSend(SofaRequest request, int timeout) throws SofaRpcException {
    SofaResponse sofaResponse = null;
    SofaRpcException throwable = null;
    try {
        RpcInternalContext context = RpcInternalContext.getContext();
        beforeSend(context, request);
        RpcInvokeContext invokeContext = RpcInvokeContext.getContext();
        invokeContext.put(TripleContants.SOFA_REQUEST_KEY, request);
        invokeContext.put(TripleContants.SOFA_CONSUMER_CONFIG_KEY, transportConfig.getConsumerConfig());
        sofaResponse = tripleClientInvoker.invoke(request, timeout);
        return sofaResponse;
    } catch (Exception e) {
        throwable = convertToRpcException(e);
        throw throwable;
    } finally {
        if (EventBus.isEnable(ClientSyncReceiveEvent.class)) {
            EventBus.post(new ClientSyncReceiveEvent(transportConfig.getConsumerConfig(), transportConfig.getProviderInfo(), request, sofaResponse, throwable));
        }
    }
}
Also used : RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) ClientSyncReceiveEvent(com.alipay.sofa.rpc.event.ClientSyncReceiveEvent) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) StatusException(io.grpc.StatusException) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Example 44 with SofaRpcException

use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.

the class JacksonSerializerTest method testJacksonFeature.

@Test
public void testJacksonFeature() throws UnsupportedEncodingException {
    try {
        JacksonSerializer serializer = new JacksonSerializer();
        serializer.decode(new ByteArrayWrapperByteBuf("{\"a\":1}".getBytes("UTF-8")), DemoRequest.class, null);
        Assert.fail();
    } catch (SofaRpcException e) {
    // ok
    } catch (Throwable e) {
        Assert.fail();
    }
    try {
        JacksonSerializer serializer = new JacksonSerializer();
        serializer.encode(new DemoRequest2(), null);
        Assert.fail();
    } catch (SofaRpcException e) {
    // ok
    } catch (Throwable e) {
        Assert.fail();
    }
    System.setProperty("sofa.rpc.codec.jackson.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES", "false");
    JacksonSerializer serializer = new JacksonSerializer();
    serializer.decode(new ByteArrayWrapperByteBuf("{\"a\":1}".getBytes("UTF-8")), DemoRequest.class, null);
    System.setProperty("sofa.rpc.codec.jackson.SerializationFeature.FAIL_ON_EMPTY_BEANS", "false");
    serializer = new JacksonSerializer();
    serializer.encode(new DemoRequest2(), null);
    System.setProperty("sofa.rpc.codec.jackson.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES", "true");
    System.setProperty("sofa.rpc.codec.jackson.SerializationFeature.FAIL_ON_EMPTY_BEANS", "true");
}
Also used : DemoRequest2(com.alipay.sofa.rpc.codec.jackson.model.DemoRequest2) ByteArrayWrapperByteBuf(com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Test(org.junit.Test)

Example 45 with SofaRpcException

use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.

the class JacksonSerializerTest method buildRequest.

private SofaRequest buildRequest(String methodName, Object[] args) throws NoSuchMethodException {
    SofaRequest request = new SofaRequest();
    request.setInterfaceName(DemoService.class.getName());
    request.setMethodName(methodName);
    Method method = null;
    for (Method m : DemoService.class.getMethods()) {
        if (m.getName().equals(methodName)) {
            method = m;
        }
    }
    request.setMethod(method);
    request.setMethodArgs(args);
    List<String> argSigs = new ArrayList<String>();
    for (Object req : args) {
        argSigs.add(req.getClass().getName());
    }
    request.setMethodArgSigs(argSigs.toArray(new String[argSigs.size()]));
    request.setTargetServiceUniqueName(DemoService.class.getName() + ":1.0");
    request.setTargetAppName("targetApp");
    request.setSerializeType((byte) 12);
    request.setTimeout(1024);
    request.setInvokeType(RpcConstants.INVOKER_TYPE_SYNC);
    Map<String, String> map = new HashMap<String, String>();
    map.put("a", "xxx");
    map.put("b", "yyy");
    request.addRequestProp(RemotingConstants.RPC_TRACE_NAME, map);
    request.setSofaResponseCallback(new SofaResponseCallback() {

        @Override
        public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
        }

        @Override
        public void onAppException(Throwable throwable, String methodName, RequestBase request) {
        }

        @Override
        public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
        }
    });
    return request;
}
Also used : SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DemoService(com.alipay.sofa.rpc.codec.jackson.model.DemoService) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) Method(java.lang.reflect.Method) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Aggregations

SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)91 Test (org.junit.Test)35 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)28 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)27 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)24 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)23 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)22 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)21 CountDownLatch (java.util.concurrent.CountDownLatch)20 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)19 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)16 HelloService (com.alipay.sofa.rpc.test.HelloService)16 RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)15 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)14 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)14 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)11 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)11 HashMap (java.util.HashMap)9 Filter (com.alipay.sofa.rpc.filter.Filter)8 InvokeTimeoutException (com.alipay.remoting.rpc.exception.InvokeTimeoutException)7