Skip to content

Commit 1570c66

Browse files
committed
docs(examples): provide exception handling code example
1 parent 00980cb commit 1570c66

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

examples/exceptions/app_raw_api.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import logging
2+
import sys
3+
import traceback
4+
5+
from trame.app import TrameApp
6+
from trame.decorators import life_cycle
7+
from trame.ui.html import DivLayout
8+
from trame.widgets import html
9+
10+
# Disable default stdout exception
11+
logging.getLogger("wslink.protocol").setLevel(logging.CRITICAL)
12+
13+
14+
class MonitorException(TrameApp):
15+
def __init__(self, server=None):
16+
super().__init__(server)
17+
self._build_ui()
18+
19+
def _build_ui(self):
20+
with DivLayout(self.server) as self.ui:
21+
html.Button("Divide by 0", click=self.divide_by_zero)
22+
html.Button(
23+
"No method",
24+
click="trame.client.getConnection().getSession().call('no_rpc_endpoint', [], {})",
25+
)
26+
27+
@life_cycle.server_ready
28+
def _setup_wslink_listener(self, **_):
29+
self.server.protocol.log_emitter.add_event_listener(
30+
"exception", self.on_exception
31+
)
32+
self.server.protocol.log_emitter.add_event_listener("error", self.on_error)
33+
34+
def divide_by_zero(self):
35+
return 1 / 0
36+
37+
def on_exception(self, exception):
38+
print(">" * 60)
39+
print("exception ->", exception)
40+
traceback.print_exception(
41+
type(exception), exception, exception.__traceback__, file=sys.stdout
42+
)
43+
print("<" * 60)
44+
45+
def on_error(self, msg):
46+
print(">" * 60)
47+
print("error ->", msg)
48+
print("<" * 60)
49+
50+
51+
def main():
52+
app = MonitorException()
53+
app.server.start()
54+
55+
56+
if __name__ == "__main__":
57+
main()

examples/exceptions/app_trame.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import logging
2+
import sys
3+
import traceback
4+
5+
from trame.app import TrameApp
6+
from trame.decorators import life_cycle
7+
from trame.ui.html import DivLayout
8+
from trame.widgets import html
9+
10+
# Disable default stdout exception
11+
logging.getLogger("wslink.protocol").setLevel(logging.CRITICAL)
12+
13+
14+
class MonitorException(TrameApp):
15+
def __init__(self, server=None):
16+
super().__init__(server)
17+
self._build_ui()
18+
self.ctrl.on_exception.add(self.on_exception)
19+
20+
def _build_ui(self):
21+
with DivLayout(self.server) as self.ui:
22+
html.Button("Divide by 0", click=self.divide_by_zero)
23+
html.Button("JS error", click="console.error('Yo')")
24+
25+
def divide_by_zero(self):
26+
return 1 / 0
27+
28+
@life_cycle.exception
29+
def on_exception(self, exception):
30+
print(">" * 60)
31+
print("exception ->", exception)
32+
traceback.print_exception(
33+
type(exception), exception, exception.__traceback__, file=sys.stdout
34+
)
35+
print("<" * 60)
36+
37+
@life_cycle.error
38+
def on_js_error(self, msg):
39+
if msg == "[object Object]":
40+
# exception... => should fix JS to send a better msg/event
41+
return
42+
43+
print("JS error:", msg)
44+
45+
46+
def main():
47+
app = MonitorException()
48+
app.server.start()
49+
50+
51+
if __name__ == "__main__":
52+
main()

0 commit comments

Comments
 (0)