Skip to content

Commit

Permalink
Fixed error call
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian-Schopp committed Nov 2, 2023
1 parent c15ef37 commit ec40c03
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
7 changes: 1 addition & 6 deletions examples/todo/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,10 @@ import './base.css'
const todoList: Record<string, Todo> = {}
const peer = new Peer({
url: `ws://localhost:8081/`
// log: {
// logCallbacks: [console.log],
// logName: 'Peer 2',
// logLevel: LogLevel.socket
// }
})

const addTodo = (title: string) => {
peer.call('todo/add', [title])
peer.call('todo/add', [title]).catch(() => console.log('Failed to add'))
}

const removeTodo = (id: string) => {
Expand Down
23 changes: 7 additions & 16 deletions examples/todo/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Daemon, LogLevel, Method, Peer, State, ValueType } from '../../../lib'
import { Daemon, Method, Peer, State, ValueType } from '../../../lib'
import { Todo } from './Todo'

var port = parseInt(process.argv[2]) || 8081
Expand All @@ -12,11 +12,6 @@ var daemon = new Daemon({
batches: true,
asNotification: true
}
// log: {
// logCallbacks: [console.log],
// logName: 'Daemon',
// logLevel: LogLevel.socket
// }
})

daemon.listen({
Expand All @@ -28,16 +23,7 @@ console.log('listening on port', port)
// Create Jet Peer
var peer = new Peer({
url: `ws://localhost:8081/`
// log: {
// logCallbacks: [console.log],
// logName: 'Peer 1',
// logLevel: LogLevel.socket
// }
})
// const peer2 = new jet.Peer({
// port: internalPort,log:{logCallbacks:[console.log],logname:"Peer 2",loglevel:jet.LogLevel.debug}

// })

var todoStates: Record<string, State<Todo>> = {}

Expand All @@ -56,8 +42,13 @@ addTodo.on('call', (args) => {
todoState.on('set', (requestedTodo) => {
todo.merge(requestedTodo)
})
if (todo.id in todoStates) {
throw 'already existent'
}
todoStates[todo.id] = todoState
peer.add(todoState)
peer.add(todoState).catch(() => {
console.log('State already existent')
})
})

// Provide a "todo/remove" method to delete a certain todo
Expand Down
2 changes: 2 additions & 0 deletions src/3_jet/daemon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,13 +336,15 @@ export class Daemon extends EventEmitter {
this.forward('set', peer.user, params)
.then((res) => newPeer.respond(id, res, true))
.catch((err) => newPeer.respond(id, err, false))
.finally(() => newPeer.send())
)
newPeer.addListener(
'call',
(peer: JsonRPC, id: string, params: PathParams) =>
this.forward('call', peer.user, params)
.then((res) => newPeer.respond(id, res, true))
.catch((err) => newPeer.respond(id, err, false))
.finally(() => newPeer.send())
)
})
this.jsonRPCServer.addListener('disconnect', (peer: JsonRPC) => {
Expand Down
16 changes: 14 additions & 2 deletions src/3_jet/peer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,20 @@ export class Peer extends EventEmitter {
this.#jsonrpc.respond(id, error, false)
return
}
method.call(m.args)
this.#jsonrpc.respond(id, {}, true)
try {
method.call(m.args)
this.#jsonrpc.respond(id, {}, true)
} catch (err) {
this.#jsonrpc.respond(
id,
new InvalidParamError(
'InvalidParam',
'Failed to call method',
err && typeof err == 'object' ? err.toString() : undefined
),
false
)
}
} else {
const error = new NotFound(m.path)
this.#log.error(error.toString())
Expand Down

0 comments on commit ec40c03

Please sign in to comment.