Error in local storage - NS_ERROR_FILE_CORRUPTED - firefox
I've been working in a web application and I'm using local storage. But for some Firefox users I notice that they're having the following error:
NS_ERROR_FILE_CORRUPTED: Component returned failure code: 0x8052000b (NS_ERROR_FILE_CORRUPTED) [nsIDOMStorage.setItem]
when it called the function:
function setLocalStorageItem(key, value){
localStorage.setItem(key, JSON.stringify(value));
}
It's is a way to avoid this error?
After an OS crash files within the Firefox profile folder might be corrupt and lead to non-functional websites (in my case ironically the Firefox marketplace). Here, webappsstore.sqlite
was affected.
As user @Oli stated over at Ask Ubuntu
Firefox stores its HTML5 data in a file called webappsstore.sqlite. That's sitting in your profile directory which lurks somewhere in ~/.mozilla/firefox/....default/ (depending on what your profile is called).
Move that out the way and restart Firefox and everything will come back to life.
More: https://developer.mozilla.org/en/dom/storage
If deleted/moved out of your profile folder, Firefox builds a new, sanitized webappsstore.sqlite
file. Worked for me.
Information on where to find your profile folder can be accessed here.
This is a browser-level error: you probably didn't do anything wrong to cause this error. The browser (or the the SQLite library it uses) either did something wrong, or the file was left in an invalid state due to a hardware problem.
You can't really prevent this issue, except by joining the Firefox development team and making the browser's storage system more fault-resistant. There doesn't seem to be any way to restore data from this error, so what you'll have to do is detect this error and tell users how to blow away their browser storage according to this MDN post:
try {
setLocalStorageItem(key, value);
} catch(e) {
if(e.name == "NS_ERROR_FILE_CORRUPTED") {
showMessageSomehow("Sorry, it looks like your browser storage has been corrupted. Please clear your storage by going to Tools -> Clear Recent History -> Cookies and set time range to 'Everything'. This will remove the corrupted browser storage across all sites.");
}
}
Note that the catch
block should verify that the error is an NS_ERROR_FILE_CORRUPTED
error. I think my check on e.name
is correct, but you should verify it for yourself.
Clearing everything via the Firefox preferences may not fully clear the local storage where the corrupted SQLite file resides.
At this point, you have two options:
- Pop open your console and clear the storage via a Javascript command (hat tip: https://davidjb.com/blog/2017/11/resolving-an-ns_error_file_corrupted-error-in-mozilla-firefox/):
localStorage.clear()
sessionStorage.clear()
- Use the terminal to delete the corrupted SQLite file and force Firefox to rebuild it.
Steps for macOS users:
cd /Users/myusername/Library/Application Support/Firefox/Profiles/.....default/
rm webappsstore.sqlite
-
Verify no other files corrupted using this script from TheConstructor:
for i in $(find . -name '*.sqlite'); do echo "$i"; echo "PRAGMA integrity_check;" | sqlite3 -bail "$i"; done
- Restart Firefox and reload the page.