improve logging, catch and log exceptions that happen while decoding messages in message handler
This commit is contained in:
parent
91236b728b
commit
9af1594e32
@ -10,22 +10,26 @@ from xdg import xdg_data_home
|
||||
|
||||
CONTENT_LENGTH = "Content-Length: "
|
||||
|
||||
def process_messages (source, sink, handlers, log):
|
||||
print("start message handler", file=log)
|
||||
def log (message, *args, **kwargs):
|
||||
print(message.format(*args, **kwargs), file=sys.stderr)
|
||||
|
||||
def process_messages (label, source, sink, handlers):
|
||||
log(f"[{label}] start process_messages")
|
||||
while True:
|
||||
line = source.readline()
|
||||
if not line:
|
||||
break
|
||||
|
||||
try:
|
||||
line = line.decode()
|
||||
if line.startswith(CONTENT_LENGTH):
|
||||
content_length = int(line[len(CONTENT_LENGTH):].strip())
|
||||
print(f">> ce: [{content_length}]", file=log)
|
||||
log(f">> [{label}] ce: [{content_length}]")
|
||||
|
||||
source.readline()
|
||||
|
||||
payload = source.read(content_length).decode()
|
||||
print(f">> payload: [{payload}]", file=log)
|
||||
log(">> [{label}] payload: [{payload}]", label=label, payload=payload)
|
||||
|
||||
if handlers:
|
||||
payload_parsed = json.loads(payload)
|
||||
@ -33,11 +37,13 @@ def process_messages (source, sink, handlers, log):
|
||||
try:
|
||||
handler(payload_parsed)
|
||||
except Exception as e:
|
||||
print(f"Error from handler: {e}", file=log)
|
||||
log(f"Error from handler: {e}")
|
||||
|
||||
transmit_payload(payload, sink)
|
||||
except Exception as e:
|
||||
log(f"[{label}] Error decoding input: {e}")
|
||||
|
||||
print("stop message handler", file=log)
|
||||
log(f"[{label}] exit process_messages")
|
||||
|
||||
def transmit_payload (payload, sink):
|
||||
if isinstance(payload, dict):
|
||||
@ -91,13 +97,16 @@ class LspServer:
|
||||
def start (self):
|
||||
handlers = self.handlers + handler.get_handlers(self)
|
||||
|
||||
log("starting lsp process: {}", " ".join(self.command))
|
||||
with Popen(self.command, stdin=PIPE, stdout=PIPE) as process:
|
||||
self.process = process
|
||||
|
||||
self.process_reader = Thread(target=process_messages, args=[process.stdout, sys.stdout.buffer, handlers, sys.stderr])
|
||||
self.process_reader = Thread(target=process_messages, args=["process.stdout reader", process.stdout, sys.stdout.buffer, handlers])
|
||||
self.process_reader.start()
|
||||
|
||||
process_messages(sys.stdin.buffer, process.stdin, handlers, sys.stderr)
|
||||
process_messages("process.stdin reader", sys.stdin.buffer, process.stdin, handlers)
|
||||
|
||||
log("lsp process ended")
|
||||
|
||||
def start2 (self):
|
||||
run(self.command)
|
||||
|
Loading…
x
Reference in New Issue
Block a user