This repository has been archived by the owner on Jun 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jessiql.py
59 lines (52 loc) · 2.31 KB
/
jessiql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import jessiql
from contextlib import contextmanager
from apiens.util.exception import exception_from
from apiens.error import exc
@contextmanager # and a decorator
def converting_jessiql_errors(*, exc=exc, _=exc._):
""" Convert JessiQL errors into human-friendly Application Errors
Raises:
exc.E_API_ARGUMENT: query object failures: invalid columns and relationships
exc.F_BAD_API_REQUEST: other query errors, e.g. wrong use of cursors
"""
try:
yield
except jessiql.exc.BaseJessiqlException as e:
new_error = convert_jessiql_error(e, exc=exc, _=exc._)
raise new_error or e
def convert_jessiql_error(error: jessiql.exc.BaseJessiqlException, *, exc=exc, _=exc._):
""" Given a JessiQL exception, convert it into a `exc` API exception """
if isinstance(error, jessiql.exc.QueryObjectError):
e = exc.E_API_ARGUMENT(
_('Query Object error: {message}').format(message=str(error)),
_('Report this issue to the developer in order to have this query fixed'),
name='query',
)
return exception_from(e, error)
elif isinstance(error, jessiql.exc.InvalidColumnError):
e = exc.E_API_ARGUMENT.format(
_('Invalid column "{column_name}" for "{model}" specified in {where}'),
_('Report this issue to the developer in order to have this query fixed'),
name=error.where,
column_name=error.column_name,
model=error.model,
where=error.where,
)
return exception_from(e, error)
elif isinstance(error, jessiql.exc.InvalidRelationError):
e = exc.E_API_ARGUMENT.format(
_('Invalid relation "{column_name}" for "{model}" specified in {where}'),
_('Report this issue to the developer in order to have this query fixed'),
name=error.where,
column_name=error.column_name,
model=error.model,
where=error.where,
)
return exception_from(e, error)
elif isinstance(error, jessiql.exc.RuntimeQueryError):
e = exc.F_BAD_API_REQUEST.format(
_('JessiQL failed: {message}'),
_('Report this issue to the developer in order to have this query fixed'),
message=str(error)
)
return exception_from(e, error)