Skip to content

Commit

Permalink
feat(SimpleCachePlugin.kt): add error handling for failed calls (#20)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ShiinaKin authored Oct 15, 2024
1 parent 4273e00 commit 3eecb41
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ allprojects {

group = "com.ucasoft.ktor"

version = "0.4.3"
version = "0.4.4"

repositories {
mavenCentral()
Expand Down
4 changes: 2 additions & 2 deletions ktor-simple-cache/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Ktor Simple Cache
Base solution which provides the plugin implementation and abstract class for cache providers.

[![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)
[![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)
## Setup
### Gradle
```kotlin
repositories {
mavenCentral()
}

implementation("com.ucasoft.ktor:ktor-simple-cache:0.4.3")
implementation("com.ucasoft.ktor:ktor-simple-cache:0.4.4")
```
## Usage
```kotlin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.ucasoft.ktor.simpleCache

import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.application.hooks.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.util.*
Expand All @@ -24,6 +25,10 @@ val SimpleCachePlugin = createRouteScopedPlugin(name = "SimpleCachePlugin", ::Si
it.respond(cache)
}
}
on(CallFailed) { _, e ->
provider.badResponse()
throw e
}
onCallRespond { call, body ->
if ((call.response.status() ?: HttpStatusCode.OK) >= HttpStatusCode.BadRequest) {
provider.badResponse()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,29 @@ internal class SimpleCacheTests {
}
}

@Test
fun `check route thrown exceptions aren not locked`() {
with(buildTestEngine(buildProvider(), Application::testApplication)) {

runBlocking {
val totalThreads = 100
val deferred = (1..totalThreads).map {
async {
shouldThrow<RuntimeException> {
client.get("/exception")
}
}
}

val result = deferred.awaitAll().map { it.message }.groupBy { it }
.map { it.key to it.value.size }
result.shouldBeSingleton {
it.second.shouldBe(totalThreads)
}
}
}
}

@Test
fun `check all parameters`() {
val firstCacheKey = "/check?param1=value1&param2=value2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ fun Application.testApplication() {
get("/bad") {
call.respond(HttpStatusCode.BadRequest, "Bad request")
}
get("/exception") {
throw RuntimeException("Just an exception")
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions ktor-simple-memory-cache/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Ktor Simple Memory Cache
Memory cache provider for Ktor Simple Cache plugin

[![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)
[![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)
## Setup
### Gradle
```kotlin
repositories {
mavenCentral()
}

implementation("com.ucasoft.ktor:ktor-simple-memory-cache:0.4.3")
implementation("com.ucasoft.ktor:ktor-simple-memory-cache:0.4.4")
```
## Usage
```kotlin
Expand Down
4 changes: 2 additions & 2 deletions ktor-simple-redis-cache/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Ktor Simple Redis Cache
Redis cache provider for Ktor Simple Cache plugin

[![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)
[![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)
## Setup
### Gradle
```kotlin
repositories {
mavenCentral()
}

implementation("com.ucasoft.ktor:ktor-simple-redis-cache:0.4.3")
implementation("com.ucasoft.ktor:ktor-simple-redis-cache:0.4.4")
```
## Usage
```kotlin
Expand Down

0 comments on commit 3eecb41

Please sign in to comment.