refactor: implement transport_error

This commit is contained in:
CJ van den Berg 2026-03-10 15:46:02 +01:00
parent bf45b592ac
commit 9a7cc8faed
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 19 additions and 3 deletions

View file

@ -165,7 +165,8 @@ const Endpoint = struct {
if (self.proxies.get(e.id)) |p| if (self.proxies.get(e.id)) |p|
p.send(.{ "exit", e.reason }) catch {}; p.send(.{ "exit", e.reason }) catch {};
}, },
.link, .transport_error => return tp.exit_error(error.UnexpectedMessage, null), .transport_error => |te| return tp.exit(te.reason),
.link => return tp.exit_error(error.UnexpectedMessage, null),
} }
} }

View file

@ -218,6 +218,7 @@ const StdioEndpoint = struct {
try self.dispatch_stdin(); try self.dispatch_stdin();
try self.fd_stdin.wait_read(); try self.fd_stdin.wait_read();
} else if (try m.match(.{ "fd", "stdin", "read_error", tp.any, tp.any })) { } else if (try m.match(.{ "fd", "stdin", "read_error", tp.any, tp.any })) {
self.send_transport_error("stdin_closed");
return tp.exit("stdin_closed"); return tp.exit("stdin_closed");
} else if (try m.match(.{ "send", tp.extract(&from_id), tp.extract(&to_id_wire), cbor.extract_cbor(&payload) })) { } else if (try m.match(.{ "send", tp.extract(&from_id), tp.extract(&to_id_wire), cbor.extract_cbor(&payload) })) {
try self.send_wire_by_id(from_id, to_id_wire, payload); try self.send_wire_by_id(from_id, to_id_wire, payload);
@ -233,7 +234,10 @@ const StdioEndpoint = struct {
error.WouldBlock => return, error.WouldBlock => return,
else => return tp.exit_error(e, @errorReturnTrace()), else => return tp.exit_error(e, @errorReturnTrace()),
}; };
if (n == 0) return tp.exit("stdin_closed"); if (n == 0) {
self.send_transport_error("stdin_closed");
return tp.exit("stdin_closed");
}
if (self.accumulator.feed(self.read_buf[0..n])) |frame| { if (self.accumulator.feed(self.read_buf[0..n])) |frame| {
try self.dispatch_frame(frame); try self.dispatch_frame(frame);
} }
@ -254,7 +258,8 @@ const StdioEndpoint = struct {
else else
return tp.exit_error(error.UnknownWireId, null); return tp.exit_error(error.UnknownWireId, null);
}, },
.link, .exit, .transport_error => return tp.exit_error(error.UnexpectedMessage, null), .transport_error => |te| return tp.exit(te.reason),
.link, .exit => return tp.exit_error(error.UnexpectedMessage, null),
} }
} }
@ -279,6 +284,16 @@ const StdioEndpoint = struct {
try framing.write_frame(&frame_stream, msg_stream.buffered()); try framing.write_frame(&frame_stream, msg_stream.buffered());
try std.fs.File.stdout().writeAll(frame_stream.buffered()); try std.fs.File.stdout().writeAll(frame_stream.buffered());
} }
fn send_transport_error(_: *@This(), reason: []const u8) void {
var msg_buf: [framing.max_frame_size]u8 = undefined;
var msg_stream: std.Io.Writer = .fixed(&msg_buf);
protocol.encode_transport_error(&msg_stream, reason) catch return;
var frame_buf: [framing.max_frame_size + 4]u8 = undefined;
var frame_stream: std.Io.Writer = .fixed(&frame_buf);
framing.write_frame(&frame_stream, msg_stream.buffered()) catch return;
std.fs.File.stdout().writeAll(frame_stream.buffered()) catch {};
}
}; };
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------