Send localized message originating from plugin (if applicable)

This commit is contained in:
sophia 2022-01-07 16:53:40 -06:00 committed by Paul Hinze
parent 45e64b9890
commit c4e97bb89b
No known key found for this signature in database
GPG key ID: B69DEDF2D55501C0
2 changed files with 17 additions and 4 deletions

View file

@ -2,12 +2,13 @@ package cli
import (
"context"
"errors"
"fmt"
"reflect"
"strconv"
"github.com/DavidGamba/go-getoptions"
"github.com/DavidGamba/go-getoptions/option"
"google.golang.org/grpc/status"
"github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk"
"github.com/hashicorp/vagrant-plugin-sdk/terminal"
@ -94,8 +95,13 @@ func (c *DynamicCommand) Run(args []string) int {
cl.UI().Output("Running of task "+c.name+" failed unexpectedly\n", terminal.WithErrorStyle())
cl.UI().Output("Error: "+err.Error(), terminal.WithErrorStyle())
} else if !r.RunResult {
cl.UI().Output("Error: "+r.RunError.Message+"\n", terminal.WithErrorStyle())
err = errors.New("execution failed")
runErrorStatus := status.FromProto(r.RunError)
details := runErrorStatus.Details()
// TODO: seach through the details for a localized message if exists
userMessage := details[0].(*vagrant_plugin_sdk.Errors_LocalizedErrorMessage).Message
cl.UI().Output("Error: "+userMessage+"\n", terminal.WithErrorStyle())
runErr := status.FromProto(r.RunError)
err = fmt.Errorf("execution failed, %w", runErr.Err())
}
c.Log.Debug("result from operation", "task", c.name, "result", r)

View file

@ -17,10 +17,17 @@ module VagrantPlugins
begin
super(*args, **opts, &block)
rescue => err
localized_msg_details_any = Google::Protobuf::Any.new
localized_msg_details_any.pack(
SDK::Errors::LocalizedErrorMessage.new(
locale: "en-US", message: err.message
)
)
proto = Google::Rpc::Status.new(
code: GRPC::Core::StatusCodes::UNKNOWN,
message: "#{err.message}\n#{err.backtrace.join("\n")}",
details: [mapper.map(err.message, to: Google::Protobuf::Any)]
details: [localized_msg_details_any]
)
encoded_proto = Google::Rpc::Status.encode(proto)
grpc_status_details_bin_trailer = 'grpc-status-details-bin'