Fix nil dereference bug in new error handling logic

Found this while running `./vagrant box` with no args to get the help
output. It turns out you can have an empty RunResult but also a nil
error. I took the occasion to unwind the conditional tree a bit which
hopefully makes it a bit easier to read.
This commit is contained in:
Paul Hinze 2022-03-30 15:24:43 -05:00
parent 1341bfe0af
commit 73a1be95fe
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0

View File

@ -95,10 +95,9 @@ func (c *DynamicCommand) Run(args []string) int {
modifier, modifier,
) )
if err != nil { // If nothing failed but we didn't get a Result back, something may
cl.UI().Output("Running of task "+c.name+" failed unexpectedly\n", terminal.WithErrorStyle()) // have gone wrong on the far side so we need to interpret the error.
cl.UI().Output("Error: "+err.Error(), terminal.WithErrorStyle()) if err == nil && !r.RunResult {
} else if !r.RunResult {
runErrorStatus := status.FromProto(r.RunError) runErrorStatus := status.FromProto(r.RunError)
details := runErrorStatus.Details() details := runErrorStatus.Details()
userError := false userError := false
@ -112,16 +111,21 @@ func (c *DynamicCommand) Run(args []string) int {
// All user-facing errors from Ruby use a 1 exit code. See // All user-facing errors from Ruby use a 1 exit code. See
// Vagrant::Errors::VagrantError. // Vagrant::Errors::VagrantError.
r.ExitCode = 1 r.ExitCode = 1
} }
} }
// If there wasn't a user-facing error, just assign the returned
// error (if any) from the response and assign that back out so it
// can be displayed as an unexpected error.
if !userError { if !userError {
runErr := status.FromProto(r.RunError) err = runErrorStatus.Err()
err = runErr.Err()
cl.UI().Output("Unexpected Error: "+err.Error()+"\n", terminal.WithErrorStyle())
} }
} }
if err != nil {
cl.UI().Output("Running of task "+c.name+" failed unexpectedly\n", terminal.WithErrorStyle())
cl.UI().Output("Error: "+err.Error(), terminal.WithErrorStyle())
}
c.Log.Debug("result from operation", "task", c.name, "result", r) c.Log.Debug("result from operation", "task", c.name, "result", r)
return err return err