StatusException/StatusRuntimeException hide stack trace in a simpler way (#11064)

This commit is contained in:
Alex Panchenko 2024-04-01 17:31:13 +02:00 committed by GitHub
parent 0866e716d6
commit e36f099be9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 4 additions and 29 deletions

View File

@ -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()}.
@ -49,21 +48,10 @@ public class StatusException extends Exception {
}
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;
}
/**

View File

@ -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()}.
*
@ -51,21 +49,10 @@ public class StatusRuntimeException extends RuntimeException {
}
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;
}
/**