Search in sources :

Example 76 with SofaRpcException

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

the class AbstractHttp2ClientTransport method syncSend.

@Override
public SofaResponse syncSend(SofaRequest request, int timeout) throws SofaRpcException {
    checkConnection();
    RpcInternalContext context = RpcInternalContext.getContext();
    try {
        beforeSend(context, request);
        return doInvokeSync(request, timeout);
    } catch (TimeoutException e) {
        throw timeoutException(request, timeout, e);
    } catch (SofaRpcException e) {
        throw e;
    } catch (Exception e) {
        throw new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, e.getMessage(), e);
    } finally {
        afterSend(context, request);
    }
}
Also used : RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) TimeoutException(java.util.concurrent.TimeoutException) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) ExecutionException(java.util.concurrent.ExecutionException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) TimeoutException(java.util.concurrent.TimeoutException)

Example 77 with SofaRpcException

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

the class AbstractHttpClientHandler method receiveHttpResponse.

public void receiveHttpResponse(FullHttpResponse msg) {
    HttpHeaders headers = msg.headers();
    ByteBuf content = msg.content();
    NettyByteBuffer data = new NettyByteBuffer(content);
    try {
        if (msg.status() == HttpResponseStatus.OK) {
            // 正常返回
            final SofaResponse response = new SofaResponse();
            String isError = headers.get(RemotingConstants.HEAD_RESPONSE_ERROR);
            if (CommonUtils.isTrue(isError)) {
                // 业务异常
                String errorMsg = StringSerializer.decode(data.array());
                Throwable throwable = new SofaRpcException(RpcErrorType.SERVER_BIZ, errorMsg);
                response.setAppResponse(throwable);
            } else {
                // 获取序列化类型
                if (data.readableBytes() > 0) {
                    byte serializeType;
                    String codeName = headers.get(RemotingConstants.HEAD_SERIALIZE_TYPE);
                    if (codeName != null) {
                        serializeType = HttpTransportUtils.getSerializeTypeByName(codeName);
                    } else {
                        // HEAD_SERIALIZE_TYPE 没设置的话 再取 content-type 兜底下
                        String contentType = StringUtils.toString(headers.get(HttpHeaderNames.CONTENT_TYPE));
                        serializeType = HttpTransportUtils.getSerializeTypeByContentType(contentType);
                    }
                    response.setSerializeType(serializeType);
                    content.retain();
                    response.setData(data);
                }
            }
            onResponse(response);
        } else {
            // 系统异常
            String errorMsg = StringSerializer.decode(data.array());
            Throwable throwable = new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, errorMsg);
            onException(throwable);
        }
    } catch (final Exception e) {
        onException(e);
    }
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) ByteBuf(io.netty.buffer.ByteBuf) AbstractByteBuf(com.alipay.sofa.rpc.transport.AbstractByteBuf) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) NettyByteBuffer(com.alipay.sofa.rpc.transport.netty.NettyByteBuffer) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Example 78 with SofaRpcException

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

the class BoltClientTransport method asyncSend.

@Override
public ResponseFuture asyncSend(SofaRequest request, int timeout) throws SofaRpcException {
    checkConnection();
    RpcInternalContext context = RpcInternalContext.getContext();
    InvokeContext boltInvokeContext = createInvokeContext(request);
    try {
        beforeSend(context, request);
        boltInvokeContext.put(RemotingConstants.INVOKE_CTX_RPC_CTX, context);
        return doInvokeAsync(request, context, boltInvokeContext, timeout);
    } catch (Exception e) {
        throw convertToRpcException(e);
    } finally {
        afterSend(context, boltInvokeContext, request);
    }
}
Also used : InvokeContext(com.alipay.remoting.InvokeContext) RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) InvokeServerException(com.alipay.remoting.rpc.exception.InvokeServerException) InvokeSendFailedException(com.alipay.remoting.rpc.exception.InvokeSendFailedException) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) DeserializationException(com.alipay.remoting.exception.DeserializationException) InvokeTimeoutException(com.alipay.remoting.rpc.exception.InvokeTimeoutException) SerializationException(com.alipay.remoting.exception.SerializationException) RemotingException(com.alipay.remoting.exception.RemotingException) ConnectionClosedException(com.alipay.remoting.exception.ConnectionClosedException) InvokeServerBusyException(com.alipay.remoting.rpc.exception.InvokeServerBusyException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)

