refactor: improve error codes return from LSP client
This commit is contained in:
parent
ba65fece7e
commit
d83d3a62bb
3 changed files with 11 additions and 5 deletions
12
src/LSP.zig
12
src/LSP.zig
|
@ -17,7 +17,7 @@ const OutOfMemoryError = error{OutOfMemory};
|
||||||
const SendError = error{SendFailed};
|
const SendError = error{SendFailed};
|
||||||
const CallError = tp.CallError;
|
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) };
|
return .{ .allocator = allocator, .pid = try Process.create(allocator, project, cmd) };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,14 +70,18 @@ const Process = struct {
|
||||||
|
|
||||||
const Receiver = tp.Receiver(*Process);
|
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;
|
var tag: []const u8 = undefined;
|
||||||
if (try cbor.match(cmd.buf, .{tp.extract(&tag)})) {
|
if (try cbor.match(cmd.buf, .{tp.extract(&tag)})) {
|
||||||
//
|
//
|
||||||
} else if (try cbor.match(cmd.buf, .{ tp.extract(&tag), tp.more })) {
|
} else if (try cbor.match(cmd.buf, .{ tp.extract(&tag), tp.more })) {
|
||||||
//
|
//
|
||||||
} else {
|
} 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);
|
const self = try allocator.create(Process);
|
||||||
var sp_tag_ = std.ArrayList(u8).init(allocator);
|
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" })) {
|
} else if (try cbor.match(m.buf, .{ "exit", "error.FileNotFound" })) {
|
||||||
self.write_log("### LSP not found ###\n", .{});
|
self.write_log("### LSP not found ###\n", .{});
|
||||||
const logger = log.logger("LSP");
|
const logger = log.logger("LSP");
|
||||||
|
defer logger.deinit();
|
||||||
var buf: [1024]u8 = undefined;
|
var buf: [1024]u8 = undefined;
|
||||||
logger.print_err("init", "executable not found: {s}", .{self.cmd.to_json(&buf) catch "{command too large}"});
|
logger.print_err("init", "executable not found: {s}", .{self.cmd.to_json(&buf) catch "{command too large}"});
|
||||||
return error.FileNotFound;
|
return error.FileNotFound;
|
||||||
|
@ -270,6 +275,7 @@ const Process = struct {
|
||||||
|
|
||||||
fn handle_terminated(self: *Process, err: []const u8, code: u32) error{ExitNormal}!void {
|
fn handle_terminated(self: *Process, err: []const u8, code: u32) error{ExitNormal}!void {
|
||||||
const logger = log.logger("LSP");
|
const logger = log.logger("LSP");
|
||||||
|
defer logger.deinit();
|
||||||
logger.print("terminated: {s} {d}", .{ err, code });
|
logger.print("terminated: {s} {d}", .{ err, code });
|
||||||
self.write_log("### subprocess terminated {s} {d} ###\n", .{ err, code });
|
self.write_log("### subprocess terminated {s} {d} ###\n", .{ err, code });
|
||||||
self.parent.send(.{ sp_tag, self.tag, "done" }) catch {};
|
self.parent.send(.{ sp_tag, self.tag, "done" }) catch {};
|
||||||
|
|
|
@ -26,7 +26,7 @@ const OutOfMemoryError = error{OutOfMemory};
|
||||||
const CallError = tp.CallError;
|
const CallError = tp.CallError;
|
||||||
const SpawnError = (OutOfMemoryError || error{ThespianSpawnFailed});
|
const SpawnError = (OutOfMemoryError || error{ThespianSpawnFailed});
|
||||||
pub const InvalidMessageError = error{ InvalidMessage, InvalidMessageField, InvalidTargetURI };
|
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 LspError = (error{ NoLsp, LspFailed } || OutOfMemoryError);
|
||||||
pub const ClientError = (error{ClientFailed} || OutOfMemoryError);
|
pub const ClientError = (error{ClientFailed} || OutOfMemoryError);
|
||||||
pub const LspOrClientError = (LspError || ClientError);
|
pub const LspOrClientError = (LspError || ClientError);
|
||||||
|
|
|
@ -419,7 +419,7 @@ const Process = struct {
|
||||||
try request_path_files_async(self.allocator, from, project, max, path);
|
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" });
|
const frame = tracy.initZone(@src(), .{ .name = module_name ++ ".did_open" });
|
||||||
defer frame.deinit();
|
defer frame.deinit();
|
||||||
const project = self.projects.get(project_directory) orelse return error.NoProject;
|
const project = self.projects.get(project_directory) orelse return error.NoProject;
|
||||||
|
|
Loading…
Add table
Reference in a new issue