Logging errors to external database

Hi all,

I have a multipage app here that has multiple users in their own session. They sometimes encounter logic errors that get displayed on screen and stop the app.

I know how to write securely to a database that I host on Render.

My question is does anyone know how to extract the error message so that I can write it to the database against the user’s session?

Example below:

TypeError: 'NoneType' object is not subscriptable
Traceback:
<the trace back errors>

Thanks!

Is this what you are looking for?

Hi @Goyo thanks for the speedy reply.

What you suggested could be what I’m looking for, and naturally I am familiar with try except blocks. Never though to do it so “globally” on a page though!

Referring to this documentation, would that mean effectively wrapping my entire page in a try …except (RuntimeError, TypeError, NameError) block so that if anything fails on that page, it results in an exception?

Well, I am not sure what the scope of your question is. AFAIK you can extract the error message only from the exception, and you can have the exception only if you catch it. So if you want to log messages raised by every line of code, you need every line of code wrapped.

Note that exception handling in the main script won’t catch exceptions raised in callbacks, you have to handle them separately.

Hi @Goyo I guess I am looking for a more “universal” solution so that if there is any logic error that occurs in a page that ends up emitting an error message and stops the app from running, I would like to capture it and store it in a database.

If the only way to do that is to wrap every line of code then that won’t be a practical solution.

If I could wrap the entire page then I guess that would be preferable but it doesn’t sound like that’s possible anyway.

Thanks for the suggestion.

How is this issue dealt with in practice? Surely this is a common scenario.

That is indeed possible, but not enough: you might need to wrap the callbacks too.

Note that this is not streamlit-specific. Logging exceptions is just one instance of handling exceptions, and exception handling in python means try ... except.

Even streamlit itself must be doing something like this, it needs to catch the exceptions in order to sent the error messages to the browser.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.