Improve run operation error handling

There are some cases where the error that gets returned from scope.Run
is not a core.CommandError which results in a type mismatch panic. This
fixes that and wraps any generic errors into the proper response struct.
This commit is contained in:
Paul Hinze 2022-06-10 11:14:14 -05:00
parent f2417e09a8
commit e829550ea7
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0

View File

@ -2,9 +2,12 @@ package runner
import (
"context"
"fmt"
"github.com/hashicorp/vagrant/internal/core"
"github.com/hashicorp/vagrant/internal/server/proto/vagrant_server"
"google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/grpc/codes"
)
type Runs interface {
@ -35,10 +38,18 @@ func (r *Runner) executeRunOp(
jrr.RunResult = err == nil
if err != nil {
jrr.RunError = err.(core.CommandError).Status()
if cmdErr, ok := err.(core.CommandError); ok {
jrr.RunError = err.(core.CommandError).Status()
jrr.ExitCode = int32(cmdErr.ExitCode())
} else {
// If we have an error without a status we'll make one here
jrr.RunError = &status.Status{
Code: int32(codes.Unknown),
Message: fmt.Sprintf("Unexpected error from run operation: %s", err),
}
jrr.ExitCode = 1
}
}
r.logger.Info("run operation is complete!")