comparison libervia/pages/_bridge/page_meta.py @ 1296:b1215347b5c3

pages (bridge): better handling of errors: when a BridgeError is catched, set a PROXY_ERROR code, and build a JSON object from the exception.
author Goffi <goffi@goffi.org>
date Fri, 19 Jun 2020 16:47:51 +0200
parents 7cec74557aa3
children 7472d5a88006
comparison
equal deleted inserted replaced
1295:0930b06f022f 1296:b1215347b5c3
6 import json 6 import json
7 from twisted.internet import defer 7 from twisted.internet import defer
8 from twisted.web import static 8 from twisted.web import static
9 from sat.core.i18n import _ 9 from sat.core.i18n import _
10 from sat.core.log import getLogger 10 from sat.core.log import getLogger
11 from sat_frontends.bridge.bridge_frontend import BridgeException
11 from libervia.server.constants import Const as C 12 from libervia.server.constants import Const as C
12 from libervia.server.utils import ProgressHandler 13 from libervia.server.utils import ProgressHandler
13 14
14 15
15 log = getLogger(__name__) 16 log = getLogger(__name__)
50 log.warning(_( 51 log.warning(_(
51 "{profile!r} has sent a badly formatted method call: {method_data}" 52 "{profile!r} has sent a badly formatted method call: {method_data}"
52 ).format(profile=profile, method_data=method_data)) 53 ).format(profile=profile, method_data=method_data))
53 return self.pageError(request, C.HTTP_BAD_REQUEST) 54 return self.pageError(request, C.HTTP_BAD_REQUEST)
54 55
55 if "profile" in kwargs: 56 if "profile" in kwargs or "profile_key" in kwargs:
56 log.warning(_( 57 log.warning(_(
57 '"profile" key should not be in method kwargs, hack attempt? ' 58 '"profile" key should not be in method kwargs, hack attempt? '
58 "profile={profile}, method_data={method_data}" 59 "profile={profile}, method_data={method_data}"
59 ).format(profile=profile, method_data=method_data)) 60 ).format(profile=profile, method_data=method_data))
60 return self.pageError(request, C.HTTP_BAD_REQUEST) 61 return self.pageError(request, C.HTTP_BAD_REQUEST)
61 62
62 ret = await bridge_method(*args, **kwargs, profile=profile) 63 try:
64 ret = await bridge_method(*args, **kwargs, profile=profile)
65 except BridgeException as e:
66 request.setResponseCode(C.HTTP_PROXY_ERROR)
67 ret = {
68 "fullname": e.fullname,
69 "message": e.message,
70 "condition": e.condition,
71 "module": e.module,
72 "classname": e.classname,
73 }
63 return json.dumps(ret) 74 return json.dumps(ret)