Feature: StatusException and StatusRuntimeException utility methods
- Lingua principale
- Java
- Stelle
- 12.1k
- Fork
- 4k
- Merge medio
- 2g 17h
- PR unite (30g)
- 37
Descrizione
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);
```
Guida per i contributori
Apri la guida per i contributori
Direzione di ricerca
Start with the Status class and its existing fromThrowable and trailersFromThrowable APIs. Review how StatusException and StatusRuntimeException are handled, then define tests for gRPC and non-gRPC throwables and for status-code matching; done means the proposed utility methods behave consistently for both exception types.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- java
- Ambito
- api
- Tipo di issue
- Funzionalità
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100