Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

refactor($browser): wrap Get(history.state) in try/catch #10369

Closed
wants to merge 1 commit into from

Conversation

caitp
Copy link
Contributor

@caitp caitp commented Dec 8, 2014

Reportedly, MSIE can throw under certain conditions when fetching this attribute.

Will confirm before landing.

Fixes #10367

Reportedly, MSIE can throw under certain conditions when fetching this attribute.

Will confirm before landing.

Fixes angular#10367
@googlebot
Copy link

CLAs look good, thanks!

@pkozlowski-opensource
Copy link
Member

It would be totally awesome to try to narrow down "certain conditions" IMO

@caitp
Copy link
Contributor Author

caitp commented Dec 8, 2014

Yeah --- I'll probably bug mr. terlson about it later (but I feel bad about doing that, which is why I really need to find more IE developers to bug with this stuff :>) maybe there's something on their issue tracker

@caitp
Copy link
Contributor Author

caitp commented Dec 8, 2014

FWIW I wasn't able to reproduce in SL, but that doesn't mean much --- the IE feedback site is too hard to find anything on, so it's gonna come down to asking I think

@@ -235,10 +235,22 @@ function Browser(window, document, $log, $sniffer) {

// This variable should be used *only* inside the cacheState function.
var lastCachedState = null;
function getCurrentState() {
try {
var state = history.state;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious: Why not use window ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we cache history anyways, might as well use it

@shahata
Copy link
Contributor

shahata commented Jan 25, 2015

@caitp any progress with this? Apparently we are experiencing this issue in one of our apps as well.

@caitp
Copy link
Contributor Author

caitp commented Jan 25, 2015

try the patch and see if it fixes it, i have no idea how we test this since nobody has produced a repro scenario

@shahata
Copy link
Contributor

shahata commented Jan 25, 2015

Yeah, I found out a couple of days ago that we've been using this patch for a while now in some project. @donataswix any chance you can post a reproduce of this issue?

@caitp
Copy link
Contributor Author

caitp commented Jan 25, 2015

Yeah, I found out a couple of days ago that we've been using this patch for a while now in some project.

does that mean the patch solved some issue, or it's still causing problems?

@shahata
Copy link
Contributor

shahata commented Jan 25, 2015

Yes, this patch solved the issue. Hopefully @donataswix will be able to add more details tomorrow.

@donataswix
Copy link

The patch solved the issue, I'll try to make reproducible example later today or at least figure out some more details.

@petebacondarwin petebacondarwin modified the milestones: 1.4.0-beta.3 / 1.3.12, Backlog Jan 27, 2015
@petebacondarwin
Copy link
Contributor

@donataswix - that would be extremely helpful. Thanks

@petebacondarwin petebacondarwin self-assigned this Jan 27, 2015
@donataswix
Copy link

For better or worse I can no longer reproduce this no matter what I try. So even though that patch helped at the time, it's no longer necessary for us. Although, I'm not sure I want to remove it from our build...

If anyone's interested our setup has dynamically created iframe where our app is loaded. There is a lot of code that runs before ours, and it seems that that code was responsible for creating preconditions for bug to appear.

function getCurrentState() {
try {
var state = history.state;
if (state === void 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should change isDefined() to use this method of testing for undefined and use it here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since history.state is either going to be a valid object, undefined or null, or it is going to throw an error we can simply do:

function getCurrentState() {
  try {
    return history.state || null;
  } catch(e) {
    return null;
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not going to be either a valid object, undefined or null, eg https://dxr.mozilla.org/mozilla-central/source/dom/webidl/History.webidl#18

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be "false", could be "0", could be the empty string, etc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically, it can legally be undefined, too... so hmm

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we are pretending that it will never be undefined : https://github.com/angular/angular.js/blob/master/src/ng/browser.js#L159-L164

@petebacondarwin
Copy link
Contributor

Given that whatever we do here it is not going to be correct, we should keep the behaviour as close to what it is currently, so can we simply go with the following?

function getCurrentState() {
  try {
    return history.state;
  } catch (e) {
    // MSIE can reportedly throw when there is no state (UNCONFIRMED).
    return null;
  }
}

function cacheState() {
  cachedState = getCurrentState();
  cachedState = isUndefined(cachedState) ? null : cachedState;
}

Is that OK with you @caitp ?

@dbabaioff
Copy link

We also are experiencing this issue. We tried the patch and didn't work (it didn't throw an exception). To solve we did like this (when we load the page):

window.history._state = null;

Object.defineProperty(window.history, 'state', {
    get: function() { return window.history._state; },
    set: function(state) { window.history._state = state; }
});

@petebacondarwin
Copy link
Contributor

@dbabaioff - when you say it didn't throw an exception with this patch, do you mean that you get the error message without an error being thrown? Are you able to create a reproduction of this that we could add to our unit tests?

@dbabaioff
Copy link

@petebacondarwin - yes - I get the error message ("SCRIPT16389: Unspecified error .") without an error being throw. I'll try to create a reproduction of this.

@petebacondarwin
Copy link
Contributor

@dbabaioff - did you ever manage to create a reproduction of this without an error being thrown?

caitp added a commit that referenced this pull request Mar 2, 2015
Reportedly, MSIE can throw under certain conditions when fetching this attribute.
We don't have a reliable reproduction for this but it doesn't do any real harm
to wrap access to this variable in a try-catch block.

Fixes #10367
Closes #10369
@caitp caitp closed this in 3b8163b Mar 2, 2015
hansmaad pushed a commit to hansmaad/angular.js that referenced this pull request Mar 10, 2015
Reportedly, MSIE can throw under certain conditions when fetching this attribute.
We don't have a reliable reproduction for this but it doesn't do any real harm
to wrap access to this variable in a try-catch block.

Fixes angular#10367
Closes angular#10369
netman92 pushed a commit to netman92/angular.js that referenced this pull request Aug 8, 2015
Reportedly, MSIE can throw under certain conditions when fetching this attribute.
We don't have a reliable reproduction for this but it doesn't do any real harm
to wrap access to this variable in a try-catch block.

Fixes angular#10367
Closes angular#10369
@filipposarzana
Copy link

@dbabaioff yes! it worked for me like a charm! Thanks!!!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"SCRIPT16389: Unspecified error" from AngularJs in IE if no browser history
9 participants