Search in sources :

Example 1 with MotanBizException

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;
}
Also used : Method(java.lang.reflect.Method) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) MotanBizException(com.weibo.api.motan.exception.MotanBizException) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) MotanBizException(com.weibo.api.motan.exception.MotanBizException)

Example 2 with MotanBizException

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;
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) MotanBizException(com.weibo.api.motan.exception.MotanBizException)

Example 3 with MotanBizException

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"));
    }
}
Also used : Expectations(org.jmock.Expectations) ArrayList(java.util.ArrayList) Cluster(com.weibo.api.motan.cluster.Cluster) Method(java.lang.reflect.Method) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) MotanBizException(com.weibo.api.motan.exception.MotanBizException) MotanBizException(com.weibo.api.motan.exception.MotanBizException) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) Test(org.junit.Test)

Example 4 with MotanBizException

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;
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) MotanBizException(com.weibo.api.motan.exception.MotanBizException)

Example 5 with MotanBizException

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());
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) YarResponse(com.weibo.yar.YarResponse) DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) Response(com.weibo.api.motan.rpc.Response) YarResponse(com.weibo.yar.YarResponse) MotanBizException(com.weibo.api.motan.exception.MotanBizException) Test(org.junit.Test)

Aggregations

MotanBizException (com.weibo.api.motan.exception.MotanBizException)8 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)3 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)3 Method (java.lang.reflect.Method)3 Response (com.weibo.api.motan.rpc.Response)2 YarResponse (com.weibo.yar.YarResponse)2 Expectations (org.jmock.Expectations)2 Test (org.junit.Test)2 Cluster (com.weibo.api.motan.cluster.Cluster)1 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)1 IHello (com.weibo.api.motan.protocol.example.IHello)1 RegistryService (com.weibo.api.motan.registry.RegistryService)1 Request (com.weibo.api.motan.rpc.Request)1 URL (com.weibo.api.motan.rpc.URL)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1