forked from OpenG2P/openg2p-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.py
66 lines (51 loc) · 2.37 KB
/
http.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
60
61
62
63
64
65
66
import json
from werkzeug.exceptions import BadRequest, InternalServerError
import odoo
from odoo.http import Root
from odoo.tools.config import config
from odoo.addons.base_rest.core import _rest_services_routes
from odoo.addons.base_rest.http import HttpRestRequest, JSONEncoder, wrapJsonException
from .exceptions.base_exception import G2PApiException, G2PApiValidationError
from .models.error_response import G2PErrorResponse
def g2pFixException(exception, original_exception=None):
def get_body(environ=None, scope=None):
if original_exception and isinstance(original_exception, G2PApiException):
res = G2PErrorResponse(
errorCode=original_exception.error_code,
errorMessage=original_exception.error_message,
errorDescription=original_exception.error_description or "",
).dict()
else:
extra_info = getattr(exception, "rest_json_info", None)
extra_info = json.dumps(extra_info) if extra_info else ""
res = G2PErrorResponse(
errorCode=exception.code,
errorMessage=exception.get_description(environ),
errorDescription=extra_info,
).dict()
if config.get_misc("base_rest", "dev_mode"):
# return exception info only if base_rest is in dev_mode
res.update({"traceback": exception.traceback})
return JSONEncoder().encode(res)
exception.get_body = get_body
return exception
class G2PHttpRestRequest(HttpRestRequest):
def _handle_exception(self, exception):
res = super()._handle_exception(exception)
if isinstance(exception, G2PApiValidationError):
res = wrapJsonException(BadRequest(exception.args[0]))
elif isinstance(exception, G2PApiException):
res = wrapJsonException(InternalServerError(exception.args[0]))
g2pFixException(res, exception)
return res
ori_get_request = Root.get_request
def get_request(self, httprequest):
db = httprequest.session.db
if db and odoo.service.db.exp_db_exist(db):
odoo.registry(db)
rest_routes = _rest_services_routes.get(db, [])
for root_path in rest_routes:
if httprequest.path.startswith(root_path):
return G2PHttpRestRequest(httprequest)
return ori_get_request(self, httprequest)
Root.get_request = get_request