Add try/catch to all localStorage methods
Return null when attempting to get an item from localStorage REF: #2699
This commit is contained in:
parent
8a71d1df70
commit
e14d911195
@ -1,22 +1,43 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
|
// This is a simple localStorage wrapper because browser can throw errors
|
||||||
|
// in different situations, including:
|
||||||
|
// - Unable to store data if storage is full
|
||||||
|
// - Local storage is blocked if "third-party cookies and site data" is disabled
|
||||||
|
//
|
||||||
|
// For more details, see:
|
||||||
|
// https://stackoverflow.com/q/14555347/1935861
|
||||||
|
// https://github.com/thelounge/thelounge/issues/2699
|
||||||
|
// https://www.chromium.org/for-testers/bug-reporting-guidelines/uncaught-securityerror-failed-to-read-the-localstorage-property-from-window-access-is-denied-for-this-document
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
set(key, value) {
|
set(key, value) {
|
||||||
try {
|
try {
|
||||||
window.localStorage.setItem(key, value);
|
window.localStorage.setItem(key, value);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Do nothing. If we end up here, web storage quota exceeded, or user is
|
//
|
||||||
// in Safari's private browsing where localStorage's setItem is not
|
|
||||||
// available. See http://stackoverflow.com/q/14555347/1935861.
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
get(key) {
|
get(key) {
|
||||||
|
try {
|
||||||
return window.localStorage.getItem(key);
|
return window.localStorage.getItem(key);
|
||||||
|
} catch (e) {
|
||||||
|
// Return null as if data is not set
|
||||||
|
return null;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
remove(key) {
|
remove(key) {
|
||||||
|
try {
|
||||||
window.localStorage.removeItem(key);
|
window.localStorage.removeItem(key);
|
||||||
|
} catch (e) {
|
||||||
|
//
|
||||||
|
}
|
||||||
},
|
},
|
||||||
clear() {
|
clear() {
|
||||||
|
try {
|
||||||
window.localStorage.clear();
|
window.localStorage.clear();
|
||||||
|
} catch (e) {
|
||||||
|
//
|
||||||
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user