$http, how to gracefully handle SyntaxError: Unexpected Token

Getting data from the server with $http I encountered this because someone turned weblogging on in Yii to debug something. So along with the json the logging was returned as well.

So I am wondering, is there a way to catch this when it’s returned and handle it instead of just writing it to console?

For instance, if WiFi is on and there is a hotspot that requres a login, when doing an $http call it will return that login page instead of the URL expected, so it causes an error. Like if you are expecting JSON it returns some webpage and of course $http sees it as invalid JSON.

I can’t figure out a way to catch that and give any hint of what might be wrong to the user.