How to manage errors with bokeh library (Python + JavaScript) and show them on the user interface?

886 views Asked by At

I am working in a Bokeh Server Application using the Python library "Bokeh" and I usually get two types of errors.

  • JavaScript errors on the Chromium console
  • Python errors on the server side, they appear in the Tornado logger. Bokeh works with the Tornado Web Server

What I could achieve is to create manual exceptions inheriting the Exception class in python. In this way I can write custom messages and run some actions when the error is raised with this class.

class ManualException(Exception):
    def __init__(self, value):
        self.value = value

        # some actions

    def __str__(self):
        return repr(
            'MANUAL ERROR\n' + self.value
        )

But if any other exception occurs then I do not know how to capture them to run some custom actions. I have tried this:

try:
    n = 9 / 0
except Exception:
    tb = sys.exc_info()[2]
    raise ManualException('ZERO ERROR').with_traceback(tb)

Here I have to catch any error manually and I raise them with my manual created class, then both appear in the python logger. Is this the proper way to do this?

2017-09-04 17:08:58,872 Error running application handler <bokeh.application.handlers.directory.DirectoryHandler object at 0x0000023E2435C208>: 'MANUAL ERROR\nZERO ERROR'
File "main.py", line 11, in <module>:
n = 9 / 0 Traceback (most recent call last):
File "[...]\main.py", line 11, in <module>
    n = 9 / 0
ZeroDivisionError: division by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "[...]\lib\site-packages\bokeh\application\handlers\code_runner.py", line 125, in run
    exec(self._code, module.__dict__)
File "[...]\main.py", line 14, in <module>
    raise ManualException('ZERO ERROR').with_traceback(tb)
File "[...]\main.py", line 11, in <module>
    n = 9 / 0
exceptions.ManualException: 'MANUAL ERROR\n'ZERO ERROR'

Anyway I want avoid this because I want to show all kind of errors to the user, even if I do not use try-except to capture exceptions.

On the other hand if a python error happens, a JavaScript message is shown. So I think the best option is to override some JavaScript method to run some custom js code. How could I achieve this? Is this achievable?

enter image description here

1

There are 1 answers

0
ChesuCR On BEST ANSWER

I got an answer in other SO question. Some listeners can be added to the window in order to get the JavaScript error messages

You can handle it as an event listener on window object.

window.onunhandledrejection = function(event) {
  console.warn(`UNHANDLED PROMISE REJECTION: ${event.reason}`);
};

window.onerror = function(event) {
  console.warn(`UNHANDLED ERROR: ${event.message}`);
};

Or also like this:

 window.addEventListener('error', function(event) { ... })

You can read more about the unhandledrejection event on the MDN web docs here and the onerror event on the docs here