Linkedin Javascript SDK catch expired token

We use Linkedin to have our members “Sign in” to access our back end. We only use the Javascript SDK as our website is very simple. We have many users that come back after 6-months and receive the following error instead of seeing the “Sign in with Linkedin” button:

[unauthorized]. Expired access token. Timestamp: 1501736450333

Since we don’t have a backend to renew the tokens in batch prior to their 60 day expiration, a simple solution for our members is to Logout first, see the “Log in” button, and sign-in again — that we know re validates the token.

Question: I don’t know how to “catch” the error or identify when a user has an expired token with Linkenin’s Javascript SDK.

<script type="text/javascript" src="//platform.linkedin.com/in.js">
    api_key:   XXXXXXXXXXXXX
    onLoad:    li_OnFrameworkLoad
    authorize: true
</script>  

...

<script type="text/javascript">
function li_OnFrameworkLoad() {
  IN.Event.on(IN, "auth", li_OnAuth);
}
</script>

I have tried using the:

  if (!IN.User.isAuthorized()) {
    li_Logout();
  }

method, but that doesn’t work as

IN.Event.on(IN, "auth", li_OnAuth);

will trigger the “[unauthorized]…” message without first calling the “li_OnAuth” callback.

Reproducing: The worst part of this bug is that I can’t seem to reproduce it on my local machine, if you know how to trigger an “[unauthorized]. Expired access token…” bug please do let me know.

Solution?: One possible recent solution is using:

<script type="text/javascript">
window.onerror = function(e) {
  if (e == 'Script error.') {
    console.log('There is a chace IN.init failed.');
  }
}
</script>

But I am not very familiar with “window.onerror” and I am not sure if the Linkedin Javascript SDK will trigger an error if the token is expired, etc. Basically I just need to catch the error and call my “li_logout();” function to Log Out the user from Linkedin and refresh the page.

Thank you much, any wisdom would be highly appreciated.