Add features to errorhandler
This commit is contained in:
+4
-4
@@ -40,8 +40,8 @@ export class RPCServer<
|
||||
this.sesame = U.makeSesameFunction(conf.sesame)
|
||||
}
|
||||
|
||||
this.errorHandler = (socket:I.Socket) => (error:any) => {
|
||||
if(conf.errorHandler) conf.errorHandler(socket, error)
|
||||
this.errorHandler = (socket:I.Socket) => (error:any, rpcName:string, args: any[]) => {
|
||||
if(conf.errorHandler) conf.errorHandler(socket, error, rpcName, args)
|
||||
else throw error
|
||||
}
|
||||
|
||||
@@ -69,14 +69,14 @@ export class RPCServer<
|
||||
try{
|
||||
this.io.attach(this.ws)
|
||||
this.io.on('socket', (socket:I.Socket) => {
|
||||
socket.on('error', (err) => this.errorHandler(socket, err))
|
||||
socket.on('error', (err) => this.errorHandler(socket, err, "bind", []))
|
||||
socket.on('close', () => this.closeHandler(socket))
|
||||
this.connectionHandler(socket)
|
||||
this.initRPCs(socket)
|
||||
})
|
||||
this.ws.listen(this.port, this.visibility)
|
||||
}catch(e){
|
||||
this.errorHandler(this.io, e)
|
||||
this.errorHandler(this.io, e, 'start', [])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ export type HookFunction<F extends AnyFunction = AnyFunction, SubresT = {}> = (.
|
||||
|
||||
export type Visibility = "127.0.0.1" | "0.0.0.0"
|
||||
export type ConnectionHandler = (socket:I.Socket) => void
|
||||
export type ErrorHandler = (socket:I.Socket, error:any) => void
|
||||
export type ErrorHandler = (socket:I.Socket, error:any, rpcName: string, args: any[]) => void
|
||||
export type CloseHandler = (socket:I.Socket) => void
|
||||
export type SesameFunction = (sesame : string) => boolean
|
||||
export type ExceptionHandling = 'local' | 'remote'
|
||||
|
||||
+5
-5
@@ -74,7 +74,7 @@ export function rpcHooker<SubResT = {}>(socket: I.Socket, exporter:I.RPCExporter
|
||||
else
|
||||
rpcFunction = info.call
|
||||
|
||||
socket.hook(ret.uniqueName, callGenerator(socket, rpcFunction, errorHandler))
|
||||
socket.hook(ret.uniqueName, callGenerator(info.name, socket, rpcFunction, errorHandler))
|
||||
return ret
|
||||
})
|
||||
}
|
||||
@@ -83,7 +83,7 @@ export function rpcHooker<SubResT = {}>(socket: I.Socket, exporter:I.RPCExporter
|
||||
* Decorate an RPC with the error handler
|
||||
* @param rpcFunction the function to decorate
|
||||
*/
|
||||
const callGenerator = (socket: I.Socket, rpcFunction : T.AnyFunction, errorHandler: T.ErrorHandler) : T.AnyFunction => {
|
||||
const callGenerator = (rpcName : string, socket: I.Socket, rpcFunction : T.AnyFunction, errorHandler: T.ErrorHandler) : T.AnyFunction => {
|
||||
const argsArr = extractArgs(rpcFunction)
|
||||
const args = argsArr.join(',')
|
||||
const argsStr = argsArr.map(stripAfterEquals).join(',')
|
||||
@@ -92,7 +92,7 @@ const callGenerator = (socket: I.Socket, rpcFunction : T.AnyFunction, errorHandl
|
||||
try{
|
||||
return await rpcFunction(`+argsStr+`)
|
||||
}catch(e){
|
||||
errorHandler(socket)(e)
|
||||
errorHandler(socket)(e, rpcName, [`+args+`])
|
||||
}
|
||||
}`)
|
||||
}
|
||||
@@ -126,7 +126,7 @@ const hookGenerator = (rpc:T.HookRPC<any, any, any>, errorHandler: T.ErrorHandle
|
||||
})
|
||||
return res
|
||||
}catch(e){
|
||||
errorHandler(socket, e)
|
||||
errorHandler(socket)(e, rpc.name, [`+args+`])
|
||||
}
|
||||
}`)
|
||||
}
|
||||
@@ -139,7 +139,7 @@ const hookGenerator = (rpc:T.HookRPC<any, any, any>, errorHandler: T.ErrorHandle
|
||||
})
|
||||
return res
|
||||
}catch(e){
|
||||
errorHandler(socket, e)
|
||||
errorHandler(socket)(e, rpc.name, [`+args+`])
|
||||
}
|
||||
}`)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user