Java Code Examples for com.alibaba.dubbo.rpc.Result#getException()

The following examples show how to use com.alibaba.dubbo.rpc.Result#getException() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DubboCodec.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Override
protected void encodeResponseData(Channel channel, ObjectOutput out, Object data, String version) throws IOException {
    Result result = (Result) data;
    // currently, the version value in Response records the version of Request
    boolean attach = Version.isSupportResponseAttatchment(version);
    Throwable th = result.getException();
    if (th == null) {
        Object ret = result.getValue();
        if (ret == null) {
            out.writeByte(attach ? RESPONSE_NULL_VALUE_WITH_ATTACHMENTS : RESPONSE_NULL_VALUE);
        } else {
            out.writeByte(attach ? RESPONSE_VALUE_WITH_ATTACHMENTS : RESPONSE_VALUE);
            out.writeObject(ret);
        }
    } else {
        out.writeByte(attach ? RESPONSE_WITH_EXCEPTION_WITH_ATTACHMENTS : RESPONSE_WITH_EXCEPTION);
        out.writeObject(th);
    }

    if (attach) {
        // returns current version of Response to consumer side.
        result.getAttachments().put(Constants.DUBBO_VERSION_KEY, Version.getProtocolVersion());
        out.writeObject(result.getAttachments());
    }
}
 
Example 2
Source File: DubboSofaTracerFilter.java    From sofa-tracer with Apache License 2.0 6 votes vote down vote up
/**
 * finish tracer under async
 * @param result
 * @param sofaTracerSpan
 * @param invocation
 */
public static void doFinishTracerUnderAsync(Result result, SofaTracerSpan sofaTracerSpan,
                                            Invocation invocation) {
    DubboConsumerSofaTracer dubboConsumerSofaTracer = DubboConsumerSofaTracer
        .getDubboConsumerSofaTracerSingleton();
    // to build tracer instance
    String resultCode = SofaTracerConstant.RESULT_CODE_SUCCESS;
    if (result.hasException()) {
        if (result.getException() instanceof RpcException) {
            resultCode = Integer.toString(((RpcException) result.getException()).getCode());
            sofaTracerSpan.setTag(CommonSpanTags.RESULT_CODE, resultCode);
        } else {
            resultCode = SofaTracerConstant.RESULT_CODE_ERROR;
        }
    }
    // add elapsed time
    appendElapsedTimeTags(invocation, sofaTracerSpan, result, true);
    dubboConsumerSofaTracer.clientReceiveTagFinish(sofaTracerSpan, resultCode);
}
 
Example 3
Source File: DubboCodec.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Override
protected void encodeResponseData(Channel channel, ObjectOutput out, Object data) throws IOException {
    Result result = (Result) data;

    Throwable th = result.getException();
    if (th == null) {
        Object ret = result.getValue();
        if (ret == null) {
            out.writeByte(RESPONSE_NULL_VALUE);
        } else {
            out.writeByte(RESPONSE_VALUE);
            out.writeObject(ret);
        }
    } else {
        out.writeByte(RESPONSE_WITH_EXCEPTION);
        out.writeObject(th);
    }
}
 
Example 4
Source File: DubboCodec.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@Override
protected void encodeResponseData(Channel channel, ObjectOutput out, Object data) throws IOException {
    Result result = (Result) data;

    Throwable th = result.getException();
    if (th == null) {
        Object ret = result.getValue();
        if (ret == null) {
            out.writeByte(RESPONSE_NULL_VALUE);
        } else {
            out.writeByte(RESPONSE_VALUE);
            out.writeObject(ret);
        }
    } else {
        out.writeByte(RESPONSE_WITH_EXCEPTION);
        out.writeObject(th);
    }
}
 
Example 5
Source File: DubboCodec.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
@Override
protected void encodeResponseData(Channel channel, ObjectOutput out, Object data) throws IOException {
    Result result = (Result) data;

    Throwable th = result.getException();
    if (th == null) {
        Object ret = result.getValue();
        if (ret == null) {
            out.writeByte(RESPONSE_NULL_VALUE);
        } else {
            out.writeByte(RESPONSE_VALUE);
            out.writeObject(ret);
        }
    } else {
        out.writeByte(RESPONSE_WITH_EXCEPTION);
        out.writeObject(th);
    }
}
 
Example 6
Source File: DubboCodec.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Override
protected void encodeResponseData(Channel channel, ObjectOutput out, Object data) throws IOException {
    Result result = (Result) data;

    Throwable th = result.getException();
    if (th == null) {
        Object ret = result.getValue();
        if (ret == null) {
            out.writeByte(RESPONSE_NULL_VALUE);
        } else {
            out.writeByte(RESPONSE_VALUE);
            out.writeObject(ret);
        }
    } else {
        out.writeByte(RESPONSE_WITH_EXCEPTION);
        out.writeObject(th);
    }
}
 
Example 7
Source File: DubboCodec.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Override
protected void encodeResponseData(Channel channel, ObjectOutput out, Object data) throws IOException {
    Result result = (Result) data;

    Throwable th = result.getException();
    if (th == null) {
        Object ret = result.getValue();
        if (ret == null) {
            out.writeByte(RESPONSE_NULL_VALUE);
        } else {
            out.writeByte(RESPONSE_VALUE);
            out.writeObject(ret);
        }
    } else {
        out.writeByte(RESPONSE_WITH_EXCEPTION);
        out.writeObject(th);
    }
}
 
Example 8
Source File: CustomExceptionFilter.java    From BigDataPlatform with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException, ValidateException {
    Result result = invoker.invoke(invocation);
    if (!result.hasException()) {
        return result;
    }

    Throwable e = result.getException();
    if (e instanceof ValidateException) {
        throw new ValidateException(e.getMessage());
    } else {
        e.printStackTrace();
        throw new RpcException(wrapperExceptionMessage(e));
    }
}
 
Example 9
Source File: Utils.java    From spring-boot-starter-dubbo with Apache License 2.0 5 votes vote down vote up
protected final static Result decodeException(Result relust) {
	if (relust != null && relust.hasException()) {
		Throwable throwable = relust.getException();
		if (throwable.getClass().equals(Exception.class) && TRANSFORM_EXCEPTION_MESSAGE.equals(throwable.getMessage())) {
			throwable = throwable.getCause();
			if (throwable != null) {
				log.debug("被包装的异常{},解开包装...", throwable.getClass());
				return new RpcResult(throwable);
			}
		}
	}
	return relust;
}
 
Example 10
Source File: Utils.java    From spring-boot-starter-dubbo with Apache License 2.0 5 votes vote down vote up
/**
 * 对服务端的异常进行编码
 * 
 * @param relust
 * @return
 */
protected final static Result encoderException(Result relust) {
	if (relust != null && relust.hasException()) {
		Throwable throwable = relust.getException();
		if (exceptions.contains(throwable.getClass())) {
			log.debug("{}异常被转化为Exception输出,用来通用异常处理....",throwable.getClass());
			return new RpcResult(new Exception(TRANSFORM_EXCEPTION_MESSAGE, throwable));
		}
	}
	return relust;
}
 
Example 11
Source File: DubboInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
    Object ret) throws Throwable {
    Result result = (Result) ret;
    if (result != null && result.getException() != null) {
        dealException(result.getException());
    }

    ContextManager.stopSpan();
    return ret;
}
 
Example 12
Source File: DubboSofaTracerFilter.java    From sofa-tracer with Apache License 2.0 4 votes vote down vote up
/**
 * rpc client handler
 * @param rpcContext
 * @param invoker
 * @param invocation
 * @return
 */
private Result doClientFilter(RpcContext rpcContext, Invoker<?> invoker, Invocation invocation) {
    // to build tracer instance
    if (dubboConsumerSofaTracer == null) {
        this.dubboConsumerSofaTracer = DubboConsumerSofaTracer
            .getDubboConsumerSofaTracerSingleton();
    }
    // get methodName
    String methodName = rpcContext.getMethodName();
    // get service interface
    String service = invoker.getInterface().getSimpleName();
    // build a dubbo rpc span
    SofaTracerSpan sofaTracerSpan = dubboConsumerSofaTracer.clientSend(service + "#"
                                                                       + methodName);
    // set tags to span
    appendRpcClientSpanTags(invoker, sofaTracerSpan);
    // do serialized and then transparent transmission to the rpc server
    String serializedSpanContext = sofaTracerSpan.getSofaTracerSpanContext()
        .serializeSpanContext();
    //put into attachments
    invocation.getAttachments().put(CommonSpanTags.RPC_TRACE_NAME, serializedSpanContext);

    boolean isOneWay = false, deferFinish = false;

    // check invoke type
    boolean isAsync = RpcUtils.isAsync(invoker.getUrl(), invocation);

    // set invoke type tag
    if (isAsync) {
        sofaTracerSpan.setTag(CommonSpanTags.INVOKE_TYPE, "future");
    } else {
        isOneWay = RpcUtils.isOneway(invoker.getUrl(), invocation);
        if (isOneWay) {
            sofaTracerSpan.setTag(CommonSpanTags.INVOKE_TYPE, "oneway");
        } else {
            sofaTracerSpan.setTag(CommonSpanTags.INVOKE_TYPE, "sync");
        }
    }

    Result result;
    Throwable exception = null;
    String resultCode = SofaTracerConstant.RESULT_CODE_SUCCESS;
    try {
        // do invoke
        result = invoker.invoke(invocation);
        if (result.hasException()) {
            exception = result.getException();
        }
        // the case on async client invocation
        Future<Object> future = rpcContext.getFuture();
        if (future instanceof FutureAdapter) {
            deferFinish = ensureSpanFinishes(future, invocation, invoker);
        }
        return result;
    } catch (RpcException e) {
        exception = e;
        throw e;
    } catch (Throwable t) {
        exception = t;
        throw new RpcException(t);
    } finally {
        if (exception != null) {
            // finish span on exception, delay to clear tl in handleError
            handleError(exception, null);
        } else {
            // sync invoke
            if (isOneWay || !deferFinish) {
                dubboConsumerSofaTracer.clientReceive(resultCode);
            } else {
                // to clean SofaTraceContext
                SofaTraceContext sofaTraceContext = SofaTraceContextHolder
                    .getSofaTraceContext();
                SofaTracerSpan clientSpan = sofaTraceContext.pop();
                if (clientSpan != null) {
                    // Record client send event
                    sofaTracerSpan.log(LogData.CLIENT_SEND_EVENT_VALUE);
                }
                // cache the current span
                TracerSpanMap.put(getTracerSpanMapKey(invoker), sofaTracerSpan);
                if (clientSpan != null && clientSpan.getParentSofaTracerSpan() != null) {
                    //restore parent
                    sofaTraceContext.push(clientSpan.getParentSofaTracerSpan());
                }
            }
        }
    }
}
 
Example 13
Source File: DubboSofaTracerFilter.java    From sofa-tracer with Apache License 2.0 4 votes vote down vote up
/**
 * rpc client handler
 * @param invoker
 * @param invocation
 * @return
 */
private Result doServerFilter(Invoker<?> invoker, Invocation invocation) {
    if (dubboProviderSofaTracer == null) {
        this.dubboProviderSofaTracer = DubboProviderSofaTracer
            .getDubboProviderSofaTracerSingleton();
    }
    SofaTracerSpan sofaTracerSpan = serverReceived(invocation);
    appendRpcServerSpanTags(invoker, sofaTracerSpan);
    Result result;
    Throwable exception = null;
    try {
        result = invoker.invoke(invocation);
        if (result == null) {
            return null;
        } else {
            appendElapsedTimeTags(invocation, sofaTracerSpan, result, false);
        }
        if (result.hasException()) {
            exception = result.getException();
        }
        return result;
    } catch (RpcException e) {
        exception = e;
        throw e;
    } catch (Throwable t) {
        exception = t;
        throw new RpcException(t);
    } finally {
        String resultCode = SofaTracerConstant.RESULT_CODE_SUCCESS;
        if (exception != null) {
            if (exception instanceof RpcException) {
                sofaTracerSpan.setTag(Tags.ERROR.getKey(), exception.getMessage());
                RpcException rpcException = (RpcException) exception;
                if (rpcException.isBiz()) {
                    resultCode = String.valueOf(rpcException.getCode());
                }
            } else {
                resultCode = SofaTracerConstant.RESULT_CODE_ERROR;
            }
        }
        dubboProviderSofaTracer.serverSend(resultCode);
    }
}
 
Example 14
Source File: CicadaDubboFilter.java    From cicada with MIT License 4 votes vote down vote up
@SuppressWarnings({"PMD.OnlyOneReturn", "PMD.OnlyOneReturn"})
public Result invoke(final Invoker<?> invoker, final Invocation invocation) throws RpcException {
  if (tracer == null) {
    return invoker.invoke(invocation);
  }
  final long startTime = System.currentTimeMillis();
  final RpcContext context = RpcContext.getContext();

  // 传入参数,暂不做处理
  // Object[] arguments = context.getArguments();
  // for (Object argument : arguments) {
  // LOGGER.error("arg:" + argument);
  // }

  final String localIp = IpUtils.getRealIpWithStaticCache();
  final int localPort = context.getLocalPort();
  final Endpoint endpoint = new Endpoint(localIp, localPort);

  final URL url = context.getUrl();
  final String appName = url.getParameter("application");
  final String serviceName = url.getServiceInterface();
  final String methodName = context.getMethodName();

  final boolean isConsumerSide = context.isConsumerSide();
  Span span = null;
  try {
    if (isConsumerSide) { // 是否是消费者
      final Span parentSpan = tracer.getParentSpan();
      if (parentSpan == null) { // 为rootSpan
        // 生成root Span
        span = tracer.newSpan(appName, serviceName, methodName);
      } else {
        span = tracer.newSpan(appName, serviceName, methodName, parentSpan);
      }
    } else if (context.isProviderSide()) {
      final String traceId = invocation.getAttachment(TracerUtils.TRACE_ID);
      final String parentId = invocation.getAttachment(TracerUtils.PARENT_SPAN_ID);
      final String spanId = invocation.getAttachment(TracerUtils.SPAN_ID);
      final boolean sample = traceId != null;
      span = tracer.genSpan(appName, serviceName, methodName, traceId, parentId, spanId, sample);
    } else {
      LOGGER.error("[" + url + "] [notConsumerNorProvider]");
      return invoker.invoke(invocation);
    }

    invokerBefore(invocation, span, endpoint, startTime);
    final Result result = invoker.invoke(invocation);
    final Throwable throwable = result.getException();
    if (throwable != null && !isConsumerSide) {
      span.addException(serviceName, methodName, throwable, endpoint);
    }

    // 返回值
    // Object resultValue = result.getValue();
    // LOGGER.error("return:" + JSON.toJSONString(resultValue));
    return result;
  } catch (final RpcException ex) {
    if (span != null) {
      span.addException(serviceName, methodName, ex, endpoint);
    }
    throw ex;
  } finally {
    if (span != null) {
      final long end = System.currentTimeMillis();
      invokerAfter(endpoint, span, end, isConsumerSide);// 调用后记录annotation
    }
  }
}