ignore exceptions while writing to stdout/stderr (#79)

`writeLine` can fail with `IOError`, and `styledWrite` when using colors
also with `ValueError`. To ensure that the control flow is unaffected,
simply ignore those errors while writing human-readable output to fds.
This commit is contained in:
Etan Kissling 2023-06-08 15:26:06 +02:00 committed by GitHub
parent 8522393cec
commit 4dbd23af3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 7 deletions

View File

@ -121,10 +121,16 @@ when useBufferedOutput:
else:
template errorOutput(args: varargs[untyped]) =
styledWrite stderr, args
try:
styledWrite stderr, args
except IOError, ValueError:
discard
template helpOutput(args: varargs[untyped]) =
styledWrite stdout, args
try:
styledWrite stdout, args
except IOError, ValueError:
discard
template flushOutput =
discard
@ -969,9 +975,15 @@ proc loadImpl[C, SecondarySources](
let trailing = if opt.typename != "bool": "=" else: ""
if argName in filterKind and len(opt.name) > 0:
stdout.writeLine("--", opt.name, trailing)
try:
stdout.writeLine("--", opt.name, trailing)
except IOError:
discard
if argAbbr in filterKind and len(opt.abbr) > 0:
stdout.writeLine('-', opt.abbr, trailing)
try:
stdout.writeLine('-', opt.abbr, trailing)
except IOError:
discard
let completion = splitCompletionLine()
# If we're not asked to complete a command line the result is an empty list
@ -1017,12 +1029,18 @@ proc loadImpl[C, SecondarySources](
let opt = findOpt(cmdStack, option_word)
if opt != nil:
for arg in getArgCompletions(opt, cur_word):
stdout.writeLine(arg)
try:
stdout.writeLine(arg)
except IOError:
discard
elif cmdStack[^1].hasSubCommands:
# Show all the available subcommands
for subCmd in subCmds(cmdStack[^1]):
if startsWithIgnoreStyle(subCmd.name, cur_word):
stdout.writeLine(subCmd.name)
try:
stdout.writeLine(subCmd.name)
except IOError:
discard
else:
# Full options listing
for i in countdown(cmdStack.len - 1, 0):
@ -1234,4 +1252,3 @@ func load*(f: TypedInputFile): f.ContentType =
else:
mixin loadFile
loadFile(f.Format, f.string, f.ContentType)