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:
parent
8522393cec
commit
4dbd23af3b
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue