From 8589666efd77417bb32c7690e6529aa8471fc339 Mon Sep 17 00:00:00 2001 From: adz Date: Sun, 21 Apr 2024 20:10:07 +0200 Subject: [PATCH] Catch exceptions in notify method and report as error --- src/events.js | 99 +++++++++++++++++++++++++----------------------- src/osc.js | 2 +- test/osc.spec.js | 12 ++++++ 3 files changed, 65 insertions(+), 48 deletions(-) diff --git a/src/events.js b/src/events.js index f73154d..c903cd2 100644 --- a/src/events.js +++ b/src/events.js @@ -203,68 +203,73 @@ export default class EventHandler { throw new Error('OSC EventHandler can not be called without any argument') } - // check for incoming dispatchable OSC data - if (args[0] instanceof Packet) { - return this.dispatch(args[0], args[1]) - } else if (args[0] instanceof Bundle || args[0] instanceof Message) { - return this.dispatch(new Packet(args[0]), args[1]) - } else if (!isString(args[0])) { - const packet = new Packet() - packet.unpack(dataView(args[0])) - return this.dispatch(packet, args[1]) - } + try { + // check for incoming dispatchable OSC data + if (args[0] instanceof Packet) { + return this.dispatch(args[0], args[1]) + } else if (args[0] instanceof Bundle || args[0] instanceof Message) { + return this.dispatch(new Packet(args[0]), args[1]) + } else if (!isString(args[0])) { + const packet = new Packet() + packet.unpack(dataView(args[0])) + return this.dispatch(packet, args[1]) + } - const name = args[0] + const name = args[0] - // data argument - let data = null + // data argument + let data = null - if (args.length > 1) { - data = args[1] - } + if (args.length > 1) { + data = args[1] + } - // timestamp argument - let timestamp = null + // timestamp argument + let timestamp = null - if (args.length > 2) { - if (isInt(args[2])) { - timestamp = args[2] - } else if (args[2] instanceof Date) { - timestamp = args[2].getTime() - } else { - throw new Error('OSC EventHandler timestamp has to be a number or Date') + if (args.length > 2) { + if (isInt(args[2])) { + timestamp = args[2] + } else if (args[2] instanceof Date) { + timestamp = args[2].getTime() + } else { + throw new Error('OSC EventHandler timestamp has to be a number or Date') + } } - } - // remote address info - let rinfo = null + // remote address info + let rinfo = null - if (args.length >= 3) { - rinfo = args[3] - } + if (args.length >= 3) { + rinfo = args[3] + } - // notify now or later - if (timestamp) { - const now = Date.now() + // notify now or later + if (timestamp) { + const now = Date.now() - // is message outdated? - if (now > timestamp) { - if (!this.options.discardLateMessages) { - return this.call(name, data, rinfo) + // is message outdated? + if (now > timestamp) { + if (!this.options.discardLateMessages) { + return this.call(name, data, rinfo) + } } - } - // notify later - const that = this + // notify later + const that = this - setTimeout(() => { - that.call(name, data, rinfo) - }, timestamp - now) + setTimeout(() => { + that.call(name, data, rinfo) + }, timestamp - now) - return true - } + return true + } - return this.call(name, data, rinfo) + return this.call(name, data, rinfo) + } catch (error) { + this.notify('error', error) + return false + } } /** diff --git a/src/osc.js b/src/osc.js index f824b00..89e421a 100644 --- a/src/osc.js +++ b/src/osc.js @@ -89,7 +89,7 @@ class OSC { this.options = { ...defaultOptions, ...options } // create default plugin with default options if (!this.options.plugin) { - this.options.plugin = new WebsocketClientPlugin(); + this.options.plugin = new WebsocketClientPlugin() } /** * @type {EventHandler} eventHandler diff --git a/test/osc.spec.js b/test/osc.spec.js index ff4b7e8..80c8555 100644 --- a/test/osc.spec.js +++ b/test/osc.spec.js @@ -82,6 +82,18 @@ describe('OSC', () => { expect(spy).to.have.been.called() }) + + it('calls an error due to an internal exception', () => { + const spy = chai.spy() + + osc.on('error', spy) + + // Receive broken OSC packet + const bytes = new Uint8Array([1, 2, 3]) + osc.eventHandler.notify(bytes) + + expect(spy).to.have.been.called() + }) }) /** @test {OSC#off} */