Skip to content

Commit

Permalink
Add a helper to determine if an exception is from a `304 Not Modified…
Browse files Browse the repository at this point in the history
…` response.

In practice, Ktor will throw a `RedirectResponseException` on a `304 Not Modified` server result. This small helper lets us avoid having to repeat ourselves with a cast and status check when we're trying to determine what happened in our exception handling:

```kotlin
try {
   val (response, responseHeaders) = exampleService.getCacheData(...)
} catch (e: ServiceException) {
  if (e.isNotModifiedException()) {
    // Ignore, local cache is not stale.
  }
}
```
  • Loading branch information
darronschall committed Feb 15, 2023
1 parent 19e771c commit d4167ea
Showing 1 changed file with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package com.collectiveidea.twirp

import io.ktor.client.plugins.RedirectResponseException
import io.ktor.client.plugins.ResponseException
import io.ktor.http.HttpStatusCode

class ServiceException(val error: ErrorResponse, responseException: ResponseException) :
RuntimeException(error.msg, responseException)
class ServiceException(val error: ErrorResponse, val responseException: ResponseException) :
RuntimeException(error.msg, responseException) {

fun isNotModifiedException(): Boolean {
return responseException is RedirectResponseException && responseException.response.status == HttpStatusCode.NotModified
}
}

0 comments on commit d4167ea

Please sign in to comment.