Skip to content

Commit

Permalink
Merge pull request #79 from adzialocha/catch-exceptions-on-notify
Browse files Browse the repository at this point in the history
Catch exceptions in `notify` method and report as error
  • Loading branch information
adzialocha authored Apr 21, 2024
2 parents 8b4c5ff + 8589666 commit 3226fdf
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 48 deletions.
99 changes: 52 additions & 47 deletions src/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/osc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/osc.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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} */
Expand Down

0 comments on commit 3226fdf

Please sign in to comment.