Skip to content

Ability to propagate cause when using StatusProto.toStatusException #10900

@jasnross

Description

@jasnross

Is your feature request related to a problem?

When converting a com.google.rpc.Status to an io.grpc.StatusException I can't find a way to propagate a cause with the resulting StatusException.

Currently StatusProto.toStatusException is defined as:

  public static StatusException toStatusException(com.google.rpc.Status statusProto) {
    return toStatus(statusProto).asException(toMetadata(statusProto));
  }

However, using this method there doesn't seem to be any way of providing a cause.

Describe the solution you'd like

My ideal would be to make StatusProto.toStatus public, which would allow creating the status and calling asException something callers could do themselves. This would also be helpful in scenarios where callers require a Status instead of a StatusException.

  private static Status toStatus(com.google.rpc.Status statusProto) {
    Status status = Status.fromCodeValue(statusProto.getCode());
    checkArgument(status.getCode().value() == statusProto.getCode(), "invalid status code");
    return status.withDescription(statusProto.getMessage());
  }

Describe alternatives you've considered

It feels a little clunky, but I'd be open to having an extra parameter on toStatusException for the cause, or another solution depending on the preferences of grpc-java maintainers.

I've also tried calling initCause on the resulting StatusException, but that isn't possible because the cause is set to null in the asException code, so initCause throws an IllegalArgumentException.

Additional context

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions