about summary refs log tree commit diff stats
path: root/src/io
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-01-26 03:01:49 +0100
committerbptato <nincsnevem662@gmail.com>2024-01-26 03:01:49 +0100
commit1c0df44ae9d9ac498ff6335f044ff5294fd62441 (patch)
tree03869a9c9ddb43000987c720322835ebcbd76bc7 /src/io
parent9b5df91240ea3e38e58d771597cb2a2c3ca95f29 (diff)
downloadchawan-1c0df44ae9d9ac498ff6335f044ff5294fd62441.tar.gz
loader: clean up error handling
* remove pointless exception -> bool conversions; usually they were
  ignored anyway + exceptions are more convenient here
* add EPIPE handler to raisePosixIOError
* fix socketstream to use raisePosixIOError
* fix socketstream sendFileHandle error handling
* cgi: immediately return on file not found error
Diffstat (limited to 'src/io')
-rw-r--r--src/io/posixstream.nim3
-rw-r--r--src/io/socketstream.nim22
2 files changed, 17 insertions, 8 deletions
diff --git a/src/io/posixstream.nim b/src/io/posixstream.nim
index dbc48ece..73a957f8 100644
--- a/src/io/posixstream.nim
+++ b/src/io/posixstream.nim
@@ -14,6 +14,7 @@ type
   ErrorInterrupted* = object of IOError
   ErrorInvalid* = object of IOError
   ErrorConnectionReset* = object of IOError
+  ErrorBrokenPipe* = object of IOError
 
 proc raisePosixIOError*() =
   # In the nim stdlib, these are only constants on linux amd64, so we
@@ -30,6 +31,8 @@ proc raisePosixIOError*() =
     raise newException(ErrorInvalid, "invalid")
   elif errno == ECONNRESET:
     raise newException(ErrorConnectionReset, "connection reset by peer")
+  elif errno == EPIPE:
+    raise newException(ErrorBrokenPipe, "broken pipe")
   else:
     raise newException(IOError, $strerror(errno))
 
diff --git a/src/io/socketstream.nim b/src/io/socketstream.nim
index 31e4a3b7..9426f7a7 100644
--- a/src/io/socketstream.nim
+++ b/src/io/socketstream.nim
@@ -47,7 +47,7 @@ proc sockWriteData(s: Stream, buffer: pointer, len: int) =
   while i < len:
     let n = SocketStream(s).source.send(addr buffer[i], len - i)
     if n < 0:
-      raise newException(IOError, $strerror(errno))
+      raisePosixIOError()
     i += n
 
 proc sockAtEnd(s: Stream): bool =
@@ -64,7 +64,7 @@ proc sendfd(sock: SocketHandle, fd: cint): int {.importc.}
 proc sendFileHandle*(s: SocketStream, fd: FileHandle) =
   assert not s.source.hasDataBuffered
   let n = sendfd(s.source.getFd(), cint(fd))
-  if n < -1:
+  if n < 0:
     raisePosixIOError()
   assert n == 1 # we send a single nul byte as buf
 
@@ -82,7 +82,8 @@ proc recvFileHandle*(s: SocketStream): FileHandle =
 func newSocketStream*(): SocketStream =
   return SocketStream(
     readDataImpl: cast[proc (s: Stream, buffer: pointer, bufLen: int): int
-        {.nimcall, raises: [Defect, IOError, OSError], tags: [ReadIOEffect], gcsafe.}
+        {.nimcall, raises: [Defect, IOError, OSError], tags: [ReadIOEffect],
+        gcsafe.}
     ](sockReadData), # ... ???
     writeDataImpl: sockWriteData,
     atEndImpl: sockAtEnd,
@@ -94,19 +95,24 @@ proc setBlocking*(ss: SocketStream, blocking: bool) =
 
 # see serversocket.nim for an explanation
 {.compile: "connect_unix.c".}
-proc connect_unix_from_c(fd: cint, path: cstring, pathlen: cint): cint {.importc.}
+proc connect_unix_from_c(fd: cint, path: cstring, pathlen: cint): cint
+  {.importc.}
 
-proc connectSocketStream*(path: string, buffered = true, blocking = true): SocketStream =
+proc connectSocketStream*(path: string, buffered = true, blocking = true):
+    SocketStream =
   result = newSocketStream()
   result.blk = blocking
-  let sock = newSocket(Domain.AF_UNIX, SockType.SOCK_STREAM, Protocol.IPPROTO_IP, buffered)
+  let sock = newSocket(Domain.AF_UNIX, SockType.SOCK_STREAM,
+    Protocol.IPPROTO_IP, buffered)
   if not blocking:
     sock.getFd().setBlocking(false)
-  if connect_unix_from_c(cint(sock.getFd()), cstring(path), cint(path.len)) != 0:
+  if connect_unix_from_c(cint(sock.getFd()), cstring(path),
+      cint(path.len)) != 0:
     raiseOSError(osLastError())
   result.source = sock
 
-proc connectSocketStream*(pid: Pid, buffered = true, blocking = true): SocketStream =
+proc connectSocketStream*(pid: Pid, buffered = true, blocking = true):
+    SocketStream =
   try:
     connectSocketStream(getSocketPath(pid), buffered, blocking)
   except OSError: