grpc / grpc/grpc-java

Feature: StatusException and StatusRuntimeException utility methods

Offen
#3,425 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
enhancement
Vorherrschende Sprache
Java
Sterne
12.1k
Forks
4k
Ø Merge
2 T. 17 Std.
Gemergte PRs (30 T.)
37

Beschreibung

I'd like to propose adding additional static utility methods to the `Status` class to simplify common patterns for dealing with `StatusException` and `StatusRuntimeException`. Since SE and SRE are unrelated, working with them cannot be done with polymorphism.

I'd like to propose the following additional API for `Status`:
```java
public static boolean hasStatus(Throwable t)
public static boolean hasStatusCode(Throwable t, Status.Code code)
public static void doWithStatus(Throwable t, BiConsumer action)
```
These methods support handling gRPC statuses like:
```java
Futures.addCallback(
response,
new FutureCallback() {
@Override
public void onFailure(Throwable t) {
if (hasStatusCode(t, Status.Code.NOT_FOUND)) {
// If you are prepared for the error's status code, handle it
doWithStatus(t, (status, metadata) -> dealWithNotFoundStatus(status));
} else if (hasStatus(t)) {
// Other gRPC errors can be handled generically
doWithStatus(t, (status, metadata) -> handleGrpcProblem(status, metadata));
} else {
// Other non-grpc exceptions are handled normally
dealWithUnknownException(t);
}
}
},
executor);
```
The above code can be written using the existing APIs, but requires multiple nested if statements and `instanceof` checks.
```java
Futures.addCallback(
response,
new FutureCallback() {
@Override
public void onFailure(Throwable t) {
if (t instanceof StatusRuntimeException || t instanceof StatusException) {
Status status = Status.fromThrowable(t);
Metadata trailers = Status.trailersFromThrowable(t);
if (status == Status.Code.NOT_FOUND) {
// If you are prepared for the error's status code, handle it
dealWithNotFoundStatus(status);
} else {
// Other gRPC errors can be handled generically
handleGrpcProblem(status, metadata);
}
} else {
// Other non-grpc exceptions are handled normally
dealWithUnknownException(t);
}
}
},
executor);
```

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginne mit der Status-Klasse und ihren vorhandenen fromThrowable- und trailersFromThrowable-APIs. Überprüfe, wie StatusException und StatusRuntimeException behandelt werden, und definiere anschließend Tests für gRPC- und Nicht-gRPC-Throwables sowie für die Übereinstimmung von Statuscodes; abgeschlossen ist die Aufgabe, wenn sich die vorgeschlagenen Utility-Methoden für beide Exception-Typen konsistent verhalten.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
java
Bereich
api
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.