Skip to content

Commit

Permalink
Ensure all usages of localforage are using try/catch
Browse files Browse the repository at this point in the history
resolves #853
  • Loading branch information
paustint committed Apr 28, 2024
1 parent 4927cf9 commit 0083628
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 283 deletions.
10 changes: 0 additions & 10 deletions apps/jetstream/src/app/app-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,6 @@ export const selectUserPreferenceState = selector<UserProfilePreferences>({
const userPreferences = get(userPreferenceState);
return userPreferences;
},
// https://rollbar.com/jetstream/Jetstream/items/519/
// Causes error when used because this is async and async set selectors are not allowed
// set: async ({ set }, userPreferences: UserProfilePreferences) => {
// set(userPreferenceState, userPreferences);
// try {
// await localforage.setItem<UserProfilePreferences>(INDEXED_DB.KEYS.userPreferences, userPreferences);
// } catch (ex) {
// // could not save to localstorage
// }
// },
});

export const useUserPreferenceState = (): [UserProfilePreferences, (pref: UserProfilePreferences) => void] => {
Expand Down
13 changes: 11 additions & 2 deletions apps/jetstream/src/app/components/anonymous-apex/apex.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ export async function cleanUpHistoryState(): Promise<Record<string, ApexHistoryI
}

logger.info('[APEX-HISTORY][CLEANUP]', 'Keeping items', itemsToKeep);
await localforage.setItem<Record<string, ApexHistoryItem>>(INDEXED_DB.KEYS.apexHistory, itemsToKeep);
try {
await localforage.setItem<Record<string, ApexHistoryItem>>(INDEXED_DB.KEYS.apexHistory, itemsToKeep);
} catch (ex) {
logger.warn('[APEX-HISTORY][CLEANUP]', 'Error saving cleaned up apex history', ex);
}
return itemsToKeep;
}
} catch (ex) {
Expand All @@ -49,7 +53,12 @@ export async function cleanUpHistoryState(): Promise<Record<string, ApexHistoryI
}

const initApexHistory = async (): Promise<Record<string, ApexHistoryItem>> => {
return (await localforage.getItem<Record<string, ApexHistoryItem>>(INDEXED_DB.KEYS.apexHistory)) || {};
try {
return (await localforage.getItem<Record<string, ApexHistoryItem>>(INDEXED_DB.KEYS.apexHistory)) || {};
} catch (ex) {
logger.error('[APEX-HISTORY][INIT]', 'Error initializing apex history', ex);
return {};
}
};

/**
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ export function getLightningChangesetUrl(changeset: ChangeSet) {
}

export async function getHistory() {
return (await localforage.getItem<SalesforceDeployHistoryItem[]>(INDEXED_DB.KEYS.deployHistory)) || [];
try {
return (await localforage.getItem<SalesforceDeployHistoryItem[]>(INDEXED_DB.KEYS.deployHistory)) || [];
} catch (ex) {
logger.warn('[DEPLOY][HISTORY][GET ERROR]', ex);
return [];
}
}

export async function getHistoryItemFile(item: SalesforceDeployHistoryItem) {
Expand Down Expand Up @@ -129,11 +134,15 @@ export async function saveHistory({
}
const existingItems = await getHistory();
existingItems.unshift(newItem);
await localforage.setItem<SalesforceDeployHistoryItem[]>(INDEXED_DB.KEYS.deployHistory, existingItems.slice(0, MAX_HISTORY_ITEMS));
logger.log('[DEPLOY][HISTORY][SAVE]', { newItem });
try {
await localforage.setItem<SalesforceDeployHistoryItem[]>(INDEXED_DB.KEYS.deployHistory, existingItems.slice(0, MAX_HISTORY_ITEMS));
logger.log('[DEPLOY][HISTORY][SAVE]', { newItem });

if (file && newItem.fileKey && localforage.driver() === localforage.INDEXEDDB) {
await localforage.setItem(newItem.fileKey, file);
if (file && newItem.fileKey && localforage.driver() === localforage.INDEXEDDB) {
await localforage.setItem(newItem.fileKey, file);
}
} catch (ex) {
logger.warn('[DEPLOY][HISTORY][SAVE ERROR]', ex);
}
// delete files, if exists, for history items that were evicted from cache
try {
Expand All @@ -144,27 +153,9 @@ export async function saveHistory({
}
} catch (ex) {
logger.warn('[DEPLOY][HISTORY][CLEANUP ERROR] Error cleaning up files', ex);
logErrorToRollbar(
ex.message,
{
stack: ex.stack,
place: 'DeployMetadataHistory',
type: 'error cleaning up metadata history',
},
'warn'
);
}
} catch (ex) {
logger.warn('[DEPLOY][HISTORY][SAVE ERROR]', ex);
logErrorToRollbar(
ex.message,
{
stack: ex.stack,
place: 'DeployMetadataHistory',
type: 'error saving metadata history',
},
'warn'
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ export interface LoadSavedMappingItem {
}

const initLoadSavedMapping = async (): Promise<Record<string, Record<string, LoadSavedMappingItem>>> => {
const results = await localforage.getItem<Record<string, Record<string, LoadSavedMappingItem>>>(INDEXED_DB.KEYS.loadSavedMapping);
return results || {};
try {
return (await localforage.getItem<Record<string, Record<string, LoadSavedMappingItem>>>(INDEXED_DB.KEYS.loadSavedMapping)) || {};
} catch (ex) {
logger.error('Error getting loadSavedMapping from localforage', ex);
return {};
}
};

export const savedFieldMappingState = atom<Record<string, Record<string, LoadSavedMappingItem>>>({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ export async function cleanUpHistoryState(): Promise<Record<string, QueryHistory
}

export async function initQueryHistory(): Promise<Record<string, QueryHistoryItem>> {
return (await localforage.getItem<Record<string, QueryHistoryItem>>(INDEXED_DB.KEYS.queryHistory)) || {};
try {
return (await localforage.getItem<Record<string, QueryHistoryItem>>(INDEXED_DB.KEYS.queryHistory)) || {};
} catch (ex) {
logger.error('[QUERY-HISTORY][INIT]', 'Error initializing query history', ex);
return {};
}
}

// FIXME: there is some really poor naming conventions surrounding the entire query history
Expand Down Expand Up @@ -126,7 +131,10 @@ export const queryHistoryState = atom<Record<string, QueryHistoryItem>>({
.then((storedHistory) =>
localforage.setItem<Record<string, QueryHistoryItem>>(INDEXED_DB.KEYS.queryHistory, { ...storedHistory, ...newQueryHistory })
)
.then(() => setSelf(newQueryHistory));
.then(() => setSelf(newQueryHistory))
.catch((ex) => {
logger.error('[QUERY-HISTORY][SAVE]', 'Error saving query history', ex);
});
});
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,13 @@ export async function cleanUpHistoryState(): Promise<Record<string, SalesforceAp
}
}

function initSalesforceApiHistory(): Promise<Record<string, SalesforceApiHistoryItem>> {
return localforage.getItem<Record<string, SalesforceApiHistoryItem>>(INDEXED_DB.KEYS.salesforceApiHistory).then((item) => item || {});
async function initSalesforceApiHistory(): Promise<Record<string, SalesforceApiHistoryItem>> {
try {
return (await localforage.getItem<Record<string, SalesforceApiHistoryItem>>(INDEXED_DB.KEYS.salesforceApiHistory)) || {};
} catch (ex) {
logger.error('Error getting salesforceApiHistory from localforage', ex);
return {};
}
}

/**
Expand Down
Loading

0 comments on commit 0083628

Please sign in to comment.