/** * 构建rpc错误结果 * * @param errorMsg 错误消息 * @return rpc结果 */ public static SofaResponse buildSofaErrorResponse(String errorMsg) { SofaResponse sofaResponse = new SofaResponse(); sofaResponse.setErrorMsg(errorMsg); return sofaResponse; } }
/** * A->B(当前)->C的场景下,将远程服务端C的结果异步返回给调用者A * * @see SofaResponseCallback#onAppResponse(Object, String, RequestBase) */ @Override public void sendAppResponse(Object appResponse) { checkState(); SofaResponse response = new SofaResponse(); response.setAppResponse(appResponse); sendSofaResponse(response, null); }
@Override protected V getNow() throws ExecutionException { if (cause != null) { // 异常 throw new ExecutionException(cause); } else if (result instanceof SofaResponse) { SofaResponse response = (SofaResponse) result; if (response.isError()) { cause = new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg()); throw new ExecutionException(cause); } else { result = response.getAppResponse(); if (result instanceof Throwable) { throw new ExecutionException((Throwable) result); } else { return (V) result; } } } else { return (V) result; } }
@Override protected SofaResponse run() throws Exception { RpcInternalContext.setContext(rpcInternalContext); RpcInvokeContext.setContext(rpcInvokeContext); SofaResponse sofaResponse = invoker.invoke(request); if (!sofaResponse.isError()) { return sofaResponse; } return getFallback(sofaResponse, null); }
@Override protected V getNow() throws ExecutionException { if (cause != null) { // 异常 throw new ExecutionException(cause); } else if (result instanceof SofaResponse) { SofaResponse response = (SofaResponse) result; if (response.isError()) { cause = new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg()); throw new ExecutionException(cause); } else { result = response.getAppResponse(); if (result instanceof Throwable) { throw new ExecutionException((Throwable) result); } else { return (V) result; } } } else { return (V) result; } }
/** * A->B(当前)->C的场景下,将远程服务端C的业务异常异步返回给调用者A * * @see SofaResponseCallback#onAppException(Throwable, String, RequestBase) */ @Override public void sendAppException(Throwable throwable) { checkState(); SofaResponse response = new SofaResponse(); response.setAppResponse(throwable); sendSofaResponse(response, null); }
/** * 构建rpc错误结果 * * @param errorMsg 错误消息 * @return rpc结果 */ public static SofaResponse buildSofaErrorResponse(String errorMsg) { SofaResponse sofaResponse = new SofaResponse(); sofaResponse.setErrorMsg(errorMsg); return sofaResponse; } }
@Override protected SofaResponse run() throws Exception { RpcInternalContext.setContext(rpcInternalContext); RpcInvokeContext.setContext(rpcInvokeContext); SofaResponse sofaResponse = invoker.invoke(request); if (!sofaResponse.isError()) { return sofaResponse; } return getFallback(sofaResponse, null); }
protected AbstractByteBuf encodeSofaResponse(SofaResponse sofaResponse, Map<String, String> context) throws SofaRpcException { AbstractByteBuf byteBuf; if (sofaResponse.isError()) { // rpc exception:error when body is illegal string byteBuf = encode(sofaResponse.getErrorMsg(), context); } else { //ok: when json can be deserialize correctly. Object appResponse = sofaResponse.getAppResponse(); if (appResponse instanceof Throwable) { // biz exception:error when body is illegal string byteBuf = encode(((Throwable) appResponse).getMessage(), context); } else { byteBuf = encode(appResponse, context); } } return byteBuf; }
/** * A->B(当前)->C的场景下,将远程服务端C的业务异常异步返回给调用者A * * @see SofaResponseCallback#onAppException(Throwable, String, RequestBase) */ @Override public void sendAppException(Throwable throwable) { checkState(); SofaResponse response = new SofaResponse(); response.setAppResponse(throwable); sendSofaResponse(response, null); }
/** * A->B(当前)->C的场景下,将远程服务端C的RPc异常异步返回给调用者A * * @see SofaResponseCallback#onSofaException(SofaRpcException, String, RequestBase) */ @Override public void sendSofaException(SofaRpcException sofaException) { checkState(); SofaResponse response = new SofaResponse(); response.setErrorMsg(sofaException.getMessage()); sendSofaResponse(response, sofaException); }
protected AbstractByteBuf encodeSofaResponse(SofaResponse sofaResponse, Map<String, String> context) throws SofaRpcException { AbstractByteBuf byteBuf; if (sofaResponse.isError()) { // 框架异常:错误则body序列化的是错误字符串 byteBuf = encode(sofaResponse.getErrorMsg(), context); } else { // 正确返回则解析序列化的protobuf返回对象 Object appResponse = sofaResponse.getAppResponse(); if (appResponse instanceof Throwable) { // 业务异常序列化的是错误字符串 byteBuf = encode(((Throwable) appResponse).getMessage(), context); } else { byteBuf = encode(appResponse, context); } } return byteBuf; }
/** * A->B(当前)->C的场景下,将远程服务端C的结果异步返回给调用者A * * @see SofaResponseCallback#onAppResponse(Object, String, RequestBase) */ @Override public void sendAppResponse(Object appResponse) { checkState(); SofaResponse response = new SofaResponse(); response.setAppResponse(appResponse); sendSofaResponse(response, null); }
/** * A->B(当前)->C的场景下,将远程服务端C的RPc异常异步返回给调用者A * * @see SofaResponseCallback#onSofaException(SofaRpcException, String, RequestBase) */ @Override public void sendSofaException(SofaRpcException sofaException) { checkState(); SofaResponse response = new SofaResponse(); response.setErrorMsg(sofaException.getMessage()); sendSofaResponse(response, sofaException); }
protected AbstractByteBuf encodeSofaResponse(SofaResponse sofaResponse, Map<String, String> context) throws SofaRpcException { AbstractByteBuf byteBuf; if (sofaResponse.isError()) { // rpc exception:error when body is illegal string byteBuf = encode(sofaResponse.getErrorMsg(), context); } else { //ok: when json can be deserialize correctly. Object appResponse = sofaResponse.getAppResponse(); if (appResponse instanceof Throwable) { // biz exception:error when body is illegal string byteBuf = encode(((Throwable) appResponse).getMessage(), context); } else { byteBuf = encode(appResponse, context); } } return byteBuf; }
private SofaResponse buildEmptyResponse(SofaRequest request) { SofaResponse response = new SofaResponse(); Method method = request.getMethod(); if (method != null) { response.setAppResponse(ClassUtils.getDefaultPrimitiveValue(method.getReturnType())); } return response; }
protected AbstractByteBuf encodeSofaResponse(SofaResponse sofaResponse, Map<String, String> context) throws SofaRpcException { AbstractByteBuf byteBuf; if (sofaResponse.isError()) { // 框架异常:错误则body序列化的是错误字符串 byteBuf = encode(sofaResponse.getErrorMsg(), context); } else { // 正确返回则解析序列化的protobuf返回对象 Object appResponse = sofaResponse.getAppResponse(); if (appResponse instanceof Throwable) { // 业务异常序列化的是错误字符串 byteBuf = encode(((Throwable) appResponse).getMessage(), context); } else { byteBuf = encode(appResponse, context); } } return byteBuf; }
private SofaResponse buildEmptyResponse(SofaRequest request) { SofaResponse response = new SofaResponse(); Method method = request.getMethod(); if (method != null) { response.setAppResponse(ClassUtils.getDefaultPrimitiveValue(method.getReturnType())); } return response; }