Skip to content

Commit 0a47705

Browse files
authored
Fixing error message responses for psycopg2.Error (Netflix#280)
* better error message readability `psycopg2.Error` * ran black * Adding a manual `print` to statement for errors. - Undo this later * Removed print + handled timeout error. * black formatting. * nit fix.
1 parent 30da52a commit 0a47705

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

services/data/db_utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
from typing import List, Dict, Any
23
import psycopg2
34
import collections
@@ -14,6 +15,28 @@
1415
def aiopg_exception_handling(exception):
1516
err_msg = str(exception)
1617
body = {"err_msg": err_msg}
18+
if isinstance(exception, asyncio.TimeoutError):
19+
body = {
20+
"err_msg": {
21+
"type": "timeout error",
22+
}
23+
}
24+
elif isinstance(exception, psycopg2.Error):
25+
# this means that this is a psycopg2 exception
26+
# since this is of type `psycopg2.Error` we can use https://www.psycopg.org/docs/module.html#psycopg2.Error
27+
body = {
28+
"err_msg": {
29+
"pgerror": exception.pgerror,
30+
"pgcode": exception.pgcode,
31+
"diag": None
32+
if exception.diag is None
33+
else {
34+
"message_primary": exception.diag.message_primary,
35+
"severity": exception.diag.severity,
36+
},
37+
}
38+
}
39+
1740
if isinstance(exception, psycopg2.IntegrityError):
1841
if "duplicate key" in err_msg:
1942
return DBResponse(response_code=409, body=json.dumps(body))

0 commit comments

Comments
 (0)