Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch exceptions in notify method and report as error #79

Merged
merged 1 commit into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading