use of com.alipay.remoting.exception.SerializationException in project sofa-rpc by sofastack.
the class SofaRpcSerialization method serializeContent.
@Override
public <Request extends RequestCommand> boolean serializeContent(Request request, InvokeContext invokeContext) throws SerializationException {
if (request instanceof RpcRequestCommand) {
RpcRequestCommand requestCommand = (RpcRequestCommand) request;
Object requestObject = requestCommand.getRequestObject();
byte serializerCode = requestCommand.getSerializer();
long serializeStartTime = System.nanoTime();
try {
Map<String, String> header = (Map<String, String>) requestCommand.getRequestHeader();
if (header == null) {
header = new HashMap<String, String>();
}
putKV(header, RemotingConstants.HEAD_GENERIC_TYPE, (String) invokeContext.get(RemotingConstants.HEAD_GENERIC_TYPE));
Serializer rpcSerializer = com.alipay.sofa.rpc.codec.SerializerFactory.getSerializer(serializerCode);
AbstractByteBuf byteBuf = rpcSerializer.encode(requestObject, header);
request.setContent(byteBuf.array());
return true;
} catch (Exception ex) {
throw new SerializationException(ex.getMessage(), ex);
} finally {
// R5:record request serialization time
recordSerializeRequest(requestCommand, invokeContext, serializeStartTime);
}
}
return false;
}
use of com.alipay.remoting.exception.SerializationException in project sofa-rpc by sofastack.
the class SofaRpcSerialization method serializeHeader.
@Override
public <Response extends ResponseCommand> boolean serializeHeader(Response response) throws SerializationException {
if (response instanceof RpcResponseCommand) {
RpcInternalContext.getContext().getStopWatch().tick();
Object responseObject = ((RpcResponseCommand) response).getResponseObject();
if (responseObject instanceof SofaResponse) {
SofaResponse sofaResponse = (SofaResponse) responseObject;
if (sofaResponse.isError() || sofaResponse.getAppResponse() instanceof Throwable) {
sofaResponse.addResponseProp(RemotingConstants.HEAD_RESPONSE_ERROR, StringUtils.TRUE);
}
byte[] header = null;
try {
header = mapSerializer.encode(sofaResponse.getResponseProps());
} catch (Exception e) {
String traceId = (String) RpcInternalContext.getContext().getAttachment("_trace_id");
String rpcId = (String) RpcInternalContext.getContext().getAttachment("_span_id");
LOGGER.error("traceId={}, rpcId={}, Response serializeHeader exception, msg={}", traceId, rpcId, e.getMessage(), e);
throw new SerializationException(e.getMessage() + ", traceId=" + traceId + ", rpcId=" + rpcId, e);
}
response.setHeader(header);
}
return true;
}
return false;
}
use of com.alipay.remoting.exception.SerializationException in project sofa-rpc by sofastack.
the class SofaRpcSerialization method serializeContent.
@Override
public <Response extends ResponseCommand> boolean serializeContent(Response response) throws SerializationException {
if (response instanceof RpcResponseCommand) {
RpcResponseCommand responseCommand = (RpcResponseCommand) response;
byte serializerCode = response.getSerializer();
long serializeStartTime = System.nanoTime();
try {
Serializer rpcSerializer = com.alipay.sofa.rpc.codec.SerializerFactory.getSerializer(serializerCode);
AbstractByteBuf byteBuf = rpcSerializer.encode(responseCommand.getResponseObject(), null);
responseCommand.setContent(byteBuf.array());
return true;
} catch (Exception ex) {
String traceId = (String) RpcInternalContext.getContext().getAttachment("_trace_id");
String rpcId = (String) RpcInternalContext.getContext().getAttachment("_span_id");
LOGGER.error("traceId={}, rpcId={}, Response serializeContent exception, msg = {}", traceId, rpcId, ex.getMessage(), ex);
throw new SerializationException(ex.getMessage() + ", traceId=" + traceId + ", rpcId=" + rpcId, ex);
} finally {
// R6:Record response serialization time
recordSerializeResponse(responseCommand, serializeStartTime);
}
}
return false;
}
use of com.alipay.remoting.exception.SerializationException in project sofa-rpc by sofastack.
the class BoltClientTransportTest method testConvertToRpcException.
@Test
public void testConvertToRpcException() {
ClientTransportConfig config1 = new ClientTransportConfig();
config1.setProviderInfo(new ProviderInfo().setHost("127.0.0.1").setPort(12222)).setContainer("bolt");
BoltClientTransport transport = new BoltClientTransport(config1);
Assert.assertTrue(transport.convertToRpcException(new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, "")) instanceof SofaRpcException);
Assert.assertTrue(transport.convertToRpcException(new InvokeTimeoutException()) instanceof SofaTimeOutException);
Assert.assertTrue(transport.convertToRpcException(new InvokeServerBusyException()).getErrorType() == RpcErrorType.SERVER_BUSY);
Assert.assertTrue(transport.convertToRpcException(new SerializationException("xx", true)).getErrorType() == RpcErrorType.SERVER_SERIALIZE);
Assert.assertTrue(transport.convertToRpcException(new SerializationException("xx", false)).getErrorType() == RpcErrorType.CLIENT_SERIALIZE);
Assert.assertTrue(transport.convertToRpcException(new DeserializationException("xx", true)).getErrorType() == RpcErrorType.SERVER_DESERIALIZE);
Assert.assertTrue(transport.convertToRpcException(new DeserializationException("xx", false)).getErrorType() == RpcErrorType.CLIENT_DESERIALIZE);
Assert.assertTrue(transport.convertToRpcException(new ConnectionClosedException()).getErrorType() == RpcErrorType.CLIENT_NETWORK);
Assert.assertTrue(transport.convertToRpcException(new InvokeSendFailedException()).getErrorType() == RpcErrorType.CLIENT_NETWORK);
Assert.assertTrue(transport.convertToRpcException(new InvokeServerException()).getErrorType() == RpcErrorType.SERVER_UNDECLARED_ERROR);
Assert.assertTrue(transport.convertToRpcException(new UnsupportedOperationException()).getErrorType() == RpcErrorType.CLIENT_UNDECLARED_ERROR);
}
use of com.alipay.remoting.exception.SerializationException in project sofa-rpc by sofastack.
the class SofaRpcSerializationTest method serializeResponseContent.
@Test
public void serializeResponseContent() {
String traceId = "traceId";
String rpcId = "rpcId";
RpcInternalContext.getContext().setAttachment("_trace_id", traceId);
RpcInternalContext.getContext().setAttachment("_span_id", rpcId);
RpcResponseCommand command = new RpcResponseCommand();
SofaRpcSerialization sofaRpcSerialization = new SofaRpcSerialization();
boolean exp = false;
try {
sofaRpcSerialization.serializeContent(command);
} catch (SerializationException e) {
exp = true;
Assert.assertTrue(e.getMessage().contains("traceId=" + traceId + ", rpcId=" + rpcId));
}
Assert.assertTrue(exp);
}
Aggregations