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);
}
}
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);
}
}
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);
}
}
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;
}
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));
}
}
}
Aggregations