question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error handling on restore state

See original GitHub issue

We are using this with LZ-compression, but are running into problems especially on Firefox with restoring state. Firefox often can’t decompress the state we have put in there with LZ (don’t know why), but this results in the state being “restored” to null.

How would you recommending handling this, so instead of restoring a key state to null, it would just act as if there was nothing to restore, and use the default state defined in the reducer?

This is our current setup for reference:

  persistStore(store, {
    whitelist: ['chat', 'contacts', 'user'],
    transforms: [chatMessageHistoryTransform, compressor]
  }, rehydrationCallback);

So for example the LZ decompress of “contacts” might fail, and then we get out null instead of the default (an empty array []).

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

4reactions
omichelsencommented, Jun 14, 2016

Another update: I tried validating the rehydrated state in my reducer on the REHYDRATE event, but because of autoHydrate.js#L37 my (fixed) reducer state is always overwritten by the incoming garbage from the corrupted stored state.

I finally ended up writing middleware, that will modify the REHYDRATE payload if it is invalid. Don’t know if this is the “correct” way to do it, but here is the code:

import { REHYDRATE } from 'redux-persist/constants';

function validate(state, value) {
  return state
    && typeof state === typeof value
    && Object.keys(state).every((key) => key in value);
}

const validateRehydrateMiddleware = store => next => action => {
  switch (action.type) {
    case REHYDRATE:
      if (action.payload && typeof action.payload === 'object') {
        const newPayload = {};
        const state = store.getState();

        Object.keys(action.payload).forEach((key) => {
          if (validate(state[key], action.payload[key])) {
            newPayload[key] = action.payload[key];
          }
        });

        action.payload = newPayload;
      }
      break;
  }

  return next(action);
};

export default validateRehydrateMiddleware;
1reaction
omichelsencommented, Jul 5, 2016

Yes, it has been working well for us so far.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Restoring Your App's State | Apple Developer Documentation
This sample project demonstrates how to preserve your appʼs state information and restore the app to that previous state on subsequent launches.
Read more >
Restore Using TRY…CATCH might fail with error « Help
Restoring this backup in SQL 2012 using TRY CATCH would fail. Tip: Whenever I see any “Error: 928, Severity: 20, State: 1.
Read more >
ERR03-J. Restore prior object state on method failure
Although the logic restores the object's original state in the absence of this exception, the rollback code fails to execute in the event...
Read more >
How to process multiple error codes from a database restore ...
CATCH block but as a restore error tends to generate two codes, the first being the actual error, and the second being a...
Read more >
Best practices for error catching and handling
Restore state. After recovering from an error, your program needs to have the correct state. If it doesn't, then you haven't really recovered ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found