github.com/vmware/govmomi@v0.43.0/fault/fault_example_test.go (about)

     1  /*
     2  Copyright (c) 2024-2024 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package fault_test
    18  
    19  import (
    20  	"fmt"
    21  	"reflect"
    22  
    23  	"github.com/vmware/govmomi/fault"
    24  	"github.com/vmware/govmomi/task"
    25  	"github.com/vmware/govmomi/vim25/soap"
    26  	"github.com/vmware/govmomi/vim25/types"
    27  )
    28  
    29  func ExampleAs_faultByAddress() {
    30  	var (
    31  		err    any
    32  		target *types.InvalidPowerState
    33  	)
    34  
    35  	err = task.Error{
    36  		LocalizedMethodFault: &types.LocalizedMethodFault{
    37  			Fault: &types.InvalidPowerState{
    38  				ExistingState:  types.VirtualMachinePowerStatePoweredOn,
    39  				RequestedState: types.VirtualMachinePowerStatePoweredOff,
    40  			},
    41  			LocalizedMessage: "vm must be powered off to encrypt",
    42  		},
    43  	}
    44  
    45  	localizedMessage, ok := fault.As(err, &target)
    46  
    47  	fmt.Printf("result              = %v\n", ok)
    48  	fmt.Printf("localizedMessage    = %v\n", localizedMessage)
    49  	fmt.Printf("existingPowerState  = %v\n", target.ExistingState)
    50  	fmt.Printf("requestedPowerState = %v\n", target.RequestedState)
    51  
    52  	// Output:
    53  	// result              = true
    54  	// localizedMessage    = vm must be powered off to encrypt
    55  	// existingPowerState  = poweredOn
    56  	// requestedPowerState = poweredOff
    57  }
    58  
    59  type valueFault uint8
    60  
    61  func (f valueFault) GetMethodFault() *types.MethodFault {
    62  	return nil
    63  }
    64  
    65  func ExampleAs_faultByValue() {
    66  	var (
    67  		err    any
    68  		target valueFault
    69  	)
    70  
    71  	err = &types.SystemError{
    72  		RuntimeFault: types.RuntimeFault{
    73  			MethodFault: types.MethodFault{
    74  				FaultCause: &types.LocalizedMethodFault{
    75  					Fault:            valueFault(1),
    76  					LocalizedMessage: "fault by value",
    77  				},
    78  			},
    79  		},
    80  	}
    81  
    82  	localizedMessage, ok := fault.As(err, &target)
    83  
    84  	fmt.Printf("result              = %v\n", ok)
    85  	fmt.Printf("localizedMessage    = %v\n", localizedMessage)
    86  	fmt.Printf("value               = %d\n", target)
    87  
    88  	// Output:
    89  	// result              = true
    90  	// localizedMessage    = fault by value
    91  	// value               = 1
    92  }
    93  
    94  func ExampleIs_baseMethodFault() {
    95  	var (
    96  		err    any
    97  		target types.BaseMethodFault
    98  	)
    99  
   100  	err = &types.SystemError{}
   101  	target = &types.SystemError{}
   102  
   103  	fmt.Printf("result = %v\n", fault.Is(err, target))
   104  
   105  	// Output:
   106  	// result = true
   107  }
   108  
   109  func ExampleIs_nestedFault() {
   110  	var (
   111  		err    any
   112  		target types.BaseMethodFault
   113  	)
   114  
   115  	err = task.Error{
   116  		LocalizedMethodFault: &types.LocalizedMethodFault{
   117  			Fault: &types.RuntimeFault{
   118  				MethodFault: types.MethodFault{
   119  					FaultCause: &types.LocalizedMethodFault{
   120  						Fault: &types.SystemError{},
   121  					},
   122  				},
   123  			},
   124  		},
   125  	}
   126  	target = &types.SystemError{}
   127  
   128  	fmt.Printf("result = %v\n", fault.Is(err, target))
   129  
   130  	// Output:
   131  	// result = true
   132  }
   133  
   134  func ExampleIs_soapFault() {
   135  	var (
   136  		err    any
   137  		target types.BaseMethodFault
   138  	)
   139  
   140  	err = soap.WrapSoapFault(&soap.Fault{
   141  		Detail: struct {
   142  			Fault types.AnyType "xml:\",any,typeattr\""
   143  		}{
   144  			Fault: &types.RuntimeFault{
   145  				MethodFault: types.MethodFault{
   146  					FaultCause: &types.LocalizedMethodFault{
   147  						Fault: &types.SystemError{},
   148  					},
   149  				},
   150  			},
   151  		},
   152  	})
   153  	target = &types.SystemError{}
   154  
   155  	fmt.Printf("result = %v\n", fault.Is(err, target))
   156  
   157  	// Output:
   158  	// result = true
   159  }
   160  
   161  func ExampleIs_vimFault() {
   162  	var (
   163  		err    any
   164  		target types.BaseMethodFault
   165  	)
   166  
   167  	err = soap.WrapVimFault(&types.RuntimeFault{
   168  		MethodFault: types.MethodFault{
   169  			FaultCause: &types.LocalizedMethodFault{
   170  				Fault: &types.SystemError{},
   171  			},
   172  		},
   173  	})
   174  	target = &types.SystemError{}
   175  
   176  	fmt.Printf("result = %v\n", fault.Is(err, target))
   177  
   178  	// Output:
   179  	// result = true
   180  }
   181  
   182  func ExampleIn_printAllTypeNamesAndMessages() {
   183  	var (
   184  		err     any
   185  		onFault fault.OnFaultFn
   186  	)
   187  
   188  	err = task.Error{
   189  		LocalizedMethodFault: &types.LocalizedMethodFault{
   190  			Fault: &types.RuntimeFault{
   191  				MethodFault: types.MethodFault{
   192  					FaultCause: &types.LocalizedMethodFault{
   193  						Fault:            &types.SystemError{},
   194  						LocalizedMessage: "inner message",
   195  					},
   196  				},
   197  			},
   198  			LocalizedMessage: "outer message",
   199  		},
   200  	}
   201  
   202  	onFault = func(
   203  		fault types.BaseMethodFault,
   204  		localizedMessage string,
   205  		localizableMessages []types.LocalizableMessage) bool {
   206  
   207  		fmt.Printf("type             = %s\n", reflect.ValueOf(fault).Elem().Type().Name())
   208  		fmt.Printf("localizedMessage = %s\n", localizedMessage)
   209  
   210  		// Return false to continue discovering faults.
   211  		return false
   212  	}
   213  
   214  	fault.In(err, onFault)
   215  
   216  	// Output:
   217  	// type             = RuntimeFault
   218  	// localizedMessage = outer message
   219  	// type             = SystemError
   220  	// localizedMessage = inner message
   221  }
   222  
   223  func ExampleIn_printFirstDiscoveredTypeNameAndMessage() {
   224  	var (
   225  		err     any
   226  		onFault fault.OnFaultFn
   227  	)
   228  
   229  	err = task.Error{
   230  		LocalizedMethodFault: &types.LocalizedMethodFault{
   231  			Fault: &types.RuntimeFault{
   232  				MethodFault: types.MethodFault{
   233  					FaultCause: &types.LocalizedMethodFault{
   234  						Fault:            &types.SystemError{},
   235  						LocalizedMessage: "inner message",
   236  					},
   237  				},
   238  			},
   239  			LocalizedMessage: "outer message",
   240  		},
   241  	}
   242  
   243  	onFault = func(
   244  		fault types.BaseMethodFault,
   245  		localizedMessage string,
   246  		localizableMessages []types.LocalizableMessage) bool {
   247  
   248  		fmt.Printf("type             = %s\n", reflect.ValueOf(fault).Elem().Type().Name())
   249  		fmt.Printf("localizedMessage = %s\n", localizedMessage)
   250  
   251  		// Return true to force discovery to halt.
   252  		return true
   253  	}
   254  
   255  	fault.In(err, onFault)
   256  
   257  	// Output:
   258  	// type             = RuntimeFault
   259  	// localizedMessage = outer message
   260  }