Skip to content

Commit

Permalink
KTOR-3419 Exceptions for database call and validation exceptions now …
Browse files Browse the repository at this point in the history
…have theirs purpose
  • Loading branch information
VGoncharova committed Mar 29, 2023
1 parent 8bc370c commit b9702e4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.example.exceptions

class DbElementInsertException(message: String? = null, throwable: Throwable? = null) : Throwable(message, throwable)
25 changes: 16 additions & 9 deletions postgres/src/main/kotlin/com/example/plugins/Routing.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.plugins

import com.example.exceptions.DbElementInsertException
import com.example.exceptions.DbElementNotFoundException
import com.example.models.Article
import com.example.service.ArticleService
Expand All @@ -17,38 +18,44 @@ fun Application.configureRouting(dbConnection: Connection) {
// Create new Article
post("/articles") {
val article = call.receive<Article>()
val id = articleService.create(article)
call.respond(HttpStatusCode.Created, id)
try {
val id = articleService.create(article)
call.respond(HttpStatusCode.Created, id)
} catch (cause: DbElementInsertException) {
call.respond(HttpStatusCode.InternalServerError)
}
}
// Read an Article
get("/articles/{id}") {
val id = call.parameters["id"]?.toInt() ?: throw DbElementNotFoundException("Invalid article ID")
try {
val id = call.parameters["id"]?.toInt() ?: throw IllegalArgumentException("Invalid article ID")
val article = articleService.read(id)
call.respond(HttpStatusCode.OK, article)
} catch (cause: DbElementNotFoundException) {
call.respond(HttpStatusCode.NotFound)
} catch (cause: IllegalArgumentException) {
call.respond(HttpStatusCode.BadRequest)
}
}
// Update an Article
put("/articles/{id}") {
val id = call.parameters["id"]?.toInt() ?: throw DbElementNotFoundException("Invalid article ID")
try {
val id = call.parameters["id"]?.toInt() ?: throw IllegalArgumentException("Invalid article ID")
val article = call.receive<Article>()
articleService.update(id, article)
call.respond(HttpStatusCode.OK)
} catch (cause: DbElementNotFoundException) {
call.respond(HttpStatusCode.NotFound)
} catch (cause: IllegalArgumentException) {
call.respond(HttpStatusCode.BadRequest)
}
}
// Delete an Article
delete("/articles/{id}") {
val id = call.parameters["id"]?.toInt() ?: throw DbElementNotFoundException("Invalid article ID")
try {
val id = call.parameters["id"]?.toInt() ?: throw IllegalArgumentException("Invalid article ID")
articleService.delete(id)
call.respond(HttpStatusCode.OK)
} catch (cause: DbElementNotFoundException) {
call.respond(HttpStatusCode.NotFound)
} catch (cause: IllegalArgumentException) {
call.respond(HttpStatusCode.BadRequest)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.service

import com.example.exceptions.DbElementInsertException
import com.example.exceptions.DbElementNotFoundException
import com.example.models.Article
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
Expand Down Expand Up @@ -33,7 +35,7 @@ class ArticleService(private val connection: Connection) {
if (generatedKeys.next()) {
return@withContext generatedKeys.getInt(1)
} else {
throw Exception("Unable to retrieve the id of the newly inserted article")
throw DbElementInsertException("Unable to retrieve the id of the newly inserted article")
}
}

Expand All @@ -48,7 +50,7 @@ class ArticleService(private val connection: Connection) {
val body = resultSet.getString("body")
return@withContext Article(title, body)
} else {
throw Exception("Record not found")
throw DbElementNotFoundException("Record not found")
}
}

Expand Down

0 comments on commit b9702e4

Please sign in to comment.