Skip to content

Commit 2c18bb3

Browse files
Add error handling and logging for database queries in API functions
1 parent 0520cfa commit 2c18bb3

File tree

1 file changed

+127
-73
lines changed

1 file changed

+127
-73
lines changed

python_api/api.py

Lines changed: 127 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,17 @@ async def get_models(
8585

8686
sql_query += " GROUP BY models.id, frameworks.name, frameworks.version"
8787

88-
cur.execute(sql_query, params)
89-
rows = cur.fetchall()
88+
try:
89+
cur.execute(sql_query, params)
90+
rows = cur.fetchall()
91+
except Exception as e:
92+
logging.error(f"An error occurred: {e} while executing query: {sql_query} in function get_models")
93+
return JSONResponse(
94+
status_code=500,
95+
content={"message": "An internal server error occurred."},
96+
)
97+
# cur.execute(sql_query, params)
98+
# rows = cur.fetchall()
9099

91100
models = []
92101
for row_dict in rows:
@@ -149,8 +158,15 @@ async def get_model(model_id: int):
149158
JOIN frameworks ON frameworks.id = models.framework_id
150159
LEFT JOIN architectures ON architectures.framework_id = frameworks.id WHERE models.id = %s GROUP BY models.id, frameworks.name, frameworks.version"""
151160
# cur.execute(f"SELECT * FROM models WHERE id={model_id} AND deleted_at IS NULL")
152-
cur.execute(query, (model_id,))
153-
row_dict = cur.fetchone()
161+
try:
162+
cur.execute(query, (model_id,))
163+
row_dict = cur.fetchone()
164+
except Exception as e:
165+
logging.error(f"An error occurred: {e} while executing query: {query} in function get_model")
166+
return JSONResponse(
167+
status_code=500,
168+
content={"message": "An internal server error occurred."},
169+
)
154170

155171
model = {
156172
"id": row_dict["id"],
@@ -202,14 +218,21 @@ async def get_model(model_id: int):
202218
async def get_frameworks():
203219
cur,conn=get_db_cur_con()
204220

205-
cur.execute("""
206-
SELECT f.id as framework_id, f.name as framework_name, f.version,
221+
query = """ SELECT f.id as framework_id, f.name as framework_name, f.version,
207222
a.name as architecture_name
208223
FROM frameworks f
209224
LEFT JOIN architectures a ON a.framework_id = f.id
210-
""")
211-
rows = cur.fetchall()
212-
cur.close()
225+
"""
226+
try:
227+
cur.execute(query)
228+
rows = cur.fetchall()
229+
cur.close()
230+
except Exception as e:
231+
logging.error(f"An error occurred: {e} while executing query: {query} in function get_frameworks")
232+
return JSONResponse(
233+
status_code=500,
234+
content={"message": "An internal server error occurred."},
235+
)
213236

214237
frameworks_dict = {}
215238
for row in rows:
@@ -233,8 +256,15 @@ async def get_frameworks():
233256
async def get_framework(framework_id: int):
234257
# get all models from the database as json
235258
cur,conn=get_db_cur_con()
236-
cur.execute(f"SELECT * FROM frameworks WHERE id={framework_id}")
237-
framework = cur.fetchone()
259+
try:
260+
cur.execute(f"SELECT * FROM frameworks WHERE id={framework_id}")
261+
framework = cur.fetchone()
262+
except Exception as e:
263+
logging.error(f"An error occurred: {e} while executing query: {query} in function get_framework")
264+
return JSONResponse(
265+
status_code=500,
266+
content={"message": "An internal server error occurred."},
267+
)
238268
# print(model)
239269
json_framework = Framework(*framework).to_dict()
240270
return{"framework": json_framework}
@@ -248,8 +278,7 @@ async def version():
248278

249279
@app.get("/experiments/{experiment_id}")
250280
async def get_experiment(experiment_id: str):
251-
cur,conn=get_db_cur_con()
252-
cur.execute("""
281+
query = """
253282
SELECT e.id AS experiment_id,
254283
t.id AS trial_id,
255284
t.created_at,
@@ -258,13 +287,25 @@ async def get_experiment(experiment_id: str):
258287
FROM experiments e
259288
JOIN trials t ON e.id = t.experiment_id
260289
WHERE e.id = %s
261-
""", (experiment_id,))
262-
263-
# Fetch all results
264-
rows = cur.fetchall()
265-
print(rows)
290+
"""
291+
cur,conn=get_db_cur_con()
292+
try:
293+
cur.execute(query, (experiment_id,))
294+
295+
# Fetch all results
296+
rows = cur.fetchall()
297+
except Exception as e:
298+
logging.error(f"An error occurred: {e} while executing query: {query} in function get_experiment")
299+
return JSONResponse(
300+
status_code=500,
301+
content={"message": "An internal server error occurred."},
302+
)
266303
if not rows:
267-
raise Exception(f"No experiment found with ID {experiment_id}")
304+
#raise Exception(f"No experiment found with ID {experiment_id}") # this crashes the whole program
305+
return JSONResponse(
306+
status_code=404,
307+
content={"message": f"No experiment found with ID {experiment_id}"},
308+
)
268309

269310
# Prepare the response structure
270311
result = {
@@ -402,13 +443,20 @@ async def delete_trial(trial_id: str):
402443
@app.get("/trial/{trial_id}")
403444
async def get_trial(trial_id: str):
404445
cur,conn=get_db_cur_con()
405-
446+
query="""
447+
SELECT * FROM trials t
448+
WHERE t.id = %s
449+
"""
406450
# check if trial has a source trial
407-
cur.execute("""
408-
SELECT * FROM trials t
409-
WHERE t.id = %s
410-
""", (trial_id,))
411-
row = cur.fetchone()
451+
try:
452+
cur.execute(query, (trial_id,))
453+
row = cur.fetchone()
454+
except Exception as e:
455+
logging.error(f"An error occurred: {e} while executing query: {query} in function get_trial")
456+
return JSONResponse(
457+
status_code=500,
458+
content={"message": "An internal server error occurred."},
459+
)
412460
if row["source_trial_id"] is not None:
413461
# print("\n\n\n\n\n\n\n\n\n\n")
414462
# print(row["source_trial_id"])
@@ -419,54 +467,60 @@ async def get_trial(trial_id: str):
419467

420468
cur.close()
421469
cur,conn=get_db_cur_con()
422-
423-
424-
cur.execute("""
425-
SELECT t.id AS trial_id,
426-
t.result,
427-
t.source_trial_id as source_trial_id,
428-
t.completed_at,
429-
ti.url AS input_url,
430-
m.id AS modelId,
431-
m.created_at AS model_created_at,
432-
433-
m.updated_at AS model_updated_at,
434-
m.attribute_top1 AS top1,
435-
m.attribute_top5 AS top5,
436-
m.attribute_kind AS kind,
437-
m.attribute_manifest_author AS manifest_author,
438-
m.attribute_training_dataset AS training_dataset,
439-
m.description,
440-
m.short_description,
441-
m.detail_graph_checksum AS graph_checksum,
442-
m.detail_graph_path AS graph_path,
443-
m.detail_weights_checksum AS weights_checksum,
444-
m.detail_weights_path AS weights_path,
445-
f.id AS framework_id,
446-
f.name AS framework_name,
447-
f.version AS framework_version,
448-
m.input_description,
449-
m.input_type,
450-
m.license,
451-
m.name AS model_name,
452-
m.output_description,
453-
m.output_type,
454-
m.url_github,
455-
m.url_citation,
456-
m.url_link1,
457-
m.url_link2,
458-
a.name AS architecture_name
459-
FROM trials t
460-
JOIN trial_inputs ti ON t.id = ti.trial_id
461-
462-
JOIN models m ON t.model_id = m.id
463-
JOIN frameworks f ON m.framework_id = f.id
464-
LEFT JOIN architectures a ON a.framework_id = f.id
465-
WHERE t.id = %s
466-
""", (trial_id,))
467-
468-
# Fetch the result
469-
row = cur.fetchone()
470+
query = """
471+
SELECT t.id AS trial_id,
472+
t.result,
473+
t.source_trial_id as source_trial_id,
474+
t.completed_at,
475+
ti.url AS input_url,
476+
m.id AS modelId,
477+
m.created_at AS model_created_at,
478+
479+
m.updated_at AS model_updated_at,
480+
m.attribute_top1 AS top1,
481+
m.attribute_top5 AS top5,
482+
m.attribute_kind AS kind,
483+
m.attribute_manifest_author AS manifest_author,
484+
m.attribute_training_dataset AS training_dataset,
485+
m.description,
486+
m.short_description,
487+
m.detail_graph_checksum AS graph_checksum,
488+
m.detail_graph_path AS graph_path,
489+
m.detail_weights_checksum AS weights_checksum,
490+
m.detail_weights_path AS weights_path,
491+
f.id AS framework_id,
492+
f.name AS framework_name,
493+
f.version AS framework_version,
494+
m.input_description,
495+
m.input_type,
496+
m.license,
497+
m.name AS model_name,
498+
m.output_description,
499+
m.output_type,
500+
m.url_github,
501+
m.url_citation,
502+
m.url_link1,
503+
m.url_link2,
504+
a.name AS architecture_name
505+
FROM trials t
506+
JOIN trial_inputs ti ON t.id = ti.trial_id
507+
508+
JOIN models m ON t.model_id = m.id
509+
JOIN frameworks f ON m.framework_id = f.id
510+
LEFT JOIN architectures a ON a.framework_id = f.id
511+
WHERE t.id = %s
512+
"""
513+
try:
514+
cur.execute(query, (trial_id,))
515+
516+
# Fetch the result
517+
row = cur.fetchone()
518+
except Exception as e:
519+
logging.error(f"An error occurred: {e} while executing query: {query} in function get_trial")
520+
return JSONResponse(
521+
status_code=500,
522+
content={"message": "An internal server error occurred."},
523+
)
470524

471525
if not row:
472526
raise Exception(f"No trial found with ID {trial_id}")

0 commit comments

Comments
 (0)