Support » Plugin: Google Analyticator » Solution for "Fatal error: Uncaught exception 'Google_AuthException'"

  • This error was mentioned in other threads but I’m starting a new one because I have tested this and it still affects the latest version (6.4.3). I also have a code solution below.

    I first noticed the error on sites where I had Google Analyticator installed previously, updated the plugin (and WP to 3.5 simultaneously) and everything seemed to work. At some point the authorization broke and the Fatal Error started happening. My site managers noticed because they could not edit their widgets.

    This particular message affects both the GA settings screen and the widgets screen. In both cases the page only renders until the time the exception is “uncaught”, resulting is completly broken screens. This means that when the bug happens in the GA settings users are unable to do ANYTHING. All you can see is the “enabled/disabled” select menu and the error. There’s no way to even act on the select menu because the form is not complete and cannot be submitted. Same goes for the widgets, which cannot be looked at or modified at all.

    Here is an example of the error:

    Fatal error: Uncaught exception 'Google_AuthException' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }'' in /path/to/site/wp-content/plugins/google-analyticator/google-api-php-client/src/auth/Google_OAuth2.php:279 Stack trace: #0 /path/to/site/wp-content/plugins/google-analyticator/google-api-php-client/src/auth/Google_OAuth2.php(239): Google_OAuth2->refreshTokenRequest(Array) #1 /path/to/site/wp-content/plugins/google-analyticator/google-api-php-client/src/auth/Google_OAuth2.php(216): Google_OAuth2->refreshToken('1/8JhPDkzVXTxkR...') #2 /path/to/site/wp-content/plugins/google-analyticator/google-api-php-client/src/service/Google_ServiceResource.php(166): Google_OAuth2->sign(Object(Google_HttpRequest)) #3 /path/to/site/wp-content/plugins/google-analyticator/google-api-php-client/src/contrib/Google_AnalyticsService.php(54): Google_ServiceResource->__call('list', Array) #4 in /path/to/site/wp-content/plugins/google-analyticator/google-api-php-client/src/auth/Google_OAuth2.php on line 279

    As far as I can tell the Google_Oauth2.php file is not one you wrote, but rather an API file from Google. Either way the bug is in that file. If you got it from somewhere else please repost a version of this report for them to read and hopefully fix the problem upstream, but for now you should probably fix it in the plugin’s copy.

    I am not experienced at all in the try/catch syntax of PHP (it is very modern compared to most PHP in WP-land) but I read the PHP documentation about exceptions and based on that it seems like the Google_OAuth2.php file is simply in violation of the fundamental precepts of a try/catch loop.

    “If an exception is not caught, a PHP Fatal Error will be issued with an “Uncaught Exception …” error”

    In Google_OAuth2::refreshToken() the method Google_OAuth2::refreshTokenRequest() is called, and refreshTokenRequest() “throws” exceptions in various scenarios where something is wrong. The problem is that the calling function (Google_OAuth2::refreshToken()) makes no attempt to “catch” these exceptions. It just stupidly fails with a fatal error and ruins the ability of users to interact with Google Analytics settings form.

    Now because I’m not familiar with Exceptions in general and the convoluted setup in Google_Oauth2.php in particular I don’t know the best way to solve this. The PHP docs mention set_exception_handler() as a potential avenue, but it seems like the real way to fix this is to have proper try/catch structures whenever a function is called that might throw an exception.

    I fixed my copy of Google_OAuth2.php so that the particular uncaught exception that was giving me problems would have a try/catch in it. This solved my problem and even though the form is still confusing, there is no longer a fatal error, so I can click the “reset+dauthorize” button at the bottom of the Analyticator settings and get things workign again.

    Here are my changes as a gist:

    FWIW if you can fix these try/catch errors you will be able to at least tell people to just use the reset link. It’s these fatal errors mid-pageview that are forcing you to tell people to mess around in the database where they dont’ belong.

    Overall you should also attempt to fix the issues themselves in the OAuth system that cause the exceptions, and handle those outcomes better (e.g. by having an explicit link saying “you should reset your authorization”), but the priority IMHO is catching these exceptions.

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Solution for "Fatal error: Uncaught exception 'Google_AuthException'"’ is closed to new replies.