0

I am debugging a weird problem that I notice on my application's logs, but is unable to reproduce. My app goes something like this:

if (typeof document.cookie !== 'string')
   throw new TypeError('how can this happen?');

In theory, this should never happen. However, I found log entries that indicates for a particular user, he does get this error; however, I have no idea how that can be possible. The user is using Windows Chrome /91.0.4472.124.

I cannot reproduce the error and I am running out of ideas on what it could be. Can anyone think of possible ways this could happen?

Adrian Pang
  • 1,125
  • 6
  • 12
  • Possibilities include users with cookies disabled. Bots/spiders imitating web-browsers. Obscure mobile browsers with custom security policies. Or if your script is running in a cross-origin context and the browser is preventing your script from accessing cookies you don't have permission to read. – Dai Jul 21 '21 at 06:02
  • But wouldn't that just make document.cookie = empty string, which is still typeof = 'string'? – Adrian Pang Jul 21 '21 at 06:06
  • Correction: I have now found 2 separate users who have this problem, which isn't a lot, but it's weird. – Adrian Pang Jul 21 '21 at 06:20
  • I suggest you change your code to log the result of the `typeof` operator in the `TypeError`'s `message` - and get a copy of the value's `toString()` if possible so you can se what it _really_ is. e.g. `if( typeof document.cookie !== 'string` ) throw new TypeError( "Expected document.cookie to be of type string but encountered " + ( typeof document.cookie ) + ( document.cookie || "" ).toString() )` - that will be far more useful than your current const message. – Dai Jul 21 '21 at 06:46
  • Also consider checking various browser features to see if the user is running in Incognito/private-mode - though this varies by browser: https://stackoverflow.com/questions/2909367/can-you-determine-if-chrome-is-in-incognito-mode-via-a-script – Dai Jul 21 '21 at 06:47

0 Answers0