Issue with secureStorage throwing errors when key doesn't exist

Hello,

I’ve encountered an unexpected behavior when using secureStorage.getItem() to retrieve stored data. Specifically, when I try to access a key that does not exist in secureStorage, instead of returning null or undefined, it throws an error.


try {
  const { secureStorage } = await uxp.storage;
  const storedToken = await secureStorage.getItem("token");
  const decodedToken = decodeToken(storedToken);
  console.log("Stored token:", decodedToken);
} catch (error) {
  console.error("Error fetching token:", error);
}

In the code above, if the key “token” doesn’t exist in secureStorage, the getItem function throws an error. From my understanding of typical storage systems (like localStorage or sessionStorage), when a key is missing, the expected behavior is to return null or undefined, not throw an error.

I was wondering if this behavior is intended in the UXP secureStorage API. Is there a way to safely check if a key exists in secureStorage without relying on catching errors?

Any clarification or advice on how to handle this issue would be greatly appreciated!

Thank you in advance for your help!

Had to deal with exactly this a couple of days ago. According to docs it should throw an error only if the provided key is something other than string, but that’s not the case.

Most likely a bug, but I’m so tired of reporting them without any response most of the time :frowning: Even if it will be a confirmed bug, just put it into try/catch and don’t hold your breath for it to be fixed any time soon :slight_smile:

3 Likes

I don’t know what’s happening in decodeToken(storedToken);.
But why don’t You check, if storedToken is actually valid before passing it to decodeToken()?

try {
  const { secureStorage } = await uxp.storage;
  const storedToken = await secureStorage.getItem("token");
  if(!storedToken) return; // <== or whatever
  const decodedToken = decodeToken(storedToken);
  console.log("Stored token:", decodedToken);
} catch (error) {
  console.error("Error fetching token:", error);
}
1 Like

Problem is, that secureStorage.getItem("token") throws an error if there’s no item with token key

Ah ok, got it.
I thought it’s due to the invalid storedToken const. :confused:

Yeah, I suspected it might be a bug. Frustrating that these issues don’t get much attention. I’ll wrap it in a try/catch for now, thanks for confirming! Have you found any other quirks with secure storage?

Same with removeItem() :smiley:

What I have, is:

const secureStorageToString = (data) => {
    let secureKey = ""

    for (let i of data) {
        secureKey += String.fromCharCode(i)
    }

    return secureKey
}

export const removeSecure = async (key) => {
    try {
        storage.secureStorage.removeItem(key)
    } catch (e) {
        console.warn(e)
    }
}

export const setSecure = async (key, data) => storage.secureStorage.setItem(
    key,
    JSON.stringify(data),
)

export const getSecure = async (key)=> {
    try {
        return JSON.parse(secureStorageToString(
            await storage.secureStorage.getItem(key),
        ))
    } catch (e) {
        console.warn(e)
    }

    return null
}
1 Like

Thank you @ismail3762 @Karmalakas for bringing this issue to notice. I have created a UXP ticket. This should be a quick change and it will be fixed with next UXP release 8.2.

5 Likes