refactor: improve error codes return from LSP client

This commit is contained in:
CJ van den Berg 2024-12-12 16:55:23 +01:00
parent ba65fece7e
commit d83d3a62bb
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
3 changed files with 11 additions and 5 deletions

View file

@ -17,7 +17,7 @@ const OutOfMemoryError = error{OutOfMemory};
const SendError = error{SendFailed};
const CallError = tp.CallError;
pub fn open(allocator: std.mem.Allocator, project: []const u8, cmd: tp.message) (error{ ThespianSpawnFailed, InvalidArgument } || cbor.Error)!Self {
pub fn open(allocator: std.mem.Allocator, project: []const u8, cmd: tp.message) (error{ ThespianSpawnFailed, InvalidLspCommand } || cbor.Error)!Self {
return .{ .allocator = allocator, .pid = try Process.create(allocator, project, cmd) };
}
@ -70,14 +70,18 @@ const Process = struct {
const Receiver = tp.Receiver(*Process);
pub fn create(allocator: std.mem.Allocator, project: []const u8, cmd: tp.message) (error{ ThespianSpawnFailed, InvalidArgument } || OutOfMemoryError || cbor.Error)!tp.pid {
pub fn create(allocator: std.mem.Allocator, project: []const u8, cmd: tp.message) (error{ ThespianSpawnFailed, InvalidLspCommand } || OutOfMemoryError || cbor.Error)!tp.pid {
var tag: []const u8 = undefined;
if (try cbor.match(cmd.buf, .{tp.extract(&tag)})) {
//
} else if (try cbor.match(cmd.buf, .{ tp.extract(&tag), tp.more })) {
//
} else {
return error.InvalidArgument;
const logger = log.logger("LSP");
defer logger.deinit();
var buf: [1024]u8 = undefined;
logger.print_err("create", "invalid command: {d} {s}", .{ cmd.buf.len, cmd.to_json(&buf) catch "{command too large}" });
return error.InvalidLspCommand;
}
const self = try allocator.create(Process);
var sp_tag_ = std.ArrayList(u8).init(allocator);
@ -197,6 +201,7 @@ const Process = struct {
} else if (try cbor.match(m.buf, .{ "exit", "error.FileNotFound" })) {
self.write_log("### LSP not found ###\n", .{});
const logger = log.logger("LSP");
defer logger.deinit();
var buf: [1024]u8 = undefined;
logger.print_err("init", "executable not found: {s}", .{self.cmd.to_json(&buf) catch "{command too large}"});
return error.FileNotFound;
@ -270,6 +275,7 @@ const Process = struct {
fn handle_terminated(self: *Process, err: []const u8, code: u32) error{ExitNormal}!void {
const logger = log.logger("LSP");
defer logger.deinit();
logger.print("terminated: {s} {d}", .{ err, code });
self.write_log("### subprocess terminated {s} {d} ###\n", .{ err, code });
self.parent.send(.{ sp_tag, self.tag, "done" }) catch {};

View file

@ -26,7 +26,7 @@ const OutOfMemoryError = error{OutOfMemory};
const CallError = tp.CallError;
const SpawnError = (OutOfMemoryError || error{ThespianSpawnFailed});
pub const InvalidMessageError = error{ InvalidMessage, InvalidMessageField, InvalidTargetURI };
pub const StartLspError = (error{ ThespianSpawnFailed, Timeout, InvalidArgument } || LspError || OutOfMemoryError || cbor.Error);
pub const StartLspError = (error{ ThespianSpawnFailed, Timeout, InvalidLspCommand } || LspError || OutOfMemoryError || cbor.Error);
pub const LspError = (error{ NoLsp, LspFailed } || OutOfMemoryError);
pub const ClientError = (error{ClientFailed} || OutOfMemoryError);
pub const LspOrClientError = (LspError || ClientError);

View file

@ -419,7 +419,7 @@ const Process = struct {
try request_path_files_async(self.allocator, from, project, max, path);
}
fn did_open(self: *Process, project_directory: []const u8, file_path: []const u8, file_type: []const u8, language_server: []const u8, version: usize, text: []const u8) (ProjectError || InvalidArgumentError || Project.StartLspError || CallError || cbor.Error)!void {
fn did_open(self: *Process, project_directory: []const u8, file_path: []const u8, file_type: []const u8, language_server: []const u8, version: usize, text: []const u8) (ProjectError || Project.StartLspError || CallError || cbor.Error)!void {
const frame = tracy.initZone(@src(), .{ .name = module_name ++ ".did_open" });
defer frame.deinit();
const project = self.projects.get(project_directory) orelse return error.NoProject;