use of com.weibo.api.motan.exception.MotanBizException in project motan by weibocom.
the class YarProtocolUtil method convert.
public static YarResponse convert(Response response, String packagerName) {
YarResponse yarResponse = new YarResponse();
yarResponse.setId(response.getRequestId());
yarResponse.setPackagerName(packagerName);
if (response.getException() != null) {
if (response.getException() instanceof MotanBizException) {
yarResponse.setError(response.getException().getCause().getMessage());
} else {
yarResponse.setError(response.getException().getMessage());
}
} else {
yarResponse.setRet(response.getValue());
}
return yarResponse;
}
use of com.weibo.api.motan.exception.MotanBizException in project motan by weibocom.
the class ServiceMockFilter method filter.
@Override
public Response filter(Caller<?> caller, Request request) {
// Do nothing when mock is empty.
String mockServiceName = caller.getUrl().getParameter(URLParamType.mock.getName());
if (StringUtils.isEmpty(mockServiceName) || "false".equals(mockServiceName)) {
return caller.call(request);
}
MockInfo info = getServiceStat(caller.getUrl());
DefaultResponse response = new DefaultResponse();
if (mockServiceName.startsWith(RETURN_PREFIX)) {
String value = mockServiceName.substring(RETURN_PREFIX.length());
try {
info.callNum.addAndGet(1);
long sleepTime = caclSleepTime(info);
Thread.sleep(sleepTime);
response.setValue(parseMockValue(value));
} catch (RuntimeException e) {
if (e.getCause() != null) {
response.setException(new MotanBizException("mock service call process error", e.getCause()));
} else {
response.setException(new MotanBizException("mock service call process error", e));
}
} catch (Exception e) {
throw new IllegalStateException("Illegal mock json value in <motan:service ... mock=\"" + mockServiceName + "\" />");
}
} else {
try {
Class<?> mockClass = isDefault(mockServiceName) ? ReflectUtil.forName(caller.getInterface().getName() + "Mock") : ReflectUtil.forName(mockServiceName);
if (!caller.getInterface().isAssignableFrom(mockClass)) {
throw new MotanFrameworkException("The mock implemention class " + mockClass.getName() + " not implement interface " + caller.getInterface().getName());
}
try {
mockClass.getConstructor();
} catch (NoSuchMethodException e) {
throw new IllegalStateException("No such empty constructor \"public " + mockClass.getSimpleName() + "()\" in mock implemention class " + mockClass.getName());
}
String methodDesc = ReflectUtil.getMethodDesc(request.getMethodName(), request.getParamtersDesc());
Method[] methods = mockClass.getMethods();
boolean invoke = false;
for (Method method : methods) {
if (methodDesc.equals(ReflectUtil.getMethodDesc(method))) {
Object value = invoke(mockClass.newInstance(), method, request.getArguments(), info);
response.setValue(value);
invoke = true;
break;
}
}
if (!invoke) {
throw new MotanFrameworkException("Mock method is not found." + methodDesc);
}
} catch (ClassNotFoundException e) {
throw new MotanFrameworkException("Mock service is not found." + (isDefault(mockServiceName) ? caller.getInterface().getName() + "Mock" : mockServiceName));
} catch (Exception e) {
if (e.getCause() != null) {
response.setException(new MotanBizException("mock service call process error", e.getCause()));
} else {
response.setException(new MotanBizException("mock service call process error", e));
}
}
}
return response;
}
use of com.weibo.api.motan.exception.MotanBizException in project motan by weibocom.
the class AccessStatisticFilterTest method testFilter.
@SuppressWarnings("unchecked")
public void testFilter() {
final Request request = mockery.mock(Request.class);
final Response response = mockery.mock(Response.class);
final Caller<IHello> caller = mockery.mock(Caller.class);
final URL url = new URL(MotanConstants.PROTOCOL_MOTAN, NetUtils.getLocalAddress().getHostAddress(), 0, RegistryService.class.getName());
final Map<String, String> attachments = new HashMap<String, String>();
attachments.put(URLParamType.host.getName(), URLParamType.host.getValue());
attachments.put(URLParamType.application.getName(), URLParamType.application.getValue());
attachments.put(URLParamType.module.getName(), URLParamType.module.getValue());
// TODO 优化重复代码
mockery.checking(new Expectations() {
{
oneOf(caller).call(request);
will(returnValue(response));
atLeast(2).of(caller).getUrl();
will(returnValue(url));
oneOf(response).getException();
will(returnValue(null));
oneOf(response).getProcessTime();
will(returnValue(1000L));
allowing(request).getInterfaceName();
will(returnValue(IHello.class.getName()));
allowing(request).getParamtersDesc();
will(returnValue("mock_param_desc"));
allowing(request).getMethodName();
will(returnValue("mock_method_name"));
atLeast(1).of(request).getAttachments();
will(returnValue(attachments));
}
});
assertEquals(response, accessStatisticFilter.filter(caller, request));
mockery.checking(new Expectations() {
{
oneOf(caller).call(request);
will(returnValue(null));
oneOf(caller).getUrl();
will(returnValue(url));
allowing(request).getInterfaceName();
will(returnValue(IHello.class.getName()));
allowing(request).getParamtersDesc();
will(returnValue("mock_param_desc"));
allowing(request).getMethodName();
will(returnValue("mock_method_name"));
}
});
assertNull(accessStatisticFilter.filter(caller, request));
mockery.checking(new Expectations() {
{
oneOf(caller).call(request);
will(returnValue(response));
oneOf(caller).getUrl();
will(returnValue(url));
atMost(2).of(response).getException();
will(returnValue(new MotanBizException()));
oneOf(response).getProcessTime();
will(returnValue(1000L));
allowing(request).getInterfaceName();
will(returnValue(IHello.class.getName()));
allowing(request).getParamtersDesc();
will(returnValue("mock_param_desc"));
allowing(request).getMethodName();
will(returnValue("mock_method_name"));
}
});
assertEquals(response, accessStatisticFilter.filter(caller, request));
mockery.checking(new Expectations() {
{
oneOf(caller).call(request);
will(returnValue(response));
oneOf(caller).getUrl();
will(returnValue(url));
atMost(2).of(response).getException();
will(returnValue(new MotanServiceException()));
oneOf(response).getProcessTime();
will(returnValue(1000L));
allowing(request).getInterfaceName();
will(returnValue(IHello.class.getName()));
allowing(request).getParamtersDesc();
will(returnValue("mock_param_desc"));
allowing(request).getMethodName();
will(returnValue("mock_method_name"));
}
});
assertEquals(response, accessStatisticFilter.filter(caller, request));
}
Aggregations