Raise exceptions back up through .trigger()

This commit is contained in:
jesopo 2018-11-27 15:06:10 +00:00
parent d13a5069e3
commit 9d9deb28b5

View file

@ -2,6 +2,9 @@ import queue, os, select, socket, sys, threading, time, traceback, typing, uuid
from src import EventManager, Exports, IRCServer, Logging, ModuleManager from src import EventManager, Exports, IRCServer, Logging, ModuleManager
from src import Socket, utils from src import Socket, utils
TRIGGER_RETURN = 1
TRIGGER_EXCEPTION = 2
class Bot(object): class Bot(object):
def __init__(self, directory, args, cache, config, database, events, def __init__(self, directory, args, cache, config, database, events,
exports, log, modules, timers): exports, log, modules, timers):
@ -45,7 +48,11 @@ class Bot(object):
self.lock.release() self.lock.release()
self._trigger_client.send(b"TRIGGER") self._trigger_client.send(b"TRIGGER")
return func_queue.get(True) type, returned = func_queue.get(True)
if type == TRIGGER_EXCEPTION:
raise returned
elif type == TRIGGER_RETURN:
return returned
def add_server(self, server_id: int, connect: bool = True, def add_server(self, server_id: int, connect: bool = True,
connection_params: typing.Optional[ connection_params: typing.Optional[
@ -177,8 +184,13 @@ class Bot(object):
self.cache.expire() self.cache.expire()
for func, func_queue in self._trigger_functions: for func, func_queue in self._trigger_functions:
returned = func() try:
func_queue.put(returned) returned = func()
type = TRIGGER_RETURN
except Exception as e:
returned = e
type = TRIGGER_EXCEPTION
func_queue.put([type, returned])
self._trigger_functions.clear() self._trigger_functions.clear()
for fd, event in events: for fd, event in events: