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