@Override
    public Message newResponse(final Message parent, final int code, final String fmt, final Object... args) {
        final RpcRequests.ErrorResponse.Builder eBuilder = RpcRequests.ErrorResponse.newBuilder();
        eBuilder.setErrorCode(code);
        if (fmt != null) {
            eBuilder.setErrorMsg(String.format(fmt, args));
        }

        if (parent == null || parent instanceof RpcRequests.ErrorResponse) {
            return eBuilder.build();
        }

        final Descriptors.FieldDescriptor errFd = parent //
            .getDescriptorForType() //
            .findFieldByNumber(ERROR_RESPONSE_NUM);
        Requires.requireNonNull(errFd, "errFd");
        final Message.Builder builder = parent.toBuilder();
        for (final Descriptors.FieldDescriptor fd : parent.getDescriptorForType().getFields()) {
            builder.setField(fd, parent.getField(fd));
        }
        return builder //
            .setField(errFd, eBuilder.build()) //
            .build();
    }
Helper to create error response for GRPC implementation.

@author jiachun.fjc
jraft-extension/rpc-grpc-impl/src/main/java/com/alipay/sofa/jraft/rpc/impl/GrpcResponseFactory.java