Skip to content

Commit 895f5d1

Browse files
Merge pull request #9 from carpentry-org/claude/read-error-reporting
Fix read/read-bytes to report errors via status out-parameter
2 parents 96ef95a + dd758d0 commit 895f5d1

4 files changed

Lines changed: 56 additions & 24 deletions

File tree

src/tcp_stream.carp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ and accepted server connections.
4242
"TcpStream_send_MINUS_len_")
4343
(private read-)
4444
(hidden read-)
45-
(register read- (Fn [&TcpStream] String) "TcpStream_read_")
45+
(register read- (Fn [&TcpStream &Int] String) "TcpStream_read_")
4646
(private read-bytes-)
4747
(hidden read-bytes-)
4848
(register read-bytes-
49-
(Fn [&TcpStream] (Array Byte))
49+
(Fn [&TcpStream &Int] (Array Byte))
5050
"TcpStream_read_MINUS_bytes_")
5151
(private read-append-)
5252
(hidden read-append-)
@@ -82,10 +82,18 @@ and accepted server connections.
8282
(doc read
8383
"reads up to 4096 bytes from the stream. Returns the data as a string, or an error.
8484
Returns an empty string on connection close.")
85-
(defn read [stream] (let [s (read- stream)] (Result.Success s)))
86-
87-
(doc read-bytes "reads up to 4096 bytes as a byte array.")
88-
(defn read-bytes [stream] (Result.Success (read-bytes- stream)))
85+
(defn read [stream]
86+
(let [status 0
87+
data (read- stream &status)]
88+
(if (< status 0) (Result.Error (System.error-text)) (Result.Success data))))
89+
90+
(doc read-bytes
91+
"reads up to 4096 bytes as a byte array. Returns an empty array on
92+
connection close, or an error if the read failed.")
93+
(defn read-bytes [stream]
94+
(let [status 0
95+
data (read-bytes- stream &status)]
96+
(if (< status 0) (Result.Error (System.error-text)) (Result.Success data))))
8997

9098
(doc read-append
9199
"reads from the stream and appends to an existing byte buffer.

src/tcp_stream.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,31 @@ int TcpStream_send_MINUS_len_(TcpStream* s, String* msg, int len) {
5555
return (int)send_all(s->fd, *msg, (size_t)len);
5656
}
5757

58-
String TcpStream_read_(TcpStream* s) {
58+
String TcpStream_read_(TcpStream* s, int *status) {
5959
String buf = CARP_MALLOC(SOCK_BUF_SIZE + 1);
6060
ssize_t r = read(s->fd, buf, SOCK_BUF_SIZE);
61-
if (r < 0) {
62-
buf[0] = '\0';
61+
if (r > 0) {
62+
buf[r] = '\0';
63+
*status = (int)r;
6364
return buf;
6465
}
65-
buf[r] = '\0';
66+
buf[0] = '\0';
67+
*status = r == 0 ? 0 : -1;
6668
return buf;
6769
}
6870

69-
Array TcpStream_read_MINUS_bytes_(TcpStream* s) {
71+
Array TcpStream_read_MINUS_bytes_(TcpStream* s, int *status) {
7072
Array buf;
7173
buf.capacity = SOCK_BUF_SIZE;
7274
buf.data = CARP_MALLOC(SOCK_BUF_SIZE);
7375
ssize_t r = read(s->fd, buf.data, SOCK_BUF_SIZE);
74-
buf.len = r < 0 ? 0 : (int)r;
76+
if (r > 0) {
77+
buf.len = (int)r;
78+
*status = (int)r;
79+
return buf;
80+
}
81+
buf.len = 0;
82+
*status = r == 0 ? 0 : -1;
7583
return buf;
7684
}
7785

src/unix_stream.carp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@
3636
"UnixStream_send_MINUS_bytes_")
3737
(private read-)
3838
(hidden read-)
39-
(register read- (Fn [&UnixStream] String) "UnixStream_read_")
39+
(register read- (Fn [&UnixStream &Int] String) "UnixStream_read_")
4040
(private read-bytes-)
4141
(hidden read-bytes-)
4242
(register read-bytes-
43-
(Fn [&UnixStream] (Array Byte))
43+
(Fn [&UnixStream &Int] (Array Byte))
4444
"UnixStream_read_MINUS_bytes_")
4545
(private read-append-)
4646
(hidden read-append-)
@@ -83,10 +83,18 @@ Returns bytes sent or an error.")
8383
(doc read
8484
"reads up to 4096 bytes from the stream. Returns the data as a string, or an error.
8585
Returns an empty string on connection close.")
86-
(defn read [stream] (let [s (read- stream)] (Result.Success s)))
87-
88-
(doc read-bytes "reads up to 4096 bytes as a byte array.")
89-
(defn read-bytes [stream] (Result.Success (read-bytes- stream)))
86+
(defn read [stream]
87+
(let [status 0
88+
data (read- stream &status)]
89+
(if (< status 0) (Result.Error (System.error-text)) (Result.Success data))))
90+
91+
(doc read-bytes
92+
"reads up to 4096 bytes as a byte array. Returns an empty array on
93+
connection close, or an error if the read failed.")
94+
(defn read-bytes [stream]
95+
(let [status 0
96+
data (read-bytes- stream &status)]
97+
(if (< status 0) (Result.Error (System.error-text)) (Result.Success data))))
9098

9199
(doc read-append
92100
"reads from the stream and appends to an existing byte buffer.

src/unix_stream.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,31 @@ int UnixStream_send_MINUS_bytes_(UnixStream* s, Array* data) {
3939
return (int)send_all(s->fd, (const char*)data->data, data->len);
4040
}
4141

42-
String UnixStream_read_(UnixStream* s) {
42+
String UnixStream_read_(UnixStream* s, int *status) {
4343
String buf = CARP_MALLOC(SOCK_BUF_SIZE + 1);
4444
ssize_t r = read(s->fd, buf, SOCK_BUF_SIZE);
45-
if (r < 0) {
46-
buf[0] = '\0';
45+
if (r > 0) {
46+
buf[r] = '\0';
47+
*status = (int)r;
4748
return buf;
4849
}
49-
buf[r] = '\0';
50+
buf[0] = '\0';
51+
*status = r == 0 ? 0 : -1;
5052
return buf;
5153
}
5254

53-
Array UnixStream_read_MINUS_bytes_(UnixStream* s) {
55+
Array UnixStream_read_MINUS_bytes_(UnixStream* s, int *status) {
5456
Array buf;
5557
buf.capacity = SOCK_BUF_SIZE;
5658
buf.data = CARP_MALLOC(SOCK_BUF_SIZE);
5759
ssize_t r = read(s->fd, buf.data, SOCK_BUF_SIZE);
58-
buf.len = r < 0 ? 0 : (int)r;
60+
if (r > 0) {
61+
buf.len = (int)r;
62+
*status = (int)r;
63+
return buf;
64+
}
65+
buf.len = 0;
66+
*status = r == 0 ? 0 : -1;
5967
return buf;
6068
}
6169

0 commit comments

Comments
 (0)