Handling g.user variable undefined errors?

137 Views Asked by At

Within our jinja templates we have a block of code that tries to create a span for the current user:

<span class="fa fa-user"></span> {{g.user.get_full_name()}}

However, if the user happens to clear the cookies on his browser, this g.user.get_full_name function will no longer exist, which causes the app to crash.

I don't necessarily want to reconstruct/preserve the g.user local proxy on cookie clear, but I at least want the app to handle it gracefully, maybe with a redirection to the login page. Is there any way to catch these exceptions for an arbitrary g.user.x reference?

1

There are 1 best solutions below

0
On

You can use something like

{{g.user.get_full_name() if g.user is not None else "do something"}}

or add the check on the back end and redirect when it is not present