Angular - How to reload app after js/ts error?

47 views Asked by At

Are there technologies or libraries capable of managing the refresh of an Angular app in case of typescript/javascript errors that can block the entire application? Is there a way to make the location.reload() function less invasive? How do you usually handle these kinds of errors?

1

There are 1 answers

3
Alberto Chiesa On

This is not a specific answer, but I think there are none, so I'm writing it anyway. There aren't any libraries that accomplish directly what you are suggesting.

There are two main real problems to solve, underlying the one described in this question:

  • how do you determine if the application has crashed or is blocked? The answer is different in different applications. Sometimes you have an application in an unrecoverable state, that is still somewhat responsive. A common case is an RxJs pipe without a catchError, that errors out without anyone recovering from it. A big part of the application could still work, but you cannot use it anymore because of the pipe failing. Then, there are unhandled exceptions, which are one of the few "smoking guns" when something bad happens. However, there could be unhandled exceptions which are not really a problem for the end user, and handled exceptions which are poorly handled and still block the application.
  • when you detect that something "very wrong" has happened, what do you do? If we are talking about uncaught exceptions going out, we are talking about cases that are not handled by the application (by definition). So the only option you are left with, AFAICT, is a hard reload. Because something you didn't expect to happen happens, resorting to a reboot seems the only option.

As you can see, those are not problems solvable in a generic, robust way. At least, as much as I know. But, in your specific application, there could be a way to solve those problems.

Error handling is a complex topic, and it must be handled in every possible way: every Rx pipe needs an error handler, every JSON.parse needs a try/catch, etc.
There are many things that can break, and robust code must count all of them. Unfortunately, JavaScript (and, to a far lesser extent, TypeScript) is a language particularly inept at identifying problems before they happen.