Skip to content

Commit e163853

Browse files
committed
fix: add error handling in WebSocket message processing
1 parent ee71ff0 commit e163853

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/index.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,24 @@ void (async () => {
6565
})();
6666

6767
async function onMessage(message: WebSocket.Data): Promise<void> {
68-
if (isDev) console.debug('Message received from server.');
69-
const earthQuakeData = JSON.parse(message.toString() as string) as
70-
| JMAQuake
71-
| JMATsunami;
72-
73-
if (earthQuakeData.code === 551) {
74-
handleEarthquake(earthQuakeData as JMAQuake, isDev);
75-
} else {
76-
if (isDev) {
77-
console.warn(
78-
'Unknown message code: ',
79-
(earthQuakeData as JMAQuake | JMATsunami).code,
80-
);
68+
try {
69+
if (isDev) console.debug('Message received from server.');
70+
const earthQuakeData = JSON.parse(message.toString() as string) as
71+
| JMAQuake
72+
| JMATsunami;
73+
74+
if (earthQuakeData.code === 551) {
75+
await handleEarthquake(earthQuakeData as JMAQuake, isDev);
76+
} else {
77+
if (isDev) {
78+
console.warn(
79+
'Unknown message code: ',
80+
(earthQuakeData as JMAQuake | JMATsunami).code,
81+
);
82+
}
8183
}
84+
} catch (error) {
85+
console.error('Error processing message:', error);
8286
}
8387
}
8488

0 commit comments

Comments
 (0)