svelte-persistent-store icon indicating copy to clipboard operation
svelte-persistent-store copied to clipboard

add optional chaining in response to sentry logs

Open nosovk opened this issue 1 year ago • 5 comments

Time to time there are errors in sentry:

Cannot read properties of null (reading 'getItem')
{0D3109ED-CF45-48B6-ADE9-A2DAA6C0B479}

nosovk avatar Dec 10 '24 00:12 nosovk

The storage parameter is required and can't be null as it must implement the interface StorageInterface

StorageInterface interface declaration
/**
 * Storage interface
 */
export interface StorageInterface<T> {
  /**
   * Get a value from the storage.
   *
   * If the value doesn't exist in the storage, `null` should be returned.
   * This method MUST be synchronous.
   * @param key The key/name of the value to retrieve
   */
  getValue(key: string): T | null

  /**
   * Save a value in the storage.
   * @param key The key/name of the value to save
   * @param value The value to save
   */
  setValue(key: string, value: T): void

  /**
   * Remove a value from the storage
   * @param key The key/name of the value to remove
   */
  deleteValue(key: string): void
}

To add the optional chaining to make code works, indicate that the compiler didn't do its job and allowed an invalid value.

Furthermore, the storage can't be defined later, so the nothing will ever be persisted in this case


If you need to conditionally set a storage, and need a "fallback" storage, you can use the createNoopStorage function (it will create a storage that don't persist anything) (The createNoopStorage is used when the requested storage is not available, like trying to use a localStorage on a Node.js server)

MacFJA avatar Dec 10 '24 23:12 MacFJA

Hm, I was using library within createLocalStorage, but because it's missing in some environments we got crashes in sentry. If there is an option to pass noop as a fallback it would be nice. Now, without that optional chaining it just throws exception and page stops loading. I think that not persisting data is better then unhandled exception.

nosovk avatar Dec 11 '24 21:12 nosovk

I was using library within createLocalStorage, but because it's missing in some environments we got crashes in sentry.

The issue seem to be here instead.

The createLocalStorage should fallback in noopStorage if the localStorage is not available: https://github.com/MacFJA/svelte-persistent-store/blob/main/src/core.ts#L267

Do you have more details about the environment that create the crashes ?

MacFJA avatar Dec 11 '24 21:12 MacFJA

It was safari mobile, I think it was private session in it. It seems that before latest releases localStorage was or absent, or with 0 size available. https://www.reddit.com/r/javascript/comments/2z06aq/local_storage_is_not_supported_with_safari_in/

nosovk avatar Dec 15 '24 10:12 nosovk

Like this https://developer.mozilla.org/en-US/docs/Web/API/StorageManager/estimate returning 0 for local storage.

nosovk avatar Dec 15 '24 10:12 nosovk