Java Code Examples for com.alipay.sofa.rpc.core.response.SofaResponse#setErrorMsg()

The following examples show how to use com.alipay.sofa.rpc.core.response.SofaResponse#setErrorMsg() . 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: RestLookoutAdapter.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
public static void sendRestServerSendEvent(RestServerSendEvent restServerSendEvent) {
    //this is special for rest
    if (EventBus.isEnable(ServerSendEvent.class)) {
        SofaRequest request = new SofaRequest();

        String appName = (String) RpcRuntimeContext.get(RpcRuntimeContext.KEY_APPNAME);
        request.setTargetAppName(appName);
        request.addRequestProp(RemotingConstants.HEAD_APP_NAME, restServerSendEvent.getRequest().getHttpHeaders()
            .getHeaderString(RemotingConstants.HEAD_APP_NAME));
        RpcInternalContext context = RpcInternalContext.getContext();
        request.setTargetServiceUniqueName((String) context.getAttachment(INTERNAL_KEY_PREFIX +
            RestConstants.REST_SERVICE_KEY));

        request.setMethodName((String) context.getAttachment(INTERNAL_KEY_PREFIX +
            RestConstants.REST_METHODNAME_KEY));
        request.addRequestProp(RemotingConstants.HEAD_PROTOCOL, RpcConstants.PROTOCOL_TYPE_REST);
        request.setInvokeType(RpcConstants.INVOKER_TYPE_SYNC);
        SofaResponse response = new SofaResponse();

        if (restServerSendEvent.getThrowable() != null) {
            response.setErrorMsg(restServerSendEvent.getThrowable().getMessage());
        }
        final ServerSendEvent event = new ServerSendEvent(request, response, restServerSendEvent.getThrowable());
        EventBus.post(event);
    }
}
 
Example 2
Source File: ComplexDataGenerator.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
public SofaResponse generateSofaResponse() {
    SofaResponse response = new SofaResponse();

    response.setErrorMsg("just_error");
    response.setAppResponse(generatePerson_6());

    return response;
}
 
Example 3
Source File: BoltSendableResponseCallback.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 4
Source File: ProtobufSerializer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
private void decodeSofaResponse(AbstractByteBuf data, SofaResponse sofaResponse, Map<String, String> head) {
    if (head == null) {
        throw buildDeserializeError("head is null!");
    }
    String targetService = head.remove(RemotingConstants.HEAD_TARGET_SERVICE);
    if (targetService == null) {
        throw buildDeserializeError("HEAD_TARGET_SERVICE is null");
    }
    String methodName = head.remove(RemotingConstants.HEAD_METHOD_NAME);
    if (methodName == null) {
        throw buildDeserializeError("HEAD_METHOD_NAME is null");
    }

    boolean isError = false;
    if (StringUtils.TRUE.equals(head.remove(RemotingConstants.HEAD_RESPONSE_ERROR))) {
        isError = true;
    }
    if (!head.isEmpty()) {
        sofaResponse.setResponseProps(head);
    }
    if (isError) {
        String errorMessage = (String) decode(data, String.class, head);
        sofaResponse.setErrorMsg(errorMessage);
    } else {
        // 根据接口+方法名找到参数类型
        Class responseClass = protobufHelper.getResClass(targetService, methodName);
        Object pbRes = decode(data, responseClass, head);
        sofaResponse.setAppResponse(pbRes);
    }
}
 
