Skip to content

Commit 7ad3b4d

Browse files
committed
Add API error checks and return more granular error codes
1 parent 1b64dc1 commit 7ad3b4d

File tree

1 file changed

+79
-57
lines changed

1 file changed

+79
-57
lines changed

src/jvmMain/kotlin/Application.kt

+79-57
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import io.ktor.serialization.jackson.*
55
import io.ktor.server.application.*
66
import io.ktor.server.auth.*
77
import io.ktor.server.http.content.*
8+
import io.ktor.server.plugins.*
89
import io.ktor.server.plugins.callloging.*
910
import io.ktor.server.plugins.compression.*
1011
import io.ktor.server.plugins.contentnegotiation.*
@@ -153,8 +154,16 @@ fun Application.main() {
153154
}
154155
}
155156
call.respond(swList)
157+
} catch (err: PSQLException) {
158+
if (err.toString().contains("The connection attempt failed.")) {
159+
call.respond(HttpStatusCode.ServiceUnavailable, "Database connection failed: $err")
160+
} else {
161+
call.respond(HttpStatusCode.Conflict, "Database error: $err")
162+
}
163+
} catch (err: BadRequestException) {
164+
call.respond(HttpStatusCode.UnprocessableEntity, "Error processing content: $err")
156165
} catch (err: Exception) {
157-
call.respond(HttpStatusCode.NotFound, "Error obtaining software table data: $err")
166+
call.respond(HttpStatusCode.InternalServerError, "Error obtaining software table data: $err")
158167
} finally {
159168
connEMD?.close()
160169
}
@@ -168,33 +177,27 @@ fun Application.main() {
168177
if (!(roles.isWriter or roles.isAdmin)) {
169178
call.respond(HttpStatusCode.Unauthorized)
170179
return@post // not really needed here but important in other places
171-
} else {
180+
}
181+
var connEMD: Connection? = null
182+
try {
172183
val sw = call.receive<SoftwareVersion>()
173-
val connEMD = newEMDConnection(config, this.context)
174-
if (connEMD == null) {
175-
call.respond(HttpStatusCode.Unauthorized)
184+
connEMD = newEMDConnection(config, this.context)
185+
val query = """INSERT INTO software_ (software_version) VALUES ('${sw.software_version}')"""
186+
println(query)
187+
connEMD!!.createStatement().executeUpdate(query)
188+
call.respond(HttpStatusCode.OK, "SW record was created")
189+
} catch (err: PSQLException) {
190+
if (err.toString().contains("The connection attempt failed.")) {
191+
call.respond(HttpStatusCode.ServiceUnavailable, "Database connection failed: $err")
176192
} else {
177-
val query = """
178-
INSERT INTO software_ (software_version)
179-
VALUES ('${sw.software_version}')
180-
""".trimIndent()
181-
// print(query)
182-
try {
183-
connEMD.createStatement().executeUpdate(query)
184-
call.response.status(HttpStatusCode.OK)
185-
call.respond("SW record was created")
186-
} catch (ex: PSQLException) {
187-
if (ex.serverErrorMessage.toString().startsWith("ERROR: permission denied for table")) {
188-
call.respond(HttpStatusCode.Unauthorized)
189-
} else if (ex.serverErrorMessage.toString().startsWith("ERROR: duplicate key value")) {
190-
call.respond(HttpStatusCode.Conflict)
191-
} else {
192-
call.respond(HttpStatusCode.InternalServerError)
193-
}
194-
} finally {
195-
connEMD.close()
196-
}
193+
call.respond(HttpStatusCode.Conflict, "Database error: $err")
197194
}
195+
} catch (err: BadRequestException) {
196+
call.respond(HttpStatusCode.UnprocessableEntity, "Error processing content: $err")
197+
} catch (err: Exception) {
198+
call.respond(HttpStatusCode.InternalServerError, "Error obtaining software table data: $err")
199+
} finally {
200+
connEMD?.close()
198201
}
199202
}
200203

@@ -215,8 +218,16 @@ fun Application.main() {
215218
}
216219
}
217220
call.respond(storageList)
221+
} catch (err: PSQLException) {
222+
if (err.toString().contains("The connection attempt failed.")) {
223+
call.respond(HttpStatusCode.ServiceUnavailable, "Database connection failed: $err")
224+
} else {
225+
call.respond(HttpStatusCode.Conflict, "Database error: $err")
226+
}
227+
} catch (err: BadRequestException) {
228+
call.respond(HttpStatusCode.UnprocessableEntity, "Error processing content: $err")
218229
} catch (err: Exception) {
219-
call.respond(HttpStatusCode.NotFound, "Error obtaining storage table data: $err")
230+
call.respond(HttpStatusCode.InternalServerError, "Error obtaining software table data: $err")
220231
} finally {
221232
connEMD?.close()
222233
}
@@ -227,33 +238,30 @@ fun Application.main() {
227238
val roles = call.principal<WithRoles>()?.roles!!
228239
if (!(roles.isWriter or roles.isAdmin)) {
229240
call.respond(HttpStatusCode.Unauthorized)
230-
} else {
241+
return@post
242+
}
243+
var connEMD: Connection? = null
244+
try {
231245
val storage = call.receive<Storage>()
232-
val connEMD = newEMDConnection(config, this.context)
233-
if (connEMD == null) {
234-
call.respond(HttpStatusCode.Unauthorized)
246+
connEMD = newEMDConnection(config, this.context)
247+
val query = """INSERT INTO storage_ (storage_name) VALUES ('${storage.storage_name}')"""
248+
println(query)
249+
250+
connEMD!!.createStatement().executeUpdate(query)
251+
call.response.status(HttpStatusCode.OK)
252+
call.respond("Storage record was created")
253+
} catch (err: PSQLException) {
254+
if (err.toString().contains("The connection attempt failed.")) {
255+
call.respond(HttpStatusCode.ServiceUnavailable, "Database connection failed: $err")
235256
} else {
236-
val query = """
237-
INSERT INTO storage_ (storage_name)
238-
VALUES ('${storage.storage_name}')
239-
""".trimIndent()
240-
// print(query)
241-
try {
242-
connEMD.createStatement().executeUpdate(query)
243-
call.response.status(HttpStatusCode.OK)
244-
call.respond("Storage record was created")
245-
} catch (ex: PSQLException) { // e.g. this version already exists
246-
if (ex.serverErrorMessage.toString().startsWith("ERROR: permission denied for table")) {
247-
call.respond(HttpStatusCode.Unauthorized)
248-
} else if (ex.serverErrorMessage.toString().startsWith("ERROR: duplicate key value")) {
249-
call.respond(HttpStatusCode.Conflict)
250-
} else {
251-
call.respond(HttpStatusCode.InternalServerError)
252-
}
253-
} finally {
254-
connEMD.close()
255-
}
257+
call.respond(HttpStatusCode.Conflict, "Database error: $err")
256258
}
259+
} catch (err: BadRequestException) {
260+
call.respond(HttpStatusCode.UnprocessableEntity, "Error processing content: $err")
261+
} catch (err: Exception) {
262+
call.respond(HttpStatusCode.InternalServerError, "Error obtaining software table data: $err")
263+
} finally {
264+
connEMD?.close()
257265
}
258266
}
259267
}
@@ -305,8 +313,16 @@ fun Application.main() {
305313
)
306314
}
307315
call.respond(mapOf("events" to lstEvents))
316+
} catch (err: PSQLException) {
317+
if (err.toString().contains("The connection attempt failed.")) {
318+
call.respond(HttpStatusCode.ServiceUnavailable, "Database connection failed: $err")
319+
} else {
320+
call.respond(HttpStatusCode.Conflict, "Database error: $err")
321+
}
322+
} catch (err: BadRequestException) {
323+
call.respond(HttpStatusCode.UnprocessableEntity, "Error processing content: $err")
308324
} catch (err: Exception) {
309-
call.respond(HttpStatusCode.NotFound, "Error obtaining event data:\n $err")
325+
call.respond(HttpStatusCode.InternalServerError, "Error obtaining software table data: $err")
310326
} finally {
311327
connEMD?.close()
312328
}
@@ -350,7 +366,7 @@ fun Application.main() {
350366
INSERT INTO file_ (storage_id, file_path)
351367
VALUES ($storage_id, '$file_path')
352368
""".trimIndent()
353-
print(fileQuery)
369+
println(fileQuery)
354370
connEMD!!.createStatement().executeUpdate(fileQuery)
355371
// TODO remove duplicate code here...
356372
val res2 = connEMD!!.createStatement().executeQuery(
@@ -379,15 +395,21 @@ fun Application.main() {
379395
VALUES ($file_guid, ${event.reference.event_number}, $software_id, ${event.period_number},
380396
${event.run_number}, $parameterValuesStr)
381397
""".trimIndent()
382-
// print(query)
398+
println(query)
383399
connEMD!!.createStatement().executeUpdate(query)
384400
}
385-
call.response.status(HttpStatusCode.OK)
386-
call.respond("Events were created")
401+
call.respond(HttpStatusCode.OK, "Events were created")
402+
} catch (err: PSQLException) {
403+
if (err.toString().contains("The connection attempt failed.")) {
404+
call.respond(HttpStatusCode.ServiceUnavailable, "Database connection failed: $err")
405+
} else {
406+
call.respond(HttpStatusCode.Conflict, "Database error: $err")
407+
}
408+
} catch (err: BadRequestException) {
409+
call.respond(HttpStatusCode.UnprocessableEntity, "Error processing content: $err")
387410
} catch (err: Exception) {
388411
call.respond(HttpStatusCode.InternalServerError, "Error obtaining software table data: $err")
389-
}
390-
finally {
412+
} finally {
391413
connEMD?.close()
392414
}
393415
}

0 commit comments

Comments
 (0)