Skip to content

Commit 0185669

Browse files
committed
Error handling for kobo attachment urls
1 parent 9a46859 commit 0185669

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

main.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,20 @@ async def docs_redirect():
6363
@app.get("/health")
6464
async def health():
6565
"""Get health of instance."""
66-
kobo = requests.get(f"https://kobo.ifrc.org/api/v2")
67-
return JSONResponse(
68-
status_code=200,
69-
content={"kobo-connect": 200, "kobo.ifrc.org": kobo.status_code},
70-
)
66+
logger.info("Health check initiated")
67+
try:
68+
kobo = requests.get(f"https://kobo.ifrc.org/api/v2", timeout=10)
69+
logger.info(f"Kobo API health check completed with status: {kobo.status_code}")
70+
return JSONResponse(
71+
status_code=200,
72+
content={"kobo-connect": 200, "kobo.ifrc.org": kobo.status_code},
73+
)
74+
except requests.exceptions.RequestException as e:
75+
logger.error(f"Kobo API health check failed: {str(e)}")
76+
return JSONResponse(
77+
status_code=200,
78+
content={"kobo-connect": 200, "kobo.ifrc.org": "unavailable"},
79+
)
7180

7281

7382
if __name__ == "__main__":

routes/routes121.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,14 @@ async def kobo_to_121(
9898
):
9999
payload[target_field] = kobo_data[kobo_field]
100100
else:
101-
payload[target_field] = attachments[kobo_value_url]["url"]
101+
try:
102+
payload[target_field] = attachments[kobo_value_url]["url"]
103+
except KeyError:
104+
logger.warning(
105+
f"Could not retrieve attachment URL for field '{target_field}' with value '{kobo_value_url}'",
106+
extra=extra_logs,
107+
)
108+
payload[target_field] = "could not retrieve attachment url from kobo"
102109

103110
payload["referenceId"] = referenceId
104111

0 commit comments

Comments
 (0)