Skip to content

Commit b9702e4

Browse files
committed
KTOR-3419 Exceptions for database call and validation exceptions now have theirs purpose
1 parent 8bc370c commit b9702e4

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.example.exceptions
2+
3+
class DbElementInsertException(message: String? = null, throwable: Throwable? = null) : Throwable(message, throwable)

postgres/src/main/kotlin/com/example/plugins/Routing.kt

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.example.plugins
22

3+
import com.example.exceptions.DbElementInsertException
34
import com.example.exceptions.DbElementNotFoundException
45
import com.example.models.Article
56
import com.example.service.ArticleService
@@ -17,38 +18,44 @@ fun Application.configureRouting(dbConnection: Connection) {
1718
// Create new Article
1819
post("/articles") {
1920
val article = call.receive<Article>()
20-
val id = articleService.create(article)
21-
call.respond(HttpStatusCode.Created, id)
21+
try {
22+
val id = articleService.create(article)
23+
call.respond(HttpStatusCode.Created, id)
24+
} catch (cause: DbElementInsertException) {
25+
call.respond(HttpStatusCode.InternalServerError)
26+
}
2227
}
2328
// Read an Article
2429
get("/articles/{id}") {
25-
val id = call.parameters["id"]?.toInt() ?: throw DbElementNotFoundException("Invalid article ID")
2630
try {
31+
val id = call.parameters["id"]?.toInt() ?: throw IllegalArgumentException("Invalid article ID")
2732
val article = articleService.read(id)
2833
call.respond(HttpStatusCode.OK, article)
2934
} catch (cause: DbElementNotFoundException) {
3035
call.respond(HttpStatusCode.NotFound)
36+
} catch (cause: IllegalArgumentException) {
37+
call.respond(HttpStatusCode.BadRequest)
3138
}
3239
}
3340
// Update an Article
3441
put("/articles/{id}") {
35-
val id = call.parameters["id"]?.toInt() ?: throw DbElementNotFoundException("Invalid article ID")
3642
try {
43+
val id = call.parameters["id"]?.toInt() ?: throw IllegalArgumentException("Invalid article ID")
3744
val article = call.receive<Article>()
3845
articleService.update(id, article)
3946
call.respond(HttpStatusCode.OK)
40-
} catch (cause: DbElementNotFoundException) {
41-
call.respond(HttpStatusCode.NotFound)
47+
} catch (cause: IllegalArgumentException) {
48+
call.respond(HttpStatusCode.BadRequest)
4249
}
4350
}
4451
// Delete an Article
4552
delete("/articles/{id}") {
46-
val id = call.parameters["id"]?.toInt() ?: throw DbElementNotFoundException("Invalid article ID")
4753
try {
54+
val id = call.parameters["id"]?.toInt() ?: throw IllegalArgumentException("Invalid article ID")
4855
articleService.delete(id)
4956
call.respond(HttpStatusCode.OK)
50-
} catch (cause: DbElementNotFoundException) {
51-
call.respond(HttpStatusCode.NotFound)
57+
} catch (cause: IllegalArgumentException) {
58+
call.respond(HttpStatusCode.BadRequest)
5259
}
5360
}
5461
}

postgres/src/main/kotlin/com/example/service/ArticleService.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.example.service
22

3+
import com.example.exceptions.DbElementInsertException
4+
import com.example.exceptions.DbElementNotFoundException
35
import com.example.models.Article
46
import kotlinx.coroutines.Dispatchers
57
import kotlinx.coroutines.withContext
@@ -33,7 +35,7 @@ class ArticleService(private val connection: Connection) {
3335
if (generatedKeys.next()) {
3436
return@withContext generatedKeys.getInt(1)
3537
} else {
36-
throw Exception("Unable to retrieve the id of the newly inserted article")
38+
throw DbElementInsertException("Unable to retrieve the id of the newly inserted article")
3739
}
3840
}
3941

@@ -48,7 +50,7 @@ class ArticleService(private val connection: Connection) {
4850
val body = resultSet.getString("body")
4951
return@withContext Article(title, body)
5052
} else {
51-
throw Exception("Record not found")
53+
throw DbElementNotFoundException("Record not found")
5254
}
5355
}
5456

0 commit comments

Comments
 (0)