Skip to content

Commit 62512a2

Browse files
committed
feat: error handling of db query writes
1 parent 34e6af2 commit 62512a2

File tree

6 files changed

+263
-115
lines changed

6 files changed

+263
-115
lines changed

src/channel.ts

Lines changed: 136 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -392,17 +392,24 @@ export class Channel {
392392
throw Error(`Reaction object is missing`);
393393
}
394394

395-
const offlineDb = this.getClient().offlineDb;
396-
if (offlineDb) {
397-
return (await offlineDb.queueTask({
398-
task: {
399-
channelId: this.id as string,
400-
channelType: this.type,
401-
messageId: messageID,
402-
payload: [messageID, reaction, options],
403-
type: 'send-reaction',
404-
},
405-
})) as ReactionAPIResponse;
395+
try {
396+
const offlineDb = this.getClient().offlineDb;
397+
if (offlineDb) {
398+
return (await offlineDb.queueTask({
399+
task: {
400+
channelId: this.id as string,
401+
channelType: this.type,
402+
messageId: messageID,
403+
payload: [messageID, reaction, options],
404+
type: 'send-reaction',
405+
},
406+
})) as ReactionAPIResponse;
407+
}
408+
} catch (error) {
409+
this._client.logger('error', `offlineDb:send-reaction`, {
410+
tags: ['channel', 'offlineDb'],
411+
error,
412+
});
406413
}
407414

408415
return this._sendReaction(messageID, reaction, options);
@@ -446,28 +453,35 @@ export class Channel {
446453
);
447454
}
448455

449-
const offlineDb = this.getClient().offlineDb;
450-
if (offlineDb) {
451-
const message = this.state.messages.find(({ id }) => id === messageID);
452-
const reaction = {
453-
created_at: '',
454-
updated_at: '',
455-
message_id: messageID,
456-
type: reactionType,
457-
user_id: (this.getClient().userID as string) ?? user_id,
458-
};
459-
await offlineDb.deleteReaction({
460-
message,
461-
reaction,
462-
});
463-
return await offlineDb.queueTask({
464-
task: {
465-
channelId: this.id as string,
466-
channelType: this.type,
467-
messageId: messageID,
468-
payload: [messageID, reactionType],
469-
type: 'delete-reaction',
470-
},
456+
try {
457+
const offlineDb = this.getClient().offlineDb;
458+
if (offlineDb) {
459+
const message = this.state.messages.find(({ id }) => id === messageID);
460+
const reaction = {
461+
created_at: '',
462+
updated_at: '',
463+
message_id: messageID,
464+
type: reactionType,
465+
user_id: (this.getClient().userID as string) ?? user_id,
466+
};
467+
await offlineDb.deleteReaction({
468+
message,
469+
reaction,
470+
});
471+
return await offlineDb.queueTask({
472+
task: {
473+
channelId: this.id as string,
474+
channelType: this.type,
475+
messageId: messageID,
476+
payload: [messageID, reactionType],
477+
type: 'delete-reaction',
478+
},
479+
});
480+
}
481+
} catch (error) {
482+
this._client.logger('error', `offlineDb:delete-reaction`, {
483+
tags: ['channel', 'offlineDb'],
484+
error,
471485
});
472486
}
473487

@@ -1449,10 +1463,14 @@ export class Channel {
14491463
isLatestMessageSet: messageSet.isLatest,
14501464
},
14511465
});
1452-
this.getClient().offlineDb?.upsertChannels?.({
1453-
channels: [state],
1454-
isLatestMessagesSet: messageSet.isLatest,
1455-
});
1466+
this.getClient().offlineDb?.executeQuerySafely(
1467+
(db) =>
1468+
db.upsertChannels?.({
1469+
channels: [state],
1470+
isLatestMessagesSet: messageSet.isLatest,
1471+
}),
1472+
{ method: 'upsertChannels' },
1473+
);
14561474

14571475
return state;
14581476
}
@@ -1719,7 +1737,10 @@ export class Channel {
17191737
channelState.unreadCount = 0;
17201738
}
17211739

