This commit is contained in:
@@ -4,12 +4,29 @@
|
||||
*/
|
||||
const PREFIX = 'discord-clone-settings:';
|
||||
|
||||
// Browsers spell the quota error in a few different ways across vendors.
|
||||
// Normalizing here so callers can `if (err.isQuotaError)` regardless.
|
||||
function isQuotaError(e) {
|
||||
if (!e) return false;
|
||||
const name = e.name || '';
|
||||
const code = e.code;
|
||||
return (
|
||||
name === 'QuotaExceededError' ||
|
||||
name === 'NS_ERROR_DOM_QUOTA_REACHED' ||
|
||||
code === 22 ||
|
||||
code === 1014
|
||||
);
|
||||
}
|
||||
|
||||
export default {
|
||||
get(key) {
|
||||
try {
|
||||
const raw = localStorage.getItem(PREFIX + key);
|
||||
return Promise.resolve(raw !== null ? JSON.parse(raw) : undefined);
|
||||
} catch {
|
||||
// Corrupted JSON or blocked storage access — return undefined so
|
||||
// the caller falls back to defaults. Worth recovering silently
|
||||
// here because a single bad key shouldn't take down the app.
|
||||
return Promise.resolve(undefined);
|
||||
}
|
||||
},
|
||||
@@ -18,8 +35,17 @@ export default {
|
||||
try {
|
||||
localStorage.setItem(PREFIX + key, JSON.stringify(value));
|
||||
return Promise.resolve();
|
||||
} catch {
|
||||
return Promise.resolve();
|
||||
} catch (e) {
|
||||
if (isQuotaError(e)) {
|
||||
// Reject so the caller can surface the quota error to the user —
|
||||
// silently swallowing meant settings just "didn't save" with no
|
||||
// warning. Decorate with a flag so callers can branch without
|
||||
// sniffing error names themselves.
|
||||
const err = new Error('Browser storage quota exceeded');
|
||||
err.isQuotaError = true;
|
||||
return Promise.reject(err);
|
||||
}
|
||||
return Promise.reject(e);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user