Hacker News new | ask | show | jobs
by josephcsible 1860 days ago
I think the point is that Google Analytics shouldn't be an essential part of your site, so it totally breaking for any reason should be unnoticeable to the end user.
2 comments

If it had broken, the code in the post would have worked. How exactly is code supposed to handle a dependency existing, but not being the API shape that it should be because a browser extension added an incomplete stub? Should every line of code get wrapped in try/catch?

There's a big difference between "resilient against a missing dependency" and "being resilient in the face of getting literally any object instead of the dependency you expected to get."

> Should every line of code get wrapped in try/catch?

Everything Google Analytics does is expendable, so the block of code that deals with it should be wrapped in try/catch.

> There's a big difference between "resilient against a missing dependency" and "being resilient in the face of getting literally any object instead of the dependency you expected to get."

If Google pushes a breaking change to the Google Analytics API tomorrow, that shouldn't break checkout either.

> Should every line of code get wrapped in try/catch?

For third party dependencies that might not get loaded for whatever reason? Yes, sure. There are better ways to do it, but that's the idea.

The code listed does check if it's been loaded. The stub that gets loaded is wrong. How do you guard against that?
It's not. It can work without it. The issue is something is pretending to be Google Analytics just enough to cause problems.