Sending error message from server to user-interface

Hi guys, I am new to Jupyter Lab and I have been trying to do something but couldn’t. So the deal is that we want to transfer error message from the server side to somewhere in the front end. For example, here is an error message:

[E 2022-08-03 14:13:23.616 ServerApp] [Ploomber] An error occured when trying to initialize the pipeline. Cells won’t be injected until your pipeline processes correctly. See error details below.
Traceback (most recent call last):
File “/Users/xilinwang/Documents/ploomber/ploomber_fork/ploomber/src/ploomber/util/”, line 111, in _default_spec_load_generator
spec = DAGSpec(path_to_entry_point,…

It is showing only in the terminal where I run jupyter Lab. People sometimes don’t check it so I want it to be on my jupyter lab interface, maybe have a pop up tab displaying this error message. How can I achieve this? Thanks a lot! :smile:

I’m assuming this is a server extension. Is this an error raised handling a request? The standard way to do that is to include the message you want to propagate in the HTTP error message, e.g.

raise web.HTTPError(500, "specific custom message")

This should propagate to the client-side where the request is handled. If you’ve written the client side and it’s not showing errors, that may be because there’s missing error handling in the requests.

It’s trickier if this is an async error and not directly associated with any request - then you’d have to have some sort of notifications/messages API in your extension to collect async messages.

Yes, we have a server extension. We have a function triggered everytime users open a file as a notebook in jupyter lab, sometimes this function can fail, and its error message is showing in the terminal, we want it to be displayed on jupyter side. I am fairly new to jupyter, how do i write code in client side? I saw some thing online to write js code for client extension, but I don’t know where do I write them and how do I enable them. Thanks for your reply!

How are you hooking in your code to every time a file opens? If it causes the actual ContentsManager.get to raise, an error should propagate to the user, preventing the file from opening.

To be honest I am not sure. This is not my code I am just trying to add on a new feature. Currently the way it works is that we are injecting a new cell to the notebook if we open a .py file as a notebook, however, if there’s something wrong before this injection process, we can still open the .py file as a notebook but the cell won’t be injected. I think currently we just caught the exception on the server side and not raise anything. Could you elaborate more on raising the HTTPError? Thanks!

1 Like

Ah, if you still want it to succeed instead of fail, then raising isn’t going to help. Since you are already injecting a code cell, maybe the simplest way to add the message is to inject a markdown cell with the message instead?

There are plans to introduce an event system, which would allow such a message to be sent to the frontend, in jupyter-server 2.0; the design is described in An event system for Jupyter · Issue #780 · jupyter-server/jupyter_server · GitHub with work in progress in GitHub - jupyter/jupyter_events: Configurable event system for Jupyter applications and extensions.; there is also a work to add support for it in JupyterLab 4.0 (Add events service by afshin · Pull Request #12667 · jupyterlab/jupyterlab · GitHub).

In the meantime, you could attempt building of GitHub - jupytercalpoly/jupyterlab-notifications: Toast notifications for JupyterLab but it does not seem to be actively maintained, or implement a custom endpoint together with a jupyterlab extension that would check for notifications; you could either use a plain HTTP endpoint or reuse the websocket machinery from JupyterLab. For displaying the result, you could use the build-in log console: extension-examples/log-messages at master · jupyterlab/extension-examples · GitHub.

1 Like

Yeah we are going to take that approach most likely. However, it breaks the tests we have since markdown cells are not trusted. Do you have any ideas? But thanks for your help! Really appreciated it!

Will take a look, thanks!