Improve errors in parse API

This commit is contained in:
2026-01-08 23:46:39 -05:00
parent ed99115969
commit f4b545f852

View File

@@ -19,6 +19,12 @@ const message = @import("./message.zig");
const Message = message.Message;
const Payload = @import("./Payload.zig");
pub const Error = error{
EndOfStream,
ReadFailed,
InvalidStream,
};
const client_control = StaticStringMap(message.Control).initComptime(
.{
// {"INFO", .info},
@@ -36,7 +42,7 @@ const client_control = StaticStringMap(message.Control).initComptime(
},
);
pub fn control(in: *Reader) !message.Control {
pub fn control(in: *Reader) Error!message.Control {
const longest_ctrl = comptime blk: {
var min_len = 0;
for (client_control.keys()) |ctrl| {
@@ -54,7 +60,7 @@ pub fn control(in: *Reader) !message.Control {
in.toss(str.len);
return ctrl;
} else if (str.len >= longest_ctrl) {
return error.InvalidControl;
return error.InvalidStream;
}
}
log.debug("filling more in control.", .{});
@@ -109,13 +115,13 @@ test control {
var in: std.testing.Reader = .init(&buf, &.{
.{ .buffer = "CONNECC" },
});
try std.testing.expectError(error.InvalidControl, control(&in.interface));
try std.testing.expectError(error.InvalidStream, control(&in.interface));
}
}
/// The return value is owned by the reader passed to this function.
/// Operations that modify the readers buffer invalidates this value.
pub fn @"pub"(in: *Reader) !Message.Pub {
pub fn @"pub"(in: *Reader) Error!Message.Pub {
// TODO: Add pedantic option.
// See: https://docs.nats.io/reference/reference-protocols/nats-protocol#syntax-1
@@ -127,7 +133,7 @@ pub fn @"pub"(in: *Reader) !Message.Pub {
if (in.buffered().len > iter.index) {
if (in.buffered()[iter.index] == '\r') {
const bytes_str = second;
const bytes = try parseUnsigned(usize, bytes_str, 10);
const bytes = parseUnsigned(usize, bytes_str, 10) catch return error.InvalidStream;
if (in.buffered().len < iter.index + bytes + 4) {
try in.fill(iter.index + bytes + 4);
@@ -147,7 +153,7 @@ pub fn @"pub"(in: *Reader) !Message.Pub {
const reply_to = second;
if (iter.next()) |bytes_str| {
const bytes = try parseUnsigned(usize, bytes_str, 10);
const bytes = parseUnsigned(usize, bytes_str, 10) catch return error.InvalidStream;
if (in.buffered().len > iter.index and in.buffered()[iter.index] == '\r') {
if (in.buffered().len < iter.index + bytes + 4) {
@@ -260,7 +266,7 @@ test @"pub" {
/// The return value is owned by the reader passed to this function.
/// Operations that modify the readers buffer invalidates this value.
pub fn sub(in: *Reader) !Message.Sub {
pub fn sub(in: *Reader) Error!Message.Sub {
// TODO: Add pedantic option.
// See: https://docs.nats.io/reference/reference-protocols/nats-protocol#syntax-1
@@ -378,7 +384,7 @@ test sub {
/// The return value is owned by the reader passed to this function.
/// Operations that modify the readers buffer invalidates this value.
pub fn unsub(in: *Reader) !Message.Unsub {
pub fn unsub(in: *Reader) Error!Message.Unsub {
// TODO: Add pedantic option.
// See: https://docs.nats.io/reference/reference-protocols/nats-protocol#syntax-1
@@ -403,7 +409,7 @@ pub fn unsub(in: *Reader) !Message.Unsub {
}
if (iter.next()) |max_msgs_str| {
if (in.buffered().len > iter.index and in.buffered()[iter.index] == '\r') {
const max_msgs = try parseUnsigned(usize, max_msgs_str, 10);
const max_msgs = parseUnsigned(usize, max_msgs_str, 10) catch return error.InvalidStream;
if (in.buffered().len < iter.index + 2) {
try in.fill(iter.index + 2);
@@ -429,7 +435,7 @@ pub fn unsub(in: *Reader) !Message.Unsub {
const sid = iter.next() orelse return error.EndOfStream;
const max_msgs = if (iter.next()) |max_msgs_str| blk: {
log.debug("max_msgs: {any}", .{max_msgs_str});
break :blk try parseUnsigned(usize, max_msgs_str, 10);
break :blk parseUnsigned(usize, max_msgs_str, 10) catch return error.InvalidStream;
} else null;
return .{
.sid = sid,
@@ -515,7 +521,7 @@ test unsub {
/// The return value is owned by the reader passed to this function.
/// Operations that modify the readers buffer invalidates this value.
pub fn hpub(in: *Reader) !Message.HPub {
pub fn hpub(in: *Reader) Error!Message.HPub {
// TODO: Add pedantic option.
// See: https://docs.nats.io/reference/reference-protocols/nats-protocol#syntax-1
while (true) {
@@ -529,8 +535,8 @@ pub fn hpub(in: *Reader) !Message.HPub {
const header_bytes_str = second;
const total_bytes_str = third;
const header_bytes = try parseUnsigned(usize, header_bytes_str, 10);
const total_bytes = try parseUnsigned(usize, total_bytes_str, 10);
const header_bytes = parseUnsigned(usize, header_bytes_str, 10) catch return error.InvalidStream;
const total_bytes = parseUnsigned(usize, total_bytes_str, 10) catch return error.InvalidStream;
if (in.buffered().len < iter.index + total_bytes + 4) {
try in.fill(iter.index + total_bytes + 4);
@@ -555,8 +561,8 @@ pub fn hpub(in: *Reader) !Message.HPub {
const header_bytes_str = third;
if (iter.next()) |total_bytes_str| {
if (in.buffered().len > iter.index and in.buffered()[iter.index] == '\r') {
const header_bytes = try parseUnsigned(usize, header_bytes_str, 10);
const total_bytes = try parseUnsigned(usize, total_bytes_str, 10);
const header_bytes = parseUnsigned(usize, header_bytes_str, 10) catch return error.InvalidStream;
const total_bytes = parseUnsigned(usize, total_bytes_str, 10) catch return error.InvalidStream;
if (in.buffered().len < iter.index + total_bytes + 4) {
try in.fill(iter.index + total_bytes + 4);
@@ -624,12 +630,7 @@ test hpub {
// TODO: more tests
}
pub fn connect(alloc: Allocator, in: *Reader) error{
EndOfStream,
ReadFailed,
OutOfMemory,
InvalidStream,
}!Message.Connect {
pub fn connect(alloc: Allocator, in: *Reader) (error{OutOfMemory} || Error)!Message.Connect {
// for storing the json string
var connect_string_writer_allocating: AllocatingWriter = .init(alloc);
defer connect_string_writer_allocating.deinit();