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