Example 5
Source File: SofaResponseHessianSerializer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public void decodeObjectByTemplate(AbstractByteBuf data, Map<String, String> context, SofaResponse template)
    throws SofaRpcException {
    try {
        UnsafeByteArrayInputStream inputStream = new UnsafeByteArrayInputStream(data.array());
        Hessian2Input input = new Hessian2Input(inputStream);
        // 根据SerializeType信息决定序列化器
        boolean genericSerialize = context != null && isGenericResponse(
            context.get(RemotingConstants.HEAD_GENERIC_TYPE));
        if (genericSerialize) {
            input.setSerializerFactory(genericSerializerFactory);
            GenericObject genericObject = (GenericObject) input.readObject();
            template.setErrorMsg((String) genericObject.getField("errorMsg"));
            template.setAppResponse(genericObject.getField("appResponse"));
            template.setResponseProps((Map<String, String>) genericObject.getField("responseProps"));
        } else {
            input.setSerializerFactory(serializerFactory);
            SofaResponse tmp = (SofaResponse) input.readObject();
            // copy values to template
            template.setErrorMsg(tmp.getErrorMsg());
            template.setAppResponse(tmp.getAppResponse());
            template.setResponseProps(tmp.getResponseProps());
        }
        input.close();
    } catch (IOException e) {
        throw buildDeserializeError(e.getMessage(), e);
    }
}
 
Example 6
Source File: SofaResponseHessianSerializer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public SofaResponse decodeObject(AbstractByteBuf data, Map<String, String> context) throws SofaRpcException {
    try {
        UnsafeByteArrayInputStream inputStream = new UnsafeByteArrayInputStream(data.array());
        Hessian2Input input = new Hessian2Input(inputStream);
        // 根据SerializeType信息决定序列化器
        Object object;
        boolean genericSerialize = context != null && isGenericResponse(
            context.get(RemotingConstants.HEAD_GENERIC_TYPE));
        if (genericSerialize) {
            input.setSerializerFactory(genericSerializerFactory);
            GenericObject genericObject = (GenericObject) input.readObject();
            SofaResponse sofaResponse = new SofaResponse();
            sofaResponse.setErrorMsg((String) genericObject.getField("errorMsg"));
            sofaResponse.setAppResponse(genericObject.getField("appResponse"));
            sofaResponse.setResponseProps((Map<String, String>) genericObject.getField("responseProps"));
            object = sofaResponse;
        } else {
            input.setSerializerFactory(serializerFactory);
            object = input.readObject();
        }
        input.close();
        return (SofaResponse) object;
    } catch (IOException e) {
        throw buildDeserializeError(e.getMessage(), e);
    }
}
 
Example 7
Source File: JacksonSerializer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
private void decodeSofaResponse(AbstractByteBuf data, SofaResponse sofaResponse, Map<String, String> head) {
    if (head == null) {
        throw buildDeserializeError("head is null!");
    }
    String targetService = head.remove(RemotingConstants.HEAD_TARGET_SERVICE);
    if (targetService == null) {
        throw buildDeserializeError("HEAD_TARGET_SERVICE is null");
    }
    String methodName = head.remove(RemotingConstants.HEAD_METHOD_NAME);
    if (methodName == null) {
        throw buildDeserializeError("HEAD_METHOD_NAME is null");
    }

    boolean isError = false;
    if (StringUtils.TRUE.equals(head.remove(RemotingConstants.HEAD_RESPONSE_ERROR))) {
        isError = true;
    }
    if (!head.isEmpty()) {
        sofaResponse.setResponseProps(head);
    }
    if (isError) {
        String errorMessage = (String) decode(data, String.class, head);
        sofaResponse.setErrorMsg(errorMessage);
    } else {
        // according interface and method name to find paramter types
        JavaType respType = jacksonHelper.getResClass(targetService, methodName);
        Object result;
        try {
            result = mapper.readValue(data.array(), respType);
        } catch (IOException e) {
            throw buildDeserializeError(e.getMessage());
        }
        sofaResponse.setAppResponse(result);
    }
}
 
Example 8
Source File: MsgPackSerializer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
private void decodeSofaResponse(AbstractByteBuf data, SofaResponse sofaResponse, Map<String, String> head) {
    if (head == null) {
        throw buildDeserializeError("head is null!");
    }
    String targetService = head.remove(RemotingConstants.HEAD_TARGET_SERVICE);
    if (targetService == null) {
        throw buildDeserializeError("HEAD_TARGET_SERVICE is null");
    }
    String methodName = head.remove(RemotingConstants.HEAD_METHOD_NAME);
    if (methodName == null) {
        throw buildDeserializeError("HEAD_METHOD_NAME is null");
    }

    boolean isError = false;
    if (StringUtils.TRUE.equals(head.remove(RemotingConstants.HEAD_RESPONSE_ERROR))) {
        isError = true;
    }
    if (!head.isEmpty()) {
        sofaResponse.setResponseProps(head);
    }
    if (isError) {
        String errorMessage = (String) decode(data, String.class, head);
        sofaResponse.setErrorMsg(errorMessage);
    } else {
        // according interface and method name to find paramter types
        Class responseClass = helper.getResClass(targetService, methodName);
        Object pbRes = decode(data, responseClass, head);
        sofaResponse.setAppResponse(pbRes);
    }
}
 
