|
| 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() |
0 commit comments