Skip to content

Commit

Permalink
Use a more specific type for error callbacks (#132)
Browse files Browse the repository at this point in the history
Error callbacks are only ever called with `ConnectException` as
arguments. Being more specific in these requests can tidy up call-sites
in avoiding type assertions (as a hard cast to `ConnectException`) or
overly generic handling where callers account for additional exception
types that won't ever be used.
  • Loading branch information
jzbrooks authored Oct 20, 2023
1 parent 21b3e2c commit 4f91ff9
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions library/src/main/kotlin/com/connectrpc/ResponseMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fun ResponseMessage<*>.exceptionOrNull(): Throwable? {
*/
inline fun <R, T> ResponseMessage<T>.fold(
onSuccess: (value: T) -> R,
onFailure: (exception: Throwable) -> R,
onFailure: (exception: ConnectException) -> R,
): R {
return when (this) {
is ResponseMessage.Success -> onSuccess(this.message)
Expand All @@ -114,7 +114,7 @@ fun <T> ResponseMessage<T>.getOrDefault(defaultValue: T): T {
*
* Note, that this function rethrows any [Throwable] exception thrown by [onFailure] function.
*/
inline fun <R, T : R> ResponseMessage<T>.getOrElse(onFailure: (exception: Throwable) -> R): R {
inline fun <R, T : R> ResponseMessage<T>.getOrElse(onFailure: (exception: ConnectException) -> R): R {
return when (this) {
is ResponseMessage.Success -> this.message
is ResponseMessage.Failure -> onFailure(this.cause)
Expand Down

0 comments on commit 4f91ff9

Please sign in to comment.