Skip to content

errors: don't print an empty line for Exit with empty message#2312

Open
alliasgher wants to merge 2 commits intourfave:mainfrom
alliasgher:fix-exit-empty-message-stderr
Open

errors: don't print an empty line for Exit with empty message#2312
alliasgher wants to merge 2 commits intourfave:mainfrom
alliasgher:fix-exit-empty-message-stderr

Conversation

@alliasgher
Copy link
Copy Markdown

Summary

When cli.Exit("", exitCode) is called, HandleExitCoder still wrote a bare newline to ErrWriter (usually stderr), because it called fmt.Fprintln(ErrWriter, err) unconditionally. An ExitCoder with an empty message should produce no output.

Before

cli.Exit("", 1)
// stderr: "\n"  ← unexpected blank line

After

cli.Exit("", 1)
// stderr: ""    ← nothing printed

Fix

Guard the fmt.Fprintln / fmt.Fprintf calls with a check that err.Error() != "" before writing to ErrWriter.

Fixes #2263

Checklist

  • Test added (TestHandleExitCoder_EmptyMessage)
  • Full test suite passes

When FlagStringer returns a string without a tab character,
strings.Index returns -1 and out[-1:] panics with
"slice bounds out of range [-1:]".

Guard against this by clamping the index to 0 when no tab is
found. In that case the entire FlagStringer output is used as
the suffix portion of the formatted string, matching the intent
of callers who use a custom stringer without the usual tab layout.

Fixes urfave#2303

Signed-off-by: alliasgher <alliasgher123@gmail.com>
When HandleExitCoder received an ExitCoder whose Error() returned an
empty string (e.g. cli.Exit("", code)), it still called
fmt.Fprintln(ErrWriter, err) which wrote a bare newline to stderr.

Guard the print with a check on the error message so that an empty
message produces no output at all.

Fixes urfave#2263

Signed-off-by: alliasgher <alliasgher123@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v3.6.2 adds empty line to stderr when cli.Exit is called with empty message

1 participant