use of com.weibo.api.motan.exception.MotanBizException in project motan by weibocom.
the class DefaultProvider method invoke.
@Override
public Response invoke(Request request) {
DefaultResponse response = new DefaultResponse();
Method method = lookupMethod(request.getMethodName(), request.getParamtersDesc());
if (method == null) {
MotanServiceException exception = new MotanServiceException("Service method not exist: " + request.getInterfaceName() + "." + request.getMethodName() + "(" + request.getParamtersDesc() + ")", MotanErrorMsgConstant.SERVICE_UNFOUND, false);
response.setException(exception);
return response;
}
boolean defaultThrowExceptionStack = URLParamType.transExceptionStack.getBooleanValue();
try {
Object value = method.invoke(proxyImpl, request.getArguments());
response.setValue(value);
} catch (Exception e) {
if (e.getCause() != null) {
response.setException(new MotanBizException("provider call process error", e.getCause()));
} else {
response.setException(new MotanBizException("provider call process error", e));
}
// not print stack in error log when exception declared in method
boolean logException = true;
for (Class<?> clazz : method.getExceptionTypes()) {
if (clazz.isInstance(response.getException().getCause())) {
logException = false;
defaultThrowExceptionStack = false;
break;
}
}
if (logException) {
LoggerUtil.error("Exception caught when during method invocation. request:" + request.toString(), e);
} else {
LoggerUtil.info("Exception caught when during method invocation. request:" + request.toString() + ", exception:" + response.getException().getCause().toString());
}
} catch (Throwable t) {
// 如果服务发生Error,将Error转化为Exception,防止拖垮调用方
if (t.getCause() != null) {
response.setException(new MotanServiceException("provider has encountered a fatal error!", t.getCause()));
} else {
response.setException(new MotanServiceException("provider has encountered a fatal error!", t));
}
// 对于Throwable,也记录日志
LoggerUtil.error("Exception caught when during method invocation. request:" + request.toString(), t);
}
if (response.getException() != null) {
// 是否传输业务异常栈
boolean transExceptionStack = this.url.getBooleanParameter(URLParamType.transExceptionStack.getName(), defaultThrowExceptionStack);
if (!transExceptionStack) {
// 不传输业务异常栈
ExceptionUtil.setMockStackTrace(response.getException().getCause());
}
}
response.setAttachments(request.getAttachments());
return response;
}
use of com.weibo.api.motan.exception.MotanBizException in project motan by weibocom.
the class DefaultRpcCodecTest method exceptionResponseSize.
/**
* 不带参数的Request大小
*/
private static byte[] exceptionResponseSize(DefaultRpcCodec codec, Channel channel) throws Exception {
DefaultResponse response = new DefaultResponse();
response.setRequestId(System.currentTimeMillis());
response.setProcessTime(System.currentTimeMillis());
response.setException(new MotanBizException(new RuntimeException("hi, boy, i am biz exception.")));
byte[] bytes = codec.encode(channel, response);
return bytes;
}
use of com.weibo.api.motan.exception.MotanBizException in project motan by weibocom.
the class RefererInvocationHandlerTest method testInvokeException.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testInvokeException() throws Throwable {
final Cluster cluster = mockery.mock(Cluster.class);
final URL u = new URL("motan", "local", 80, "test");
u.addParameter(URLParamType.nodeType.getName(), MotanConstants.NODE_TYPE_REFERER);
mockery.checking(new Expectations() {
{
one(cluster).call(with(any(Request.class)));
will(throwException(new MotanBizException("just test", new StackOverflowError())));
allowing(cluster).getUrl();
will(returnValue(u));
}
});
List<Cluster> clus = new ArrayList<Cluster>();
clus.add(cluster);
RefererInvocationHandler handler = new RefererInvocationHandler(String.class, clus);
Method[] methods = String.class.getMethods();
try {
handler.invoke(null, methods[1], null);
} catch (Exception e) {
assertTrue(e instanceof MotanServiceException);
assertTrue(e.getMessage().contains("StackOverflowError"));
}
}
use of com.weibo.api.motan.exception.MotanBizException in project motan by weibocom.
the class YarProtocolUtil method convert.
public static Response convert(YarResponse yarResponse) {
DefaultResponse response = new DefaultResponse();
response.setRequestId(yarResponse.getId());
response.setValue(yarResponse.getRet());
if (StringUtils.isNotBlank(yarResponse.getError())) {
response.setException(new MotanBizException(yarResponse.getError()));
}
return response;
}
use of com.weibo.api.motan.exception.MotanBizException in project motan by weibocom.
the class YarProtocolUtilTest method testConvertYarResponse.
@Test
public void testConvertYarResponse() {
DefaultResponse response = new DefaultResponse();
response.setRequestId(456);
response.setValue("stringValue");
YarResponse yarResponse = YarProtocolUtil.convert(response, "JSON");
assertNotNull(yarResponse);
Response newResponse = YarProtocolUtil.convert(yarResponse);
assertEquals(response.getRequestId(), newResponse.getRequestId());
assertEquals(response.getValue(), newResponse.getValue());
response.setException(new RuntimeException("test exception"));
yarResponse = YarProtocolUtil.convert(response, "JSON");
assertNotNull(yarResponse);
newResponse = YarProtocolUtil.convert(yarResponse);
assertEquals(response.getRequestId(), newResponse.getRequestId());
// yarresponse的异常会转为motan业务异常
assertEquals(new MotanBizException(response.getException().getMessage()).getMessage(), newResponse.getException().getMessage());
}
Aggregations