Skip to content

Commit

Permalink
Ensure error messages always end in a period.
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchell-as committed Jan 22, 2024
1 parent fe3d58e commit 3868090
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
14 changes: 8 additions & 6 deletions internal/runbits/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (o *OutputError) MarshalOutput(f output.Format) interface{} {
rerrs = []error{o.error}
}
for _, errv := range rerrs {
message := trimError(locale.ErrorMessage(errv))
message := normalizeError(locale.ErrorMessage(errv))
if f == output.PlainFormatName {
outLines = append(outLines, formatMessage(message)...)
} else {
Expand All @@ -76,7 +76,7 @@ func (o *OutputError) MarshalOutput(f output.Format) interface{} {
outLines = append(outLines, "") // separate error from "Need More Help?" header
outLines = append(outLines, strings.TrimSpace(output.Title(locale.Tl("err_more_help", "Need More Help?")).String()))
for _, tip := range errorTips {
outLines = append(outLines, fmt.Sprintf(" [DISABLED]•[/RESET] %s", trimError(tip)))
outLines = append(outLines, fmt.Sprintf(" [DISABLED]•[/RESET] %s", normalizeError(tip)))
}
}
return strings.Join(outLines, "\n")
Expand Down Expand Up @@ -127,11 +127,13 @@ func (o *OutputError) MarshalStructured(f output.Format) interface{} {
return output.StructuredError{message, getErrorTips(o.error)}
}

func trimError(msg string) string {
if strings.Count(msg, ".") > 1 || strings.Count(msg, ",") > 0 {
return msg // Don't trim dots if we have multiple sentences.
// normalizeError ensures the given erorr message ends with a period.
func normalizeError(msg string) string {
msg = strings.TrimRight(msg, " ")
if !strings.HasSuffix(msg, ".") {
msg = msg + "."
}
return strings.TrimRight(msg, " .")
return msg
}

// ParseUserFacing returns the exit code and a user facing error message.
Expand Down
2 changes: 1 addition & 1 deletion test/integration/errors_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (suite *ErrorsIntegrationTestSuite) TestMultiError() {
defer ts.Close()

cp := ts.Spawn("__test", "multierror")
cp.ExpectRe(`\s+x error1\s+\s+x error2\s+x error3\s+x error4\s+█\s+Need More Help`)
cp.ExpectRe(`\s+x error1.\s+\s+x error2.\s+x error3.\s+x error4.\s+█\s+Need More Help`)
cp.ExpectExitCode(1)
ts.IgnoreLogErrors()
}
Expand Down

0 comments on commit 3868090

Please sign in to comment.