Skip to content

Commit 4016272

Browse files
committed
better error handling when failure to init
1 parent 33bfa8d commit 4016272

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

packages/idb-cache/src/index.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ export class IDBCache implements IDBCacheInterface {
187187
}, 10000);
188188
})
189189
.catch((error) => {
190-
console.error("Worker initialization failed:", error);
190+
this.workerInitializationFailed = true;
191+
console.warn("Worker initialization failed:", error);
191192
});
192193
}
193194

@@ -235,7 +236,6 @@ export class IDBCache implements IDBCacheInterface {
235236
try {
236237
await this.workerReadyPromise;
237238
} catch (error) {
238-
console.error("Worker failed to initialize:", error);
239239
if (error instanceof IDBCacheError) {
240240
throw error;
241241
}
@@ -433,7 +433,7 @@ export class IDBCache implements IDBCacheInterface {
433433
*/
434434
public async getItem(itemKey: string): Promise<string | null> {
435435
if (this.workerInitializationFailed) {
436-
return Promise.resolve(null);
436+
return null;
437437
}
438438

439439
try {
@@ -525,9 +525,10 @@ export class IDBCache implements IDBCacheInterface {
525525

526526
chunks.sort((a, b) => a.index - b.index);
527527

528+
const port = this.getPort();
528529
const decryptedChunks = await Promise.all(
529530
chunks.map(({ data: { iv, ciphertext } }) =>
530-
decryptChunk(this.getPort(), iv, ciphertext, this.pendingRequests)
531+
decryptChunk(port, iv, ciphertext, this.pendingRequests)
531532
)
532533
);
533534

@@ -576,7 +577,7 @@ export class IDBCache implements IDBCacheInterface {
576577
*/
577578
public async setItem(itemKey: string, value: string): Promise<void> {
578579
if (this.workerInitializationFailed) {
579-
return Promise.resolve();
580+
return;
580581
}
581582

582583
try {
@@ -707,7 +708,7 @@ export class IDBCache implements IDBCacheInterface {
707708
}
708709
} catch (error) {
709710
if (error instanceof WorkerInitializationError) {
710-
console.error("Worker port is not initialized:", error);
711+
console.error("Worker initialization error in setItem:", error);
711712
throw error;
712713
}
713714
if (error instanceof DatabaseError) {
@@ -734,7 +735,7 @@ export class IDBCache implements IDBCacheInterface {
734735
*/
735736
public async removeItem(itemKey: string): Promise<void> {
736737
if (this.workerInitializationFailed) {
737-
return Promise.resolve();
738+
return;
738739
}
739740

740741
try {
@@ -776,9 +777,7 @@ export class IDBCache implements IDBCacheInterface {
776777
*/
777778
public async count(): Promise<number> {
778779
if (this.workerInitializationFailed) {
779-
throw new WorkerInitializationError(
780-
"Worker initialization previously failed"
781-
);
780+
return 0;
782781
}
783782

784783
try {

0 commit comments

Comments
 (0)