Skip to content

Commit a94b638

Browse files
committed
Warn instead of throw for idb errors
1 parent f5426a5 commit a94b638

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

packages/app/src/errors.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ export const enum AppError {
2424
APP_DELETED = 'app-deleted',
2525
INVALID_APP_ARGUMENT = 'invalid-app-argument',
2626
INVALID_LOG_ARGUMENT = 'invalid-log-argument',
27-
STORAGE_OPEN = 'storage-open',
28-
STORAGE_GET = 'storage-get',
29-
STORAGE_WRITE = 'storage-set',
30-
STORAGE_DELETE = 'storage-delete'
27+
IDB_OPEN = 'idb-open',
28+
IDB_GET = 'idb-get',
29+
IDB_WRITE = 'idb-set',
30+
IDB_DELETE = 'idb-delete'
3131
}
3232

3333
const ERRORS: ErrorMap<AppError> = {
@@ -43,14 +43,14 @@ const ERRORS: ErrorMap<AppError> = {
4343
'Firebase App instance.',
4444
[AppError.INVALID_LOG_ARGUMENT]:
4545
'First argument to `onLog` must be null or a function.',
46-
[AppError.STORAGE_OPEN]:
47-
'Error thrown when opening storage. Original error: {$originalErrorMessage}.',
48-
[AppError.STORAGE_GET]:
49-
'Error thrown when reading from storage. Original error: {$originalErrorMessage}.',
50-
[AppError.STORAGE_WRITE]:
51-
'Error thrown when writing to storage. Original error: {$originalErrorMessage}.',
52-
[AppError.STORAGE_DELETE]:
53-
'Error thrown when deleting from storage. Original error: {$originalErrorMessage}.'
46+
[AppError.IDB_OPEN]:
47+
'Error thrown when opening IndexedDB. Original error: {$originalErrorMessage}.',
48+
[AppError.IDB_GET]:
49+
'Error thrown when reading from IndexedDB. Original error: {$originalErrorMessage}.',
50+
[AppError.IDB_WRITE]:
51+
'Error thrown when writing to IndexedDB. Original error: {$originalErrorMessage}.',
52+
[AppError.IDB_DELETE]:
53+
'Error thrown when deleting from IndexedDB. Original error: {$originalErrorMessage}.'
5454
};
5555

5656
interface ErrorParams {
@@ -59,10 +59,10 @@ interface ErrorParams {
5959
[AppError.DUPLICATE_APP]: { appName: string };
6060
[AppError.APP_DELETED]: { appName: string };
6161
[AppError.INVALID_APP_ARGUMENT]: { appName: string };
62-
[AppError.STORAGE_OPEN]: { originalErrorMessage?: string };
63-
[AppError.STORAGE_GET]: { originalErrorMessage?: string };
64-
[AppError.STORAGE_WRITE]: { originalErrorMessage?: string };
65-
[AppError.STORAGE_DELETE]: { originalErrorMessage?: string };
62+
[AppError.IDB_OPEN]: { originalErrorMessage?: string };
63+
[AppError.IDB_GET]: { originalErrorMessage?: string };
64+
[AppError.IDB_WRITE]: { originalErrorMessage?: string };
65+
[AppError.IDB_DELETE]: { originalErrorMessage?: string };
6666
}
6767

6868
export const ERROR_FACTORY = new ErrorFactory<AppError, ErrorParams>(

packages/app/src/indexeddb.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { FirebaseError } from '@firebase/util';
1819
import { DBSchema, openDB, IDBPDatabase } from 'idb';
1920
import { AppError, ERROR_FACTORY } from './errors';
2021
import { FirebaseApp } from './public-types';
2122
import { HeartbeatsInIndexedDB } from './types';
23+
import { logger } from './logger';
2224

2325
const DB_NAME = 'firebase-heartbeat-database';
2426
const DB_VERSION = 1;
@@ -47,7 +49,7 @@ function getDbPromise(): Promise<IDBPDatabase<AppDB>> {
4749
}
4850
}
4951
}).catch(e => {
50-
throw ERROR_FACTORY.create(AppError.STORAGE_OPEN, {
52+
throw ERROR_FACTORY.create(AppError.IDB_OPEN, {
5153
originalErrorMessage: e.message
5254
});
5355
});
@@ -65,9 +67,14 @@ export async function readHeartbeatsFromIndexedDB(
6567
.objectStore(STORE_NAME)
6668
.get(computeKey(app)) as Promise<HeartbeatsInIndexedDB | undefined>;
6769
} catch (e) {
68-
throw ERROR_FACTORY.create(AppError.STORAGE_GET, {
69-
originalErrorMessage: (e as Error)?.message
70-
});
70+
if (e instanceof FirebaseError) {
71+
logger.warn(e.message);
72+
} else {
73+
const idbGetError = ERROR_FACTORY.create(AppError.IDB_GET, {
74+
originalErrorMessage: (e as Error)?.message
75+
});
76+
logger.warn(idbGetError.message);
77+
}
7178
}
7279
}
7380

@@ -82,9 +89,14 @@ export async function writeHeartbeatsToIndexedDB(
8289
await objectStore.put(heartbeatObject, computeKey(app));
8390
return tx.done;
8491
} catch (e) {
85-
throw ERROR_FACTORY.create(AppError.STORAGE_WRITE, {
86-
originalErrorMessage: (e as Error)?.message
87-
});
92+
if (e instanceof FirebaseError) {
93+
logger.warn(e.message);
94+
} else {
95+
const idbGetError = ERROR_FACTORY.create(AppError.IDB_WRITE, {
96+
originalErrorMessage: (e as Error)?.message
97+
});
98+
logger.warn(idbGetError.message);
99+
}
88100
}
89101
}
90102

0 commit comments

Comments
 (0)