@@ -5,6 +5,7 @@ import io.ktor.serialization.jackson.*
5
5
import io.ktor.server.application.*
6
6
import io.ktor.server.auth.*
7
7
import io.ktor.server.http.content.*
8
+ import io.ktor.server.plugins.*
8
9
import io.ktor.server.plugins.callloging.*
9
10
import io.ktor.server.plugins.compression.*
10
11
import io.ktor.server.plugins.contentnegotiation.*
@@ -153,8 +154,16 @@ fun Application.main() {
153
154
}
154
155
}
155
156
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 " )
156
165
} 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 " )
158
167
} finally {
159
168
connEMD?.close()
160
169
}
@@ -168,33 +177,27 @@ fun Application.main() {
168
177
if (! (roles.isWriter or roles.isAdmin)) {
169
178
call.respond(HttpStatusCode .Unauthorized )
170
179
return @post // not really needed here but important in other places
171
- } else {
180
+ }
181
+ var connEMD: Connection ? = null
182
+ try {
172
183
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 " )
176
192
} 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 " )
197
194
}
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()
198
201
}
199
202
}
200
203
@@ -215,8 +218,16 @@ fun Application.main() {
215
218
}
216
219
}
217
220
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 " )
218
229
} 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 " )
220
231
} finally {
221
232
connEMD?.close()
222
233
}
@@ -227,33 +238,30 @@ fun Application.main() {
227
238
val roles = call.principal<WithRoles >()?.roles!!
228
239
if (! (roles.isWriter or roles.isAdmin)) {
229
240
call.respond(HttpStatusCode .Unauthorized )
230
- } else {
241
+ return @post
242
+ }
243
+ var connEMD: Connection ? = null
244
+ try {
231
245
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 " )
235
256
} 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 " )
256
258
}
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()
257
265
}
258
266
}
259
267
}
@@ -305,8 +313,16 @@ fun Application.main() {
305
313
)
306
314
}
307
315
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 " )
308
324
} 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 " )
310
326
} finally {
311
327
connEMD?.close()
312
328
}
@@ -350,7 +366,7 @@ fun Application.main() {
350
366
INSERT INTO file_ (storage_id, file_path)
351
367
VALUES ($storage_id , '$file_path ')
352
368
""" .trimIndent()
353
- print (fileQuery)
369
+ println (fileQuery)
354
370
connEMD!! .createStatement().executeUpdate(fileQuery)
355
371
// TODO remove duplicate code here...
356
372
val res2 = connEMD!! .createStatement().executeQuery(
@@ -379,15 +395,21 @@ fun Application.main() {
379
395
VALUES ($file_guid , ${event.reference.event_number} , $software_id , ${event.period_number} ,
380
396
${event.run_number} , $parameterValuesStr )
381
397
""" .trimIndent()
382
- // print (query)
398
+ println (query)
383
399
connEMD!! .createStatement().executeUpdate(query)
384
400
}
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 " )
387
410
} catch (err: Exception ) {
388
411
call.respond(HttpStatusCode .InternalServerError , " Error obtaining software table data: $err " )
389
- }
390
- finally {
412
+ } finally {
391
413
connEMD?.close()
392
414
}
393
415
}
0 commit comments