From eba8ffcf70cca5dd802c5d33f6ecea814829f9fc Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Thu, 27 Dec 2018 13:46:53 -0800 Subject: [PATCH] `checkErr` now shows actual system error msg instead of unknown error (#9987) --- lib/system/sysio.nim | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/system/sysio.nim b/lib/system/sysio.nim index 20964b166a..5b0278d745 100644 --- a/lib/system/sysio.nim +++ b/lib/system/sysio.nim @@ -74,10 +74,21 @@ proc raiseEIO(msg: string) {.noinline, noreturn.} = proc raiseEOF() {.noinline, noreturn.} = sysFatal(EOFError, "EOF reached") +proc strerror(errnum: cint): cstring {.importc, header: "".} + +when not defined(NimScript): + var + errno {.importc, header: "".}: cint ## error variable + proc checkErr(f: File) = - if c_ferror(f) != 0: - c_clearerr(f) - raiseEIO("Unknown IO Error") + when not defined(NimScript): + if c_ferror(f) != 0: + let msg = "errno: " & $errno & " `" & $strerror(errno) & "`" + c_clearerr(f) + raiseEIO(msg) + else: + # shouldn't happen + quit(1) {.push stackTrace:off, profiler:off.} proc readBuffer(f: File, buffer: pointer, len: Natural): int =