Skip to content

(atomWithStorage): Exceptions from Storage providers should not be silenced #1347

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

Closed
mwisnicki opened this issue Aug 15, 2022 · 6 comments · Fixed by #1363
Closed

(atomWithStorage): Exceptions from Storage providers should not be silenced #1347

mwisnicki opened this issue Aug 15, 2022 · 6 comments · Fixed by #1363
Assignees

Comments

@mwisnicki
Copy link

At the moment if anything throws inside StorageSync the error will be swallowed and not even logged in console.
This behavior is very unfriendly.

const storage = createJSONStorage(() => localStorage);
const prefsAtom = atomWithStorage(
  "prefs",
  { x: 1 },
  {
    ...storage,
    getItem: (key) => {
      console.log("loading", key);
      throw new Error("Cannot load prefs");
      return storage.getItem(key);
    }
  }
);

https://codesandbox.io/s/jotai-useatomcallback-async-error-forked-zr737n?file=/src/App.tsx:187-470

I had a bug where zod couldn't parse data but took me a while to figure out because of suppressed exceptions.

IMHO exceptions should be at the very least logged with console.error if not made fatal.

@dai-shi
Copy link
Member

dai-shi commented Aug 17, 2022

Good point. We should only catch if (a) storage doesn't have an item or (b) if JSON.parse fails with defaultStorage.

I can work on it unless anyone else would like to take it.

@dai-shi dai-shi changed the title Exceptions from Storage providers should not be silenced (atomWithStorage): Exceptions from Storage providers should not be silenced Aug 17, 2022
@joshmkennedy
Copy link

Would the solution to this be something like this, or is there a better way?

  const handleErr = ()=>{
    //Logic for deciding if return initialValue or not
  }
  const getInitialValue = () => {
    try {
      const value = storage.getItem(key)
      if (value instanceof Promise) {
        return value.catch(handleErr)
      }
      return value
    } catch (e) {
      return handleErr(e)
    }
  }

@dai-shi
Copy link
Member

dai-shi commented Aug 21, 2022

It's close. We should return initial value if (a) or (b), otherwise throw the error.

@dai-shi
Copy link
Member

dai-shi commented Aug 23, 2022

I'll work on it.

@dai-shi
Copy link
Member

dai-shi commented Aug 23, 2022

Could you try #1363?
https://ci.codesandbox.io/status/pmndrs/jotai/pr/1363
Find "Local Install Instruction" ☝️

@joshmkennedy
Copy link

Yeah, that works!
https://codesandbox.io/s/jotai-useatomcallback-async-error-forked-bgpmet

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

Successfully merging a pull request may close this issue.

3 participants