From c4ffe464e89c93cbf83f96cce01c76b48ea328cf Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Fri, 19 May 2023 10:53:21 -0700 Subject: [PATCH] fix: add esxcli.Fault and revert Error() string PR #3072 added fault detail to the error message, breaking existing code at runtime. --- govc/host/esxcli/esxcli.go | 8 ++++++-- govc/host/esxcli/executor.go | 29 ++++++++++++++++++++++------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/govc/host/esxcli/esxcli.go b/govc/host/esxcli/esxcli.go index 6ca0cd39b..3ddd14300 100644 --- a/govc/host/esxcli/esxcli.go +++ b/govc/host/esxcli/esxcli.go @@ -1,11 +1,11 @@ /* -Copyright (c) 2014-2015 VMware, Inc. All Rights Reserved. +Copyright (c) 2014-2023 VMware, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 +http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, @@ -18,6 +18,7 @@ package esxcli import ( "context" + "errors" "flag" "fmt" "io" @@ -92,6 +93,9 @@ func (cmd *esxcli) Run(ctx context.Context, f *flag.FlagSet) error { res, err := e.Run(f.Args()) if err != nil { + if f, ok := err.(*Fault); ok { + return errors.New(f.messageDetail()) + } return err } diff --git a/govc/host/esxcli/executor.go b/govc/host/esxcli/executor.go index 7959e679e..792809eac 100644 --- a/govc/host/esxcli/executor.go +++ b/govc/host/esxcli/executor.go @@ -1,11 +1,11 @@ /* -Copyright (c) 2014-2015 VMware, Inc. All Rights Reserved. +Copyright (c) 2014-2023 VMware, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 +http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, @@ -18,7 +18,6 @@ package esxcli import ( "context" - "errors" "fmt" "github.com/vmware/govmomi/internal" @@ -27,6 +26,23 @@ import ( "github.com/vmware/govmomi/vim25/xml" ) +type Fault struct { + Message string + Detail string +} + +func (f Fault) Error() string { + return f.Message +} + +func (f Fault) messageDetail() string { + if f.Detail != "" { + return fmt.Sprintf("%s %s", f.Message, f.Detail) + } + + return f.Message +} + type Executor struct { c *vim25.Client host *object.HostSystem @@ -138,11 +154,10 @@ func (e *Executor) Execute(req *internal.ExecuteSoapRequest, res interface{}) er if x.Returnval != nil { if x.Returnval.Fault != nil { - msg := x.Returnval.Fault.FaultMsg - if x.Returnval.Fault.FaultDetail != "" { - msg = fmt.Sprintf("%s %s", msg, x.Returnval.Fault.FaultDetail) + return &Fault{ + x.Returnval.Fault.FaultMsg, + x.Returnval.Fault.FaultDetail, } - return errors.New(msg) } if err := xml.Unmarshal([]byte(x.Returnval.Response), res); err != nil {