Example 9
Source File: ProtostuffSerializer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
private void decodeSofaResponse(AbstractByteBuf data, SofaResponse sofaResponse, Map<String, String> head) {
    if (head == null) {
        throw buildDeserializeError("head is null!");
    }
    String targetService = head.remove(RemotingConstants.HEAD_TARGET_SERVICE);
    if (targetService == null) {
        throw buildDeserializeError("HEAD_TARGET_SERVICE is null");
    }
    String methodName = head.remove(RemotingConstants.HEAD_METHOD_NAME);
    if (methodName == null) {
        throw buildDeserializeError("HEAD_METHOD_NAME is null");
    }

    boolean isError = false;
    if (StringUtils.TRUE.equals(head.remove(RemotingConstants.HEAD_RESPONSE_ERROR))) {
        isError = true;
    }
    if (!head.isEmpty()) {
        sofaResponse.setResponseProps(head);
    }
    if (isError) {
        String errorMessage = (String) decode(data, String.class, head);
        sofaResponse.setErrorMsg(errorMessage);
    } else {
        // 根据接口+方法名找到参数类型
        Class responseClass = protostuffHelper.getResClass(targetService, methodName);
        Object pbRes = decode(data, responseClass, head);
        sofaResponse.setAppResponse(pbRes);
    }
}
 
Example 10
Source File: MessageBuilder.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 构建rpc错误结果
 *
 * @param errorMsg 错误消息
 * @return rpc结果
 */
public static SofaResponse buildSofaErrorResponse(String errorMsg) {
    SofaResponse sofaResponse = new SofaResponse();
    sofaResponse.setErrorMsg(errorMsg);

    return sofaResponse;
}
 
Example 11
Source File: RestTracerAdapter.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
/**
 * 适配服务端serverSend
 */
public static void serverSend(NettyHttpResponse response, Throwable throwable) {
    try {
        SofaRequest sofaRequest = new SofaRequest();
        SofaResponse sofaResponse = new SofaResponse();

        if (response == null) {
            sofaResponse.setErrorMsg("rest path ends with /favicon.ico");
        } else if (throwable != null) {
            if (response.getStatus() == 500) {
                sofaResponse.setAppResponse(throwable);
            } else {
                sofaResponse.setErrorMsg(throwable.getMessage());
            }

            Object method = RpcInternalContext.getContext().getAttachment(METHOD_TYPE_STRING);
            if (method != null) {
                Class[] parameterTypes = ((Method) method).getParameterTypes();
                String[] methodTypeString = new String[parameterTypes.length];
                for (int i = 0; i < methodTypeString.length; i++) {
                    methodTypeString[i] = (parameterTypes[i].getName());
                }
                sofaRequest.setMethodArgSigs(methodTypeString);
            }
        }

        SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
        SofaTracerSpan serverSpan = sofaTraceContext.getCurrentSpan();

        RpcInternalContext context = RpcInternalContext.getContext();

        if (serverSpan != null) {
            serverSpan.setTag(RpcSpanTags.SERVER_BIZ_TIME,
                (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE));
        }

        RestBaggageItemsHandler.encodeBaggageItemsToResponse(response, sofaResponse);
        Tracers.serverSend(sofaRequest, sofaResponse, null);
    } catch (Throwable t) {
        if (LOGGER.isWarnEnabled()) {
            LOGGER.warn("the process of rest tracer server send occur error ", t);
        }
    }
}