Feature: StatusException and StatusRuntimeException utility methods
- Lenguaje dominante
- Java
- Estrellas
- 12.1k
- Forks
- 4k
- Merge medio
- 2 d 17 h
- PR fusionados (30 d)
- 37
Descripción
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);
```
Guía de contribución
Línea de trabajo
Comienza con la clase Status y sus APIs existentes fromThrowable y trailersFromThrowable. Revisa cómo se gestionan StatusException y StatusRuntimeException y, a continuación, define pruebas para throwables de gRPC y que no sean de gRPC, así como para la coincidencia de códigos de estado; se considera completado cuando los métodos de utilidad propuestos se comporten de forma coherente para ambos tipos de excepción.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- java
- Área
- api
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100