dave08
06/03/2024, 12:25 PMdave08
06/03/2024, 12:42 PMtry {
db.runQuery(QueryDsl.executeScript(getResourceAsText("/schema.sql")!!))
} catch (t: R2dbcNonTransientException) {
val error = t.nonFatalOrThrow() as? PostgresqlException
logger.error { "Error importing schema: ${error?.errorDetails}"}
}
Since PostgresqlException is just an interface... and only it has the errorDetails
property 🙈simon.vergauwen
06/03/2024, 12:50 PMPostgresqlException
is an interface implemented by R2dbcNonTransientException
? 🤯
Good idea, I think in a lot of cases catch
can be replaced by try/catch
. Everywhere where you catch a specific exception, except IllegalStateException
.dave08
06/03/2024, 12:54 PMstatic final class PostgresqlPermissionDeniedException extends R2dbcPermissionDeniedException implements PostgresqlException
public interface PostgresqlException {
ErrorDetails getErrorDetails();
}
dave08
06/03/2024, 12:54 PMR2dbcPermissionDeniedException
extends public abstract class R2dbcException extends RuntimeException
in the end... I'm not sure why that exception doesn't have those error details instead of making an interface out of it... 😵💫dave08
06/03/2024, 12:56 PM, I think in a lot of casesSo when would we use the catch variant that catches a specific throwable?can be replaced bycatch
.try/catch
dave08
06/03/2024, 12:59 PMsimon.vergauwen
06/03/2024, 1:09 PMCancellationException
, and CancellationException
extends IllegalStateException
which is thrown by many functions. So catching IllegalStateException
might accidentally result in catching, and recovering from a CancellationException
. That's when you should be using catch
.
Additionally, when you need to catch Throwable
, well you need to protect from CancellationException
and fatal JVM exceptions.
So in any of those cases you'd need catch
, you might prefer to just use catch
always to be consistent and avoid surprises. That's what many people do. I've personally gone back and forth, not sure if I have any preference but if I write public examples I prefer to avoid more new things all at once so I use try/catch
when it makes sense, and it turns out I don't need to catch Throwable
or IllegalStateException
much. Mostly I am turning SDK, or Database, exceptions into typed errors where I need them.
A back-and-forth refactor action, with an analyses for preventing this mistake seems appropriate since it's more subjective and a plugin can help to easily refactor.