Skip to content

Commit

Permalink
StatusException/StatusRuntimeException hide stack trace in a simpler …
Browse files Browse the repository at this point in the history
…way (#11064)
  • Loading branch information
panchenko authored Apr 1, 2024
1 parent 0866e71 commit e36f099
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 29 deletions.
16 changes: 2 additions & 14 deletions api/src/main/java/io/grpc/StatusException.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public class StatusException extends Exception {
private static final long serialVersionUID = -660954903976144640L;
private final Status status;
private final Metadata trailers;
private final boolean fillInStackTrace;

/**
* Constructs an exception with both a status. See also {@link Status#asException()}.
Expand All @@ -49,21 +48,10 @@ public StatusException(Status status, @Nullable Metadata trailers) {
}

StatusException(Status status, @Nullable Metadata trailers, boolean fillInStackTrace) {
super(Status.formatThrowableMessage(status), status.getCause());
super(Status.formatThrowableMessage(status), status.getCause(),
/* enableSuppression */ true, /* writableStackTrace */fillInStackTrace);
this.status = status;
this.trailers = trailers;
this.fillInStackTrace = fillInStackTrace;
fillInStackTrace();
}

@Override
public synchronized Throwable fillInStackTrace() {
// Let's observe final variables in two states! This works because Throwable will invoke this
// method before fillInStackTrace is set, thus doing nothing. After the constructor has set
// fillInStackTrace, this method will properly fill it in. Additionally, sub classes may call
// this normally, because fillInStackTrace will either be set, or this method will be
// overriden.
return fillInStackTrace ? super.fillInStackTrace() : this;
}

/**
Expand Down
17 changes: 2 additions & 15 deletions api/src/main/java/io/grpc/StatusRuntimeException.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public class StatusRuntimeException extends RuntimeException {
private final Status status;
private final Metadata trailers;

private final boolean fillInStackTrace;

/**
* Constructs the exception with both a status. See also {@link Status#asRuntimeException()}.
*
Expand All @@ -51,21 +49,10 @@ public StatusRuntimeException(Status status, @Nullable Metadata trailers) {
}

StatusRuntimeException(Status status, @Nullable Metadata trailers, boolean fillInStackTrace) {
super(Status.formatThrowableMessage(status), status.getCause());
super(Status.formatThrowableMessage(status), status.getCause(),
/* enable suppressions */ true, /* writableStackTrace */ fillInStackTrace);
this.status = status;
this.trailers = trailers;
this.fillInStackTrace = fillInStackTrace;
fillInStackTrace();
}

@Override
public synchronized Throwable fillInStackTrace() {
// Let's observe final variables in two states! This works because Throwable will invoke this
// method before fillInStackTrace is set, thus doing nothing. After the constructor has set
// fillInStackTrace, this method will properly fill it in. Additionally, sub classes may call
// this normally, because fillInStackTrace will either be set, or this method will be
// overriden.
return fillInStackTrace ? super.fillInStackTrace() : this;
}

/**
Expand Down

0 comments on commit e36f099

Please sign in to comment.