Skip to content

Commit 3eecb41

Browse files
authored
feat(SimpleCachePlugin.kt): add error handling for failed calls (#20)
* feat(SimpleCachePlugin.kt): add error handling for failed calls Add `on(CallFailed)` hook to handle and log failed calls, improving error management within the SimpleCachePlugin. * chore: bump version to 0.4.4 * unit test
1 parent 4273e00 commit 3eecb41

File tree

7 files changed

+38
-7
lines changed

7 files changed

+38
-7
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ allprojects {
66

77
group = "com.ucasoft.ktor"
88

9-
version = "0.4.3"
9+
version = "0.4.4"
1010

1111
repositories {
1212
mavenCentral()

ktor-simple-cache/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# Ktor Simple Cache
22
Base solution which provides the plugin implementation and abstract class for cache providers.
33

4-
[![Maven Central with version prefix filter](https://img.shields.io/maven-central/v/com.ucasoft.ktor/ktor-simple-cache/0.4.3?color=blue)](https://search.maven.org/artifact/com.ucasoft.ktor/ktor-simple-cache/0.4.3/jar)
4+
[![Maven Central with version prefix filter](https://img.shields.io/maven-central/v/com.ucasoft.ktor/ktor-simple-cache/0.4.4?color=blue)](https://search.maven.org/artifact/com.ucasoft.ktor/ktor-simple-cache/0.4.4/jar)
55
## Setup
66
### Gradle
77
```kotlin
88
repositories {
99
mavenCentral()
1010
}
1111

12-
implementation("com.ucasoft.ktor:ktor-simple-cache:0.4.3")
12+
implementation("com.ucasoft.ktor:ktor-simple-cache:0.4.4")
1313
```
1414
## Usage
1515
```kotlin

ktor-simple-cache/src/main/kotlin/com/ucasoft/ktor/simpleCache/SimpleCachePlugin.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.ucasoft.ktor.simpleCache
22

33
import io.ktor.http.*
44
import io.ktor.server.application.*
5+
import io.ktor.server.application.hooks.*
56
import io.ktor.server.request.*
67
import io.ktor.server.response.*
78
import io.ktor.util.*
@@ -24,6 +25,10 @@ val SimpleCachePlugin = createRouteScopedPlugin(name = "SimpleCachePlugin", ::Si
2425
it.respond(cache)
2526
}
2627
}
28+
on(CallFailed) { _, e ->
29+
provider.badResponse()
30+
throw e
31+
}
2732
onCallRespond { call, body ->
2833
if ((call.response.status() ?: HttpStatusCode.OK) >= HttpStatusCode.BadRequest) {
2934
provider.badResponse()

ktor-simple-cache/src/test/kotlin/com/ucasoft/ktor/simpleCache/SimpleCacheTests.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,29 @@ internal class SimpleCacheTests {
157157
}
158158
}
159159

160+
@Test
161+
fun `check route thrown exceptions aren not locked`() {
162+
with(buildTestEngine(buildProvider(), Application::testApplication)) {
163+
164+
runBlocking {
165+
val totalThreads = 100
166+
val deferred = (1..totalThreads).map {
167+
async {
168+
shouldThrow<RuntimeException> {
169+
client.get("/exception")
170+
}
171+
}
172+
}
173+
174+
val result = deferred.awaitAll().map { it.message }.groupBy { it }
175+
.map { it.key to it.value.size }
176+
result.shouldBeSingleton {
177+
it.second.shouldBe(totalThreads)
178+
}
179+
}
180+
}
181+
}
182+
160183
@Test
161184
fun `check all parameters`() {
162185
val firstCacheKey = "/check?param1=value1&param2=value2"

ktor-simple-cache/src/test/kotlin/com/ucasoft/ktor/simpleCache/TestApplication.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ fun Application.testApplication() {
3030
get("/bad") {
3131
call.respond(HttpStatusCode.BadRequest, "Bad request")
3232
}
33+
get("/exception") {
34+
throw RuntimeException("Just an exception")
35+
}
3336
}
3437
}
3538
}

ktor-simple-memory-cache/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# Ktor Simple Memory Cache
22
Memory cache provider for Ktor Simple Cache plugin
33

4-
[![Maven Central with version prefix filter](https://img.shields.io/maven-central/v/com.ucasoft.ktor/ktor-simple-memory-cache/0.4.3?color=blue)](https://search.maven.org/artifact/com.ucasoft.ktor/ktor-simple-memory-cache/0.4.3/jar)
4+
[![Maven Central with version prefix filter](https://img.shields.io/maven-central/v/com.ucasoft.ktor/ktor-simple-memory-cache/0.4.4?color=blue)](https://search.maven.org/artifact/com.ucasoft.ktor/ktor-simple-memory-cache/0.4.4/jar)
55
## Setup
66
### Gradle
77
```kotlin
88
repositories {
99
mavenCentral()
1010
}
1111

12-
implementation("com.ucasoft.ktor:ktor-simple-memory-cache:0.4.3")
12+
implementation("com.ucasoft.ktor:ktor-simple-memory-cache:0.4.4")
1313
```
1414
## Usage
1515
```kotlin

ktor-simple-redis-cache/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# Ktor Simple Redis Cache
22
Redis cache provider for Ktor Simple Cache plugin
33

4-
[![Maven Central with version prefix filter](https://img.shields.io/maven-central/v/com.ucasoft.ktor/ktor-simple-redis-cache/0.4.3?color=blue)](https://search.maven.org/artifact/com.ucasoft.ktor/ktor-simple-redis-cache/0.4.3/jar)
4+
[![Maven Central with version prefix filter](https://img.shields.io/maven-central/v/com.ucasoft.ktor/ktor-simple-redis-cache/0.4.4?color=blue)](https://search.maven.org/artifact/com.ucasoft.ktor/ktor-simple-redis-cache/0.4.4/jar)
55
## Setup
66
### Gradle
77
```kotlin
88
repositories {
99
mavenCentral()
1010
}
1111

12-
implementation("com.ucasoft.ktor:ktor-simple-redis-cache:0.4.3")
12+
implementation("com.ucasoft.ktor:ktor-simple-redis-cache:0.4.4")
1313
```
1414
## Usage
1515
```kotlin

0 commit comments

Comments
 (0)