From 6f2f3fdb6830c9f09f5442e027eb7768c3595b5a Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Mon, 10 Aug 2020 11:01:57 -0500 Subject: [PATCH] Return an error message if writes fail. Address #462. --- CHANGELOG.md | 2 ++ src/core/net.c | 10 ++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1bed45d0..ad3b16c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file. ## Unreleased - ??? - Update meson build script to fix bug on Debian's version of meson - Add `xprint`, `xprin`, `xprintf`, and `xprinf`. +- `net/write` now returns an error message if write fails. +- Fix issue with SIGPIPE on macOS and BSDs. ## 1.11.3 - 2020-08-03 - Add `JANET_HASHSEED` environment variable when `JANET_PRF` is enabled. diff --git a/src/core/net.c b/src/core/net.c index 29a49883..f20dfff0 100644 --- a/src/core/net.c +++ b/src/core/net.c @@ -350,6 +350,12 @@ static size_t janet_loop_event(size_t index) { if (nwrote > 0) { start += nwrote; } else { + if (nwrote == -1) { + const uint8_t *msg = janet_formatc("write error: %s", strerror(JLASTERR)); + resumeval = janet_wrap_string(msg); + } else { + resumeval = janet_cstringv("could not write"); + } start = len; } } @@ -637,7 +643,7 @@ static const JanetReg net_cfuns[] = { JDOC("(net/read stream nbytes &opt buf)\n\n" "Read up to n bytes from a stream, suspending the current fiber until the bytes are available. " "If less than n bytes are available (and more than 0), will push those bytes and return early. " - "Returns a buffer with up to n more bytes in it.") + "Returns a buffer with up to n more bytes in it, or nil if the read failed.") }, { "net/chunk", cfun_stream_chunk, @@ -648,7 +654,7 @@ static const JanetReg net_cfuns[] = { "net/write", cfun_stream_write, JDOC("(net/write stream data)\n\n" "Write data to a stream, suspending the current fiber until the write " - "completes. Returns stream.") + "completes. Returns nil, or an error message if the write failed.") }, { "net/close", cfun_stream_close,