Example 79 with SofaRpcException

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

the class BoltClientTransport method convertToRpcException.

/**
 * 转换调用出现的异常为RPC异常
 *
 * @param e 异常
 * @return RPC异常
 */
protected SofaRpcException convertToRpcException(Exception e) {
    SofaRpcException exception;
    if (e instanceof SofaRpcException) {
        exception = (SofaRpcException) e;
    } else // 超时
    if (e instanceof InvokeTimeoutException) {
        exception = new SofaTimeOutException(e);
    } else // 服务器忙
    if (e instanceof InvokeServerBusyException) {
        exception = new SofaRpcException(RpcErrorType.SERVER_BUSY, e);
    } else // 序列化
    if (e instanceof SerializationException) {
        boolean isServer = ((SerializationException) e).isServerSide();
        exception = isServer ? new SofaRpcException(RpcErrorType.SERVER_SERIALIZE, e) : new SofaRpcException(RpcErrorType.CLIENT_SERIALIZE, e);
    } else // 反序列化
    if (e instanceof DeserializationException) {
        boolean isServer = ((DeserializationException) e).isServerSide();
        exception = isServer ? new SofaRpcException(RpcErrorType.SERVER_DESERIALIZE, e) : new SofaRpcException(RpcErrorType.CLIENT_DESERIALIZE, e);
    } else // 长连接断连
    if (e instanceof ConnectionClosedException) {
        exception = new SofaRpcException(RpcErrorType.CLIENT_NETWORK, e);
    } else // 客户端发送失败
    if (e instanceof InvokeSendFailedException) {
        exception = new SofaRpcException(RpcErrorType.CLIENT_NETWORK, e);
    } else // 服务端未知异常
    if (e instanceof InvokeServerException) {
        exception = new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, e.getCause());
    } else // 客户端未知
    {
        exception = new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, e);
    }
    return exception;
}
Also used : InvokeTimeoutException(com.alipay.remoting.rpc.exception.InvokeTimeoutException) SerializationException(com.alipay.remoting.exception.SerializationException) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) InvokeSendFailedException(com.alipay.remoting.rpc.exception.InvokeSendFailedException) ConnectionClosedException(com.alipay.remoting.exception.ConnectionClosedException) InvokeServerException(com.alipay.remoting.rpc.exception.InvokeServerException) InvokeServerBusyException(com.alipay.remoting.rpc.exception.InvokeServerBusyException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) DeserializationException(com.alipay.remoting.exception.DeserializationException)

Example 80 with SofaRpcException

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

the class BoltClientTransport method oneWaySend.

@Override
public void oneWaySend(SofaRequest request, int timeout) throws SofaRpcException {
    checkConnection();
    RpcInternalContext context = RpcInternalContext.getContext();
    InvokeContext invokeContext = createInvokeContext(request);
    SofaRpcException throwable = null;
    try {
        beforeSend(context, request);
        doOneWay(request, invokeContext, timeout);
    } catch (Exception e) {
        // 其它异常
        throwable = convertToRpcException(e);
        throw throwable;
    } finally {
        afterSend(context, invokeContext, request);
        if (EventBus.isEnable(ClientSyncReceiveEvent.class)) {
            EventBus.post(new ClientSyncReceiveEvent(transportConfig.getConsumerConfig(), transportConfig.getProviderInfo(), request, null, throwable));
        }
    }
}
Also used : InvokeContext(com.alipay.remoting.InvokeContext) RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) ClientSyncReceiveEvent(com.alipay.sofa.rpc.event.ClientSyncReceiveEvent) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) InvokeServerException(com.alipay.remoting.rpc.exception.InvokeServerException) InvokeSendFailedException(com.alipay.remoting.rpc.exception.InvokeSendFailedException) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) DeserializationException(com.alipay.remoting.exception.DeserializationException) InvokeTimeoutException(com.alipay.remoting.rpc.exception.InvokeTimeoutException) SerializationException(com.alipay.remoting.exception.SerializationException) RemotingException(com.alipay.remoting.exception.RemotingException) ConnectionClosedException(com.alipay.remoting.exception.ConnectionClosedException) InvokeServerBusyException(com.alipay.remoting.rpc.exception.InvokeServerBusyException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)

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