Skip to content

Commit bd6af00

Browse files
committed
Improve error handling
1 parent a8b5a49 commit bd6af00

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

application/static/metacom.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -290,14 +290,8 @@ class Metacom extends Emitter {
290290
}
291291

292292
async message(data) {
293-
if (data === '{}') return;
294293
this.lastActivity = Date.now();
295-
let packet;
296-
try {
297-
packet = JSON.parse(data);
298-
} catch {
299-
return;
300-
}
294+
const packet = JSON.parse(data);
301295
const { type, id, name } = packet;
302296
if (type === 'event') {
303297
const [unit, eventName] = name.split('/');
@@ -308,7 +302,7 @@ class Metacom extends Emitter {
308302
if (!id) throw new Error('Packet structure error');
309303
if (type === 'callback') {
310304
const promised = this.calls.get(id);
311-
if (!promised) return;
305+
if (!promised) throw new Error(`Callback ${id} not found`);
312306
const [resolve, reject, timeout] = promised;
313307
this.calls.delete(id);
314308
clearTimeout(timeout);
@@ -321,21 +315,21 @@ class Metacom extends Emitter {
321315
const stream = this.streams.get(id);
322316
if (name && typeof name === 'string' && Number.isSafeInteger(size)) {
323317
if (stream) {
324-
console.error(new Error(`Stream ${name} is already initialized`));
318+
throw new Error(`Stream ${name} is already initialized`);
325319
} else {
326320
const stream = new MetaReadable(id, name, size);
327321
this.streams.set(id, stream);
328322
}
329323
} else if (!stream) {
330-
console.error(new Error(`Stream ${id} is not initialized`));
324+
throw new Error(`Stream ${id} is not initialized`);
331325
} else if (status === 'end') {
332326
await stream.close();
333327
this.streams.delete(id);
334328
} else if (status === 'terminate') {
335329
await stream.terminate();
336330
this.streams.delete(id);
337331
} else {
338-
console.error(new Error('Stream packet structure error'));
332+
throw new Error('Stream packet structure error');
339333
}
340334
}
341335
}
@@ -345,7 +339,7 @@ class Metacom extends Emitter {
345339
const { id, payload } = chunkDecode(byteView);
346340
const stream = this.streams.get(id);
347341
if (stream) await stream.push(payload);
348-
else console.warn(`Stream ${id} is not initialized`);
342+
else throw new Error(`Stream ${id} is not initialized`);
349343
}
350344

351345
async load(...units) {
@@ -590,14 +584,16 @@ class MetacomProxy extends Emitter {
590584
const { type } = event.data;
591585
if (type === 'metacom:connect') {
592586
const port = event.ports[0];
593-
if (!port) return;
587+
if (!port) throw new Error('MessagePort not provided');
594588
const portId = this.generateId();
595589
this.ports.set(portId, port);
596590
port.addEventListener('message', async (messageEvent) => {
597591
const { data } = messageEvent;
598-
if (data === undefined) return;
592+
if (data === undefined) throw new Error('Message data is undefined');
599593
await this.open();
600-
if (!this.connection || !this.connection.connected) return;
594+
if (!this.connection || !this.connection.connected) {
595+
throw new Error('Not connected to server');
596+
}
601597
this.connection.write(data);
602598
});
603599
port.start();

0 commit comments

Comments
 (0)