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