Store: Error handling for setObject (#23650)

* Error handling for setObject to store

* Update public/app/core/store.ts

Co-Authored-By: kay delaney <45561153+kaydelaney@users.noreply.github.com>

* Update public/app/features/explore/RichHistory/RichHistory.tsx

Co-Authored-By: kay delaney <45561153+kaydelaney@users.noreply.github.com>

* Move setState outside of try-catch block

Co-authored-by: kay delaney <45561153+kaydelaney@users.noreply.github.com>
This commit is contained in:
Ivana Huckova
2020-04-20 15:13:02 +02:00
committed by GitHub
parent 4ea41dc19d
commit 174ee95153
6 changed files with 67 additions and 33 deletions

View File

@@ -29,21 +29,19 @@ export class Store {
return ret;
}
// Returns true when successfully stored
setObject(key: string, value: any): boolean {
/* Returns true when successfully stored, throws error if not successfully stored */
setObject(key: string, value: any) {
let json;
try {
json = JSON.stringify(value);
} catch (error) {
console.error(`Could not stringify object: ${key}. [${error}]`);
return false;
throw new Error(`Could not stringify object: ${key}. [${error}]`);
}
try {
this.set(key, json);
} catch (error) {
// Likely hitting storage quota
console.error(`Could not save item in localStorage: ${key}. [${error}]`);
return false;
throw new Error(`Could not save item in localStorage: ${key}. [${error}]`);
}
return true;
}