Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The app can not be initialised when the local storage value is invalid #319

Open
gedaiu opened this issue Jan 24, 2020 · 4 comments
Open

Comments

@gedaiu
Copy link

gedaiu commented Jan 24, 2020

It looks like if the stored value can't be parsed, an error is thrown and the default value will not be set.

@fsmanuel
Copy link
Member

Hey @gedaiu, do you have an example of the data?

@gedaiu
Copy link
Author

gedaiu commented Jan 27, 2020

you can reproduce it with any string that is not a json. You can try with something like this:

[{]}

or with a random string

asdfg

I think the problem is that the deserialization code assumes that the stored data is always valid.

@fsmanuel
Copy link
Member

What could be the right behavior if the deserialization doesn't work? Reset it to the initial data?

@gedaiu
Copy link
Author

gedaiu commented Jan 27, 2020

I'm not sure... right now, I do something like this when I start the app:

    try {
      JSON.parse(window.localStorage.getItem("storage:pending-data"));
    } catch(err) {
      window.localStorage.removeItem("storage:pending-data")
      // eslint-disable-next-line no-console
      console.log("the local storage is corrupted: ", err);
     }

but for me, it's not that critical if this data is lost.

i'm thinking that ideally, it would be nice to have something like this:

Storage.reopenClass({
  initialState() {
    return {
      lastKnownValue: null
    };
  },
  recoverState(stringValue) {
     /// ... do something here or just
     return this.initialState();
  }
});

or allow the Storage object to serialize/deserialize the data, and handle the errors if there are any... just like ember transforms.

I am not sure how other people use this library, but I am fine with either solution.

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

No branches or pull requests

2 participants