Skip to content

Commit 8589666

Browse files
committed
Catch exceptions in notify method and report as error
1 parent fbdfd11 commit 8589666

3 files changed

Lines changed: 65 additions & 48 deletions

File tree

‎src/events.js‎

Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -203,68 +203,73 @@ export default class EventHandler {
203203
throw new Error('OSC EventHandler can not be called without any argument')
204204
}
205205

206-
// check for incoming dispatchable OSC data
207-
if (args[0] instanceof Packet) {
208-
return this.dispatch(args[0], args[1])
209-
} else if (args[0] instanceof Bundle || args[0] instanceof Message) {
210-
return this.dispatch(new Packet(args[0]), args[1])
211-
} else if (!isString(args[0])) {
212-
const packet = new Packet()
213-
packet.unpack(dataView(args[0]))
214-
return this.dispatch(packet, args[1])
215-
}
206+
try {
207+
// check for incoming dispatchable OSC data
208+
if (args[0] instanceof Packet) {
209+
return this.dispatch(args[0], args[1])
210+
} else if (args[0] instanceof Bundle || args[0] instanceof Message) {
211+
return this.dispatch(new Packet(args[0]), args[1])
212+
} else if (!isString(args[0])) {
213+
const packet = new Packet()
214+
packet.unpack(dataView(args[0]))
215+
return this.dispatch(packet, args[1])
216+
}
216217

217-
const name = args[0]
218+
const name = args[0]
218219

219-
// data argument
220-
let data = null
220+
// data argument
221+
let data = null
221222

222-
if (args.length > 1) {
223-
data = args[1]
224-
}
223+
if (args.length > 1) {
224+
data = args[1]
225+
}
225226

226-
// timestamp argument
227-
let timestamp = null
227+
// timestamp argument
228+
let timestamp = null
228229

229-
if (args.length > 2) {
230-
if (isInt(args[2])) {
231-
timestamp = args[2]
232-
} else if (args[2] instanceof Date) {
233-
timestamp = args[2].getTime()
234-
} else {
235-
throw new Error('OSC EventHandler timestamp has to be a number or Date')
230+
if (args.length > 2) {
231+
if (isInt(args[2])) {
232+
timestamp = args[2]
233+
} else if (args[2] instanceof Date) {
234+
timestamp = args[2].getTime()
235+
} else {
236+
throw new Error('OSC EventHandler timestamp has to be a number or Date')
237+
}
236238
}
237-
}
238239

239-
// remote address info
240-
let rinfo = null
240+
// remote address info
241+
let rinfo = null
241242

242-
if (args.length >= 3) {
243-
rinfo = args[3]
244-
}
243+
if (args.length >= 3) {
244+
rinfo = args[3]
245+
}
245246

246-
// notify now or later
247-
if (timestamp) {
248-
const now = Date.now()
247+
// notify now or later
248+
if (timestamp) {
249+
const now = Date.now()
249250

250-
// is message outdated?
251-
if (now > timestamp) {
252-
if (!this.options.discardLateMessages) {
253-
return this.call(name, data, rinfo)
251+
// is message outdated?
252+
if (now > timestamp) {
253+
if (!this.options.discardLateMessages) {
254+
return this.call(name, data, rinfo)
255+
}
254256
}
255-
}
256257

257-
// notify later
258-
const that = this
258+
// notify later
259+
const that = this
259260

260-
setTimeout(() => {
261-
that.call(name, data, rinfo)
262-
}, timestamp - now)
261+
setTimeout(() => {
262+
that.call(name, data, rinfo)
263+
}, timestamp - now)
263264

264-
return true
265-
}
265+
return true
266+
}
266267

267-
return this.call(name, data, rinfo)
268+
return this.call(name, data, rinfo)
269+
} catch (error) {
270+
this.notify('error', error)
271+
return false
272+
}
268273
}
269274

270275
/**

‎src/osc.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class OSC {
8989
this.options = { ...defaultOptions, ...options }
9090
// create default plugin with default options
9191
if (!this.options.plugin) {
92-
this.options.plugin = new WebsocketClientPlugin();
92+
this.options.plugin = new WebsocketClientPlugin()
9393
}
9494
/**
9595
* @type {EventHandler} eventHandler

‎test/osc.spec.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ describe('OSC', () => {
8282

8383
expect(spy).to.have.been.called()
8484
})
85+
86+
it('calls an error due to an internal exception', () => {
87+
const spy = chai.spy()
88+
89+
osc.on('error', spy)
90+
91+
// Receive broken OSC packet
92+
const bytes = new Uint8Array([1, 2, 3])
93+
osc.eventHandler.notify(bytes)
94+
95+
expect(spy).to.have.been.called()
96+
})
8597
})
8698

8799
/** @test {OSC#off} */

0 commit comments

Comments
 (0)