This occasionally comes up
.nim(855) writeFile
Error: unhandled exception: cannot open: /dev/stderr [IOError]
The only proc I have that uses writeFile
#
# >
#
# Write 'text\n' to 'filename', overwrite previous content. Close on finish.
# example:
# "Hello" & " world" >* "/tmp/test.txt"
# "Hello" >* "/dev/stderr"
#
proc `>*`*(text, filename: string): bool {.discardable.} =
var text = text & "\n"
try:
writeFile(filename, text)
except ValueError as e:
echo e.msg
It's called by this proc
# Send message to stderr
#
# stdErr("Ending OK")
#
#
proc stdErr*(s: string) =
s >* "/dev/stderr"
What's happening it can't open stderr? I'm running an old version of nim 1.4 and don't want to upgrade only for this. Is there another recommend method for writing to stderr? Thanks!
What's wrong with stdout.write(s)?
I'd totally forgotten about symlinks in /dev. You can always just write to fd #2 :)
Excellent thanks for catching that. I did some research and found this:
try:
writeFile(filename, text)
except Exception as e:
echo e.msg
I'm try it and see what happens, because it seems more robust for any type of Exception, future proof. If the same problem I'll change to IOError. Still, I wonder why it fails writing to stderr.
I wonder why it fails writing to stderr.
its even more of a head scratcher how it can write the exception to stderr after it couldnt write to stderr
This appears resolved by setting the exception per above. Now looks like:
#
# showException
#
proc showException(msg: string) =
try:
stderr.write(msg)
except Exception as e:
discard e.msg # optionally log or email
#
# >*
#
# Write 'text\n' to 'filename', overwrite previous content. Close on finish.
# example:
# "Hello" & " world" >* "/tmp/test.txt"
# "Hello" >* "/dev/stderr"
#
proc `>*`*(text, filename: string): bool {.discardable.} =
var text = text & "\n"
try:
writeFile(filename, text)
except Exception as e:
showException(e.msg)
return false
return true
I suspect there was never an actual error writing to stderr, but because of the wrong exception value, it caused something unexpected.
It would be nice if when there is an incorrect value, it defaults to "except Exception", to protect programmers from themselves. Or gave a compile warning if possible. I don't know how the exception system works underneath.