1722-
this.getClient().offlineDb?.handleRead({ event, unreadMessages: 0 });
1740+
this.getClient().offlineDb?.executeQuerySafely(
1741+
(db) => db.handleRead({ event, unreadMessages: 0 }),
1742+
{ method: 'handleRead' },
1743+
);
17231744
}
17241745
break;
17251746
case 'user.watching.start':
@@ -1739,7 +1760,10 @@ export class Channel {
17391760
if (event.hard_delete) channelState.removeMessage(event.message);
17401761
else channelState.addMessageSorted(event.message, false, false);
17411762

1742-
this.getClient().offlineDb?.handleDeleteMessage({ event });
1763+
this.getClient().offlineDb?.executeQuerySafely(
1764+
(db) => db.handleDeleteMessage({ event }),
1765+
{ method: 'handleDeleteMessage' },
1766+
);
17431767

17441768
channelState.removeQuotedMessageReferences(event.message);
17451769

@@ -1788,11 +1812,10 @@ export class Channel {
17881812
}
17891813

17901814
if (!isThreadMessage) {
1791-
console.log('DID STUFF AND NOW HANDLING EVENT !');
1792-
this.getClient()
1793-
.offlineDb?.handleNewMessage({ event })
1794-
.then(() => console.log('SUCCESS IN NEW MESSAGE !'))
1795-
.catch((err) => console.log('ERROR: ', err));
1815+
this.getClient().offlineDb?.executeQuerySafely(
1816+
(db) => db.handleNewMessage({ event }),
1817+
{ method: 'handleNewMessage' },
1818+
);
17961819
}
17971820
}
17981821
break;
@@ -1807,7 +1830,10 @@ export class Channel {
18071830
} else {
18081831
channelState.removePinnedMessage(event.message);
18091832
}
1810-
this.getClient().offlineDb?.handleMessageUpdatedEvent({ event });
1833+
this.getClient().offlineDb?.executeQuerySafely(
1834+
(db) => db.handleMessageUpdatedEvent({ event }),
1835+
{ method: 'handleMessageUpdatedEvent' },
1836+
);
18111837
}
18121838
break;
18131839
case 'channel.truncated':
@@ -1841,7 +1867,10 @@ export class Channel {
18411867
}
18421868
}
18431869

1844-
this.getClient().offlineDb?.handleChannelTruncatedEvent({ event });
1870+
this.getClient().offlineDb?.executeQuerySafely(
1871+
(db) => db.handleChannelTruncatedEvent({ event }),
1872+
{ method: 'handleChannelTruncatedEvent' },
1873+
);
18451874

18461875
break;
18471876
case 'member.added':
@@ -1866,9 +1895,13 @@ export class Channel {
18661895
if (channel.data?.member_count && event.type === 'member.added') {
18671896
channel.data.member_count += 1;
18681897
}
1869-
this.getClient().offlineDb?.handleMemberEvent({
1870-
event: { ...event, member: memberCopy },
1871-
});
1898+
this.getClient().offlineDb?.executeQuerySafely(
1899+
(db) =>
1900+
db.handleMemberEvent({
1901+
event: { ...event, member: memberCopy },
1902+
}),
1903+
{ method: 'handleMemberEvent' },
1904+
);
18721905
}
18731906

18741907
const currentUserId = this.getClient().userID;
@@ -1895,9 +1928,13 @@ export class Channel {
18951928
channel.data.member_count = Math.max(channel.data.member_count - 1, 0);
18961929
}
18971930

1898-
this.getClient().offlineDb?.handleMemberEvent({
1899-
event,
1900-
});
1931+
this.getClient().offlineDb?.executeQuerySafely(
1932+
(db) =>
1933+
db.handleMemberEvent({
1934+
event,
1935+
}),
1936+
{ method: 'handleMemberEvent' },
1937+
);
19011938

19021939
// TODO?: unset membership
19031940
}
@@ -1917,7 +1954,9 @@ export class Channel {
19171954
};
19181955

19191956
channelState.unreadCount = unreadCount;
1920-
this.getClient().offlineDb?.handleRead({ event });
1957+
this.getClient().offlineDb?.executeQuerySafely((db) => db.handleRead({ event }), {
1958+
method: 'handleRead',
1959+
});
19211960
break;
19221961
}
19231962
case 'channel.updated':
@@ -1935,54 +1974,76 @@ export class Channel {
19351974
event.channel?.own_capabilities ?? channel.data?.own_capabilities,
19361975
};
19371976
channel.data = newChannelData;
1938-
this.getClient().offlineDb?.upsertChannelData({ channel: newChannelData });
1977+
this.getClient().offlineDb?.executeQuerySafely(
1978+
(db) => db.upsertChannelData({ channel: newChannelData }),
1979+
{ method: 'upsertChannelData' },
1980+
);
19391981
}
19401982
break;
19411983
case 'reaction.new':
19421984
if (event.message && event.reaction) {
1943-
event.message = channelState.addReaction(
1944-
event.reaction,
1945-
event.message,
1946-
) as MessageResponse;
1947-
this.getClient().offlineDb?.insertReaction({
1948-
message: event.message,
1949-
reaction: event.reaction,
1950-
});
1985+
const { message, reaction } = event;
1986+
event.message = channelState.addReaction(reaction, message) as MessageResponse;
1987+
this.getClient().offlineDb?.executeQuerySafely(
1988+
(db) =>
1989+
db.insertReaction({
1990+
message,
1991+
reaction,
1992+
}),
1993+
{ method: 'insertReaction' },
1994+
);
19511995
}
19521996
break;
19531997
case 'reaction.deleted':
19541998
if (event.message && event.reaction) {
1955-
event.message = channelState.removeReaction(event.reaction, event.message);
1956-
this.getClient().offlineDb?.deleteReaction({
1957-
message: event.message,
1958-
reaction: event.reaction,
1959-
});
1999+
const { message, reaction } = event;
2000+
event.message = channelState.removeReaction(reaction, message);
2001+
this.getClient().offlineDb?.executeQuerySafely(
2002+
(db) =>
2003+
db.deleteReaction({
2004+
message,
2005+
reaction,
2006+
}),
2007+
{ method: 'deleteReaction' },
2008+
);
19602009
}
19612010
break;
19622011
case 'reaction.updated':
1963-
if (event.reaction) {
2012+
if (event.message && event.reaction) {
2013+
const { message, reaction } = event;
19642014
// assuming reaction.updated is only called if enforce_unique is true
19652015
event.message = channelState.addReaction(
1966-
event.reaction,
1967-
event.message,
2016+
reaction,
2017+
message,
19682018
true,
19692019
) as MessageResponse;
1970-
this.getClient().offlineDb?.updateReaction({
1971-
message: event.message,
1972-
reaction: event.reaction,
1973-
});
2020+
this.getClient().offlineDb?.executeQuerySafely(
2021+
(db) =>
2022+
db.updateReaction({
2023+
message,
2024+
reaction,
2025+
}),
2026+
{ method: 'updateReaction' },
2027+
);
19742028
}
19752029
break;
19762030
case 'channel.hidden':
19772031
channel.data = { ...channel.data, hidden: true };
19782032
if (event.clear_history) {
19792033
channelState.clearMessages();
19802034
}
1981-
this.getClient().offlineDb?.handleChannelVisibilityEvent({ event });
2035+
this.getClient().offlineDb?.executeQuerySafely(
2036+
(db) => db.handleChannelVisibilityEvent({ event }),
2037+
{ method: 'handleChannelVisibilityEvent' },
2038+
);
19822039
break;
19832040
case 'channel.visible':
19842041
channel.data = { ...channel.data, hidden: false };
19852042
this.getClient().offlineDb?.handleChannelVisibilityEvent({ event });
2043+
this.getClient().offlineDb?.executeQuerySafely(
2044+
(db) => db.handleChannelVisibilityEvent({ event }),
2045+
{ method: 'handleChannelVisibilityEvent' },
2046+
);
19862047
break;
19872048
case 'user.banned':
19882049
if (!event.user?.id) break;

src/channel_manager.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,15 @@ export class ChannelManager {
216216
channels,
217217
pagination: { filters, sort },
218218
} = this.state.getLatestValue();
219-
this.client.offlineDb?.upsertCidsForQuery?.({
220-
cids: channels.map((channel) => channel.cid),
221-
filters,
222-
sort,
223-
});
219+
this.client.offlineDb?.executeQuerySafely(
220+
(db) =>
221+
db.upsertCidsForQuery({
222+
cids: channels.map((channel) => channel.cid),
223+
filters,
224+
sort,
225+
}),
226+
{ method: 'upsertCidsForQuery' },
227+
);
224228
};
225229

226230
public setEventHandlerOverrides = (
@@ -289,11 +293,15 @@ export class ChannelManager {
289293
},
290294
initialized: true,
291295
});
292-
await this.client.offlineDb?.upsertCidsForQuery?.({
293-
cids: channels.map((channel) => channel.cid),
294-
filters: pagination.filters,
295-
sort: pagination.sort,
296-
});
296+
this.client.offlineDb?.executeQuerySafely(
297+
(db) =>
298+
db.upsertCidsForQuery({
299+
cids: channels.map((channel) => channel.cid),
300+
filters: pagination.filters,
301+
sort: pagination.sort,
302+
}),
303+
{ method: 'upsertCidsForQuery' },
304+
);
297305
} catch (err) {
298306
// TODO: Maybe make this configurable ?
299307
await waitSeconds(2);

0 commit comments

Comments
 (0)