Skip to content

Commit e44335c

Browse files
committed
[common] tryCatch throughout project
1 parent 6dd764f commit e44335c

9 files changed

Lines changed: 86 additions & 103 deletions

File tree

‎src/common/other.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,16 @@ export const errorNew = (message: string) => {
8585
throw new Error(message);
8686
};
8787

88-
export const tryCatch = async (
89-
action: () => unknown | Promise<unknown>,
90-
onIgnoredError?: (error: any) => void,
91-
then?: () => unknown,
92-
): Promise<void> => {
88+
export const tryCatch = async <Return>(
89+
action: () => Return | Promise<Return>,
90+
then1?: (error: any) => void,
91+
then2?: (error: any) => void,
92+
): Promise<Return | void> => {
9393
try {
94-
await action();
94+
return await action();
9595
} catch (error) {
9696
/*! istanbul ignore next */
97-
onIgnoredError?.(error);
98-
then?.();
97+
then1?.(error);
98+
then2?.(error);
9999
}
100100
};

‎src/persisters/common/database/commands.ts‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
objToArray,
2727
objValues,
2828
} from '../../../common/obj.ts';
29-
import {isUndefined, promiseAll} from '../../../common/other.ts';
29+
import {isUndefined, promiseAll, tryCatch} from '../../../common/other.ts';
3030
import {IdSet2, setAdd, setNew} from '../../../common/set.ts';
3131
import {COMMA, TRUE} from '../../../common/strings.ts';
3232
import {
@@ -309,11 +309,7 @@ export const getCommandFunctions = (
309309
): Promise<Return> => {
310310
let result;
311311
await databaseExecuteCommand('BEGIN');
312-
try {
313-
result = await actions();
314-
} catch (error) {
315-
onIgnoredError?.(error);
316-
}
312+
await tryCatch(async () => (result = await actions()), onIgnoredError);
317313
await databaseExecuteCommand('END');
318314
return result as Return;
319315
};

‎src/persisters/common/database/sqlite.ts‎

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type {
99
} from '../../../@types/persisters/index.d.ts';
1010
import {collValues} from '../../../common/coll.ts';
1111
import {IdObj} from '../../../common/obj.ts';
12-
import {startInterval, stopInterval} from '../../../common/other.ts';
12+
import {startInterval, stopInterval, tryCatch} from '../../../common/other.ts';
1313
import {EMPTY_STRING} from '../../../common/strings.ts';
1414
import {
1515
DATA_VERSION,
@@ -62,23 +62,25 @@ export const createCustomSqlitePersister = <
6262
let interval: NodeJS.Timeout;
6363

6464
const startPolling = () =>
65-
(interval = startInterval(async () => {
66-
try {
67-
const [{d, s, c}] = (await executeCommand(
68-
SELECT +
69-
// eslint-disable-next-line max-len
70-
` ${DATA_VERSION} d,${SCHEMA_VERSION} s,TOTAL_CHANGES() c FROM ${PRAGMA}${DATA_VERSION} JOIN ${PRAGMA}${SCHEMA_VERSION}`,
71-
)) as [IdObj<number>];
72-
if (d != dataVersion || s != schemaVersion || c != totalChanges) {
73-
if (dataVersion != null) {
74-
listener();
65+
(interval = startInterval(
66+
() =>
67+
tryCatch(async () => {
68+
const [{d, s, c}] = (await executeCommand(
69+
SELECT +
70+
// eslint-disable-next-line max-len
71+
` ${DATA_VERSION} d,${SCHEMA_VERSION} s,TOTAL_CHANGES() c FROM ${PRAGMA}${DATA_VERSION} JOIN ${PRAGMA}${SCHEMA_VERSION}`,
72+
)) as [IdObj<number>];
73+
if (d != dataVersion || s != schemaVersion || c != totalChanges) {
74+
if (dataVersion != null) {
75+
listener();
76+
}
77+
dataVersion = d;
78+
schemaVersion = s;
79+
totalChanges = c;
7580
}
76-
dataVersion = d;
77-
schemaVersion = s;
78-
totalChanges = c;
79-
}
80-
} catch {}
81-
}, autoLoadIntervalSeconds as number));
81+
}),
82+
autoLoadIntervalSeconds as number,
83+
));
8284

8385
const stopPolling = () => {
8486
dataVersion = schemaVersion = totalChanges = null;

‎src/persisters/persister-browser/index.ts‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ import type {
66
Persists as PersistsType,
77
} from '../../@types/persisters/index.d.ts';
88
import type {
9-
LocalPersister,
10-
SessionPersister,
119
createLocalPersister as createLocalPersisterDecl,
1210
createSessionPersister as createSessionPersisterDecl,
11+
LocalPersister,
12+
SessionPersister,
1313
} from '../../@types/persisters/persister-browser/index.d.ts';
1414
import type {Store} from '../../@types/store/index.d.ts';
1515
import {
1616
jsonParse,
1717
jsonParseWithUndefined,
1818
jsonStringWithUndefined,
1919
} from '../../common/json.ts';
20-
import {WINDOW} from '../../common/other.ts';
20+
import {tryCatch, WINDOW} from '../../common/other.ts';
2121
import {createCustomPersister} from '../common/create.ts';
2222

2323
type StorageListener = (event: StorageEvent) => void;
@@ -43,11 +43,7 @@ const createStoragePersister = (
4343
): StorageListener => {
4444
const storageListener = (event: StorageEvent): void => {
4545
if (event.storageArea === storage && event.key === storageName) {
46-
try {
47-
listener(jsonParse(event.newValue as string));
48-
} catch {
49-
listener();
50-
}
46+
tryCatch(() => listener(jsonParse(event.newValue as string)), listener);
5147
}
5248
};
5349
WINDOW.addEventListener(STORAGE, storageListener);

‎src/persisters/persister-indexed-db/index.ts‎

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
promiseNew,
1414
startInterval,
1515
stopInterval,
16+
tryCatch,
1617
} from '../../common/other.ts';
1718
import {T, V} from '../../common/strings.ts';
1819
import {createCustomPersister} from '../common/create.ts';
@@ -64,34 +65,36 @@ export const createIndexedDbPersister = ((
6465
);
6566
request.onupgradeneeded = () =>
6667
create &&
67-
arrayMap(OBJECT_STORE_NAMES, (objectStoreName) => {
68-
try {
69-
request.result.createObjectStore(objectStoreName, KEY_PATH);
70-
} catch {}
71-
});
72-
request.onsuccess = async () => {
73-
try {
74-
const transaction = request.result.transaction(
75-
OBJECT_STORE_NAMES,
76-
'readwrite',
77-
);
78-
const result = await promiseAll(
79-
arrayMap(
68+
arrayMap(OBJECT_STORE_NAMES, (objectStoreName) =>
69+
tryCatch(() =>
70+
request.result.createObjectStore(objectStoreName, KEY_PATH),
71+
),
72+
);
73+
request.onsuccess = () =>
74+
tryCatch(
75+
async () => {
76+
const transaction = request.result.transaction(
8077
OBJECT_STORE_NAMES,
81-
async (objectStoreName, index) =>
82-
await forObjectStore(
83-
transaction.objectStore(objectStoreName),
84-
params[index],
85-
),
86-
),
87-
);
88-
request.result.close();
89-
resolve(result as [any, any]);
90-
} catch (e) {
91-
request.result.close();
92-
reject(e);
93-
}
94-
};
78+
'readwrite',
79+
);
80+
const result = await promiseAll(
81+
arrayMap(
82+
OBJECT_STORE_NAMES,
83+
async (objectStoreName, index) =>
84+
await forObjectStore(
85+
transaction.objectStore(objectStoreName),
86+
params[index],
87+
),
88+
),
89+
);
90+
request.result.close();
91+
resolve(result as [any, any]);
92+
},
93+
(error) => {
94+
request.result.close();
95+
reject(error);
96+
},
97+
);
9598
request.onerror = () => reject('indexedDB.open error');
9699
});
97100

‎src/persisters/persister-pglite/index.ts‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {
1010
} from '../../@types/persisters/persister-pglite/index.d.ts';
1111
import type {Store} from '../../@types/store/index.d.ts';
1212
import {IdObj} from '../../common/obj.ts';
13-
import {noop} from '../../common/other.ts';
13+
import {noop, tryCatch} from '../../common/other.ts';
1414
import {createCustomPostgreSqlPersister} from '../common/database/postgresql.ts';
1515

1616
export const createPglitePersister = (async (
@@ -29,13 +29,7 @@ export const createPglitePersister = (async (
2929
channel: string,
3030
listener: DatabaseChangeListener,
3131
): Promise<() => Promise<void>> => await pglite.listen(channel, listener),
32-
async (unlisten: () => Promise<void>) => {
33-
try {
34-
await unlisten();
35-
} catch (e) {
36-
onIgnoredError?.(e);
37-
}
38-
},
32+
(unlisten: () => Promise<void>) => tryCatch(unlisten, onIgnoredError),
3933
onSqlCommand,
4034
onIgnoredError,
4135
noop,

‎src/persisters/persister-postgres/index.ts‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
createPostgresPersister as createPostgresPersisterDecl,
1010
} from '../../@types/persisters/persister-postgres/index.d.ts';
1111
import type {Store} from '../../@types/store/index.d.ts';
12+
import {tryCatch} from '../../common/other.ts';
1213
import {createCustomPostgreSqlPersister} from '../common/database/postgresql.ts';
1314

1415
export const createPostgresPersister = (async (
@@ -28,13 +29,8 @@ export const createPostgresPersister = (async (
2829
channel: string,
2930
listener: DatabaseChangeListener,
3031
): Promise<ListenMeta> => sql.listen(channel, listener),
31-
async (notifyListener: ListenMeta) => {
32-
try {
33-
await notifyListener.unlisten();
34-
} catch (e) {
35-
onIgnoredError?.(e);
36-
}
37-
},
32+
(notifyListener: ListenMeta) =>
33+
tryCatch(notifyListener.unlisten, onIgnoredError),
3834
onSqlCommand,
3935
onIgnoredError,
4036
() => commandSql?.release?.(),

‎src/store/index.ts‎

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ import {
101101
isTypeStringOrBoolean,
102102
isUndefined,
103103
slice,
104+
tryCatch,
104105
} from '../common/other.ts';
105106
import {
106107
Pair,
@@ -1221,29 +1222,26 @@ export const createStore: typeof createStoreDecl = (): Store => {
12211222
});
12221223

12231224
const setTablesJson = (tablesJson: Json): Store => {
1224-
try {
1225-
setOrDelTables(jsonParse(tablesJson));
1226-
} catch {}
1225+
tryCatch(() => setOrDelTables(jsonParse(tablesJson)));
12271226
return store;
12281227
};
12291228

12301229
const setValuesJson = (valuesJson: Json): Store => {
1231-
try {
1232-
setOrDelValues(jsonParse(valuesJson));
1233-
} catch {}
1230+
tryCatch(() => setOrDelValues(jsonParse(valuesJson)));
12341231
return store;
12351232
};
12361233

12371234
const setJson = (tablesAndValuesJson: Json): Store =>
1238-
fluentTransaction(() => {
1239-
try {
1240-
const [tables, values] = jsonParse(tablesAndValuesJson);
1241-
setOrDelTables(tables);
1242-
setOrDelValues(values);
1243-
} catch {
1244-
setTablesJson(tablesAndValuesJson);
1245-
}
1246-
});
1235+
fluentTransaction(() =>
1236+
tryCatch(
1237+
() => {
1238+
const [tables, values] = jsonParse(tablesAndValuesJson);
1239+
setOrDelTables(tables);
1240+
setOrDelValues(values);
1241+
},
1242+
() => setTablesJson(tablesAndValuesJson),
1243+
),
1244+
);
12471245

12481246
const setTablesSchema = (tablesSchema: TablesSchema): Store =>
12491247
fluentTransaction(() => {

‎src/synchronizers/index.ts‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
isUndefined,
3232
promiseNew,
3333
startTimeout,
34+
tryCatch,
3435
} from '../common/other.ts';
3536
import {getLatestTime, stampNew, stampNewObj} from '../common/stamps.ts';
3637
import {EMPTY_STRING} from '../common/strings.ts';
@@ -151,12 +152,12 @@ export const createCustomSynchronizer = (
151152
tablesStamp[1] = getLatestTime(tablesStamp[1], tablesTime2);
152153
};
153154

154-
const getChangesFromOtherStore = async (
155+
const getChangesFromOtherStore = (
155156
otherClientId: IdOrNull = null,
156157
otherContentHashes?: ContentHashes,
157158
transactionId: Id = getTransactionId(),
158-
): Promise<MergeableChanges | void> => {
159-
try {
159+
): Promise<MergeableChanges | void> =>
160+
tryCatch(async () => {
160161
if (isUndefined(otherContentHashes)) {
161162
[otherContentHashes, otherClientId, transactionId] =
162163
await request<ContentHashes>(
@@ -220,10 +221,7 @@ export const createCustomSynchronizer = (
220221
)[0],
221222
1,
222223
];
223-
} catch (error) {
224-
onIgnoredError?.(error);
225-
}
226-
};
224+
}, onIgnoredError);
227225

228226
const getPersisted = async (): Promise<MergeableContent | undefined> => {
229227
const changes = (await getChangesFromOtherStore()) as any;

0 commit comments

Comments
 (0)