debian-forge-composer/vendor/github.com/vmware/govmomi/fault/fault.go
Achilleas Koutsou 3fd7092db5 go.mod: update osbuild/images to v0.156.0
tag v0.155.0
Tagger: imagebuilder-bot <imagebuilder-bots+imagebuilder-bot@redhat.com>

Changes with 0.155.0

----------------
  * Fedora 43: add shadow-utils when LockRoot is enabled, update cloud-init service name (osbuild/images#1618)
    * Author: Achilleas Koutsou, Reviewers: Gianluca Zuccarelli, Michael Vogt
  * Update osbuild dependency commit ID to latest (osbuild/images#1609)
    * Author: SchutzBot, Reviewers: Achilleas Koutsou, Simon de Vlieger, Tomáš Hozza
  * Update snapshots to 20250626 (osbuild/images#1623)
    * Author: SchutzBot, Reviewers: Achilleas Koutsou, Simon de Vlieger
  * distro/rhel9: xz compress azure-cvm image type [HMS-8587] (osbuild/images#1620)
    * Author: Achilleas Koutsou, Reviewers: Simon de Vlieger, Tomáš Hozza
  * distro/rhel: introduce new image type: Azure SAP Apps [HMS-8738] (osbuild/images#1612)
    * Author: Achilleas Koutsou, Reviewers: Simon de Vlieger, Tomáš Hozza
  * distro/rhel: move ansible-core to sap_extras_pkgset (osbuild/images#1624)
    * Author: Achilleas Koutsou, Reviewers: Brian C. Lane, Tomáš Hozza
  * github/create-tag: allow passing the version when run manually (osbuild/images#1621)
    * Author: Achilleas Koutsou, Reviewers: Lukáš Zapletal, Tomáš Hozza
  * rhel9: move image-config into pure YAML (HMS-8593) (osbuild/images#1616)
    * Author: Michael Vogt, Reviewers: Achilleas Koutsou, Simon de Vlieger
  * test: split manifest checksums into separate files (osbuild/images#1625)
    * Author: Achilleas Koutsou, Reviewers: Simon de Vlieger, Tomáš Hozza

— Somewhere on the Internet, 2025-06-30

---

tag v0.156.0
Tagger: imagebuilder-bot <imagebuilder-bots+imagebuilder-bot@redhat.com>

Changes with 0.156.0

----------------
  * Many: delete repositories for EOL distributions (HMS-7044) (osbuild/images#1607)
    * Author: Tomáš Hozza, Reviewers: Michael Vogt, Simon de Vlieger
  * RHSM/facts: add 'image-builder CLI' API type (osbuild/images#1640)
    * Author: Tomáš Hozza, Reviewers: Brian C. Lane, Simon de Vlieger
  * Update dependencies 2025-06-29 (osbuild/images#1628)
    * Author: SchutzBot, Reviewers: Simon de Vlieger, Tomáš Hozza
  * Update osbuild dependency commit ID to latest (osbuild/images#1627)
    * Author: SchutzBot, Reviewers: Simon de Vlieger, Tomáš Hozza
  * [RFC] image: drop `InstallWeakDeps` from image.DiskImage (osbuild/images#1642)
    * Author: Michael Vogt, Reviewers: Brian C. Lane, Simon de Vlieger, Tomáš Hozza
  * build(deps): bump the go-deps group across 1 directory with 3 updates (osbuild/images#1632)
    * Author: dependabot[bot], Reviewers: SchutzBot, Tomáš Hozza
  * distro/rhel10: xz compress azure-cvm image type (osbuild/images#1638)
    * Author: Achilleas Koutsou, Reviewers: Brian C. Lane, Simon de Vlieger
  * distro: cleanup/refactor distro/{defs,generic} (HMS-8744) (osbuild/images#1570)
    * Author: Michael Vogt, Reviewers: Simon de Vlieger, Tomáš Hozza
  * distro: remove some hardcoded values from generic/images.go (osbuild/images#1636)
    * Author: Michael Vogt, Reviewers: Simon de Vlieger, Tomáš Hozza
  * distro: small tweaks for the YAML based imagetypes (osbuild/images#1622)
    * Author: Michael Vogt, Reviewers: Brian C. Lane, Simon de Vlieger
  * fedora/wsl: packages and locale (osbuild/images#1635)
    * Author: Simon de Vlieger, Reviewers: Michael Vogt, Tomáš Hozza
  * image/many: make compression more generic (osbuild/images#1634)
    * Author: Simon de Vlieger, Reviewers: Brian C. Lane, Michael Vogt
  * manifest: handle content template name with spaces (osbuild/images#1641)
    * Author: Bryttanie, Reviewers: Brian C. Lane, Michael Vogt, Tomáš Hozza
  * many: implement gzip (osbuild/images#1633)
    * Author: Simon de Vlieger, Reviewers: Michael Vogt, Tomáš Hozza
  * rhel/azure: set GRUB_TERMINAL based on architecture [RHEL-91383] (osbuild/images#1626)
    * Author: Achilleas Koutsou, Reviewers: Simon de Vlieger, Tomáš Hozza

— Somewhere on the Internet, 2025-07-07

---
2025-07-14 13:13:20 +02:00

305 lines
8.6 KiB
Go

// © Broadcom. All Rights Reserved.
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
// SPDX-License-Identifier: Apache-2.0
package fault
import (
"reflect"
"github.com/vmware/govmomi/vim25/types"
)
// As finds the first fault in the error's tree that matches target, and if one
// is found, sets the target to that fault value and returns the fault's
// localized message and true. Otherwise, false is returned.
//
// The tree is inspected according to the object type. If the object implements
// Golang's error interface, the Unwrap() error or Unwrap() []error methods are
// repeatedly checked for additional errors. If the object implements GoVmomi's
// BaseMethodFault or HasLocalizedMethodFault interfaces, the object is checked
// for an underlying FaultCause. When err wraps multiple errors or faults, err
// is examined followed by a depth-first traversal of its children.
//
// An error matches target if the error's concrete value is assignable to the
// value pointed to by target, or if the error has a method
// AsFault(BaseMethodFault) (string, bool) such that AsFault(BaseMethodFault)
// returns true. In the latter case, the AsFault method is responsible for
// setting target.
//
// An error type might provide an AsFault method so it can be treated as if it
// were a different error type.
//
// This function panics if err does not implement error, types.BaseMethodFault,
// types.HasLocalizedMethodFault, Fault() types.BaseMethodFault, or if target is
// not a pointer.
func As(err, target any) (localizedMessage string, okay bool) {
if err == nil {
return
}
if target == nil {
panic("fault: target cannot be nil")
}
val := reflect.ValueOf(target)
typ := val.Type()
if typ.Kind() != reflect.Ptr || val.IsNil() {
panic("fault: target must be a non-nil pointer")
}
targetType := typ.Elem()
if targetType.Kind() != reflect.Interface &&
!targetType.Implements(baseMethodFaultType) {
panic("fault: *target must be interface or implement BaseMethodFault")
}
if !as(err, target, val, targetType, &localizedMessage) {
return "", false
}
return localizedMessage, true
}
func as(
err,
target any,
targetVal reflect.Value,
targetType reflect.Type,
localizedMsg *string) bool {
for {
if reflect.TypeOf(err).AssignableTo(targetType) {
targetVal.Elem().Set(reflect.ValueOf(err))
return true
}
if tErr, ok := err.(hasAsFault); ok {
if msg, ok := tErr.AsFault(target); ok {
*localizedMsg = msg
return true
}
return false
}
switch tErr := err.(type) {
case types.HasLocalizedMethodFault:
if fault := tErr.GetLocalizedMethodFault(); fault != nil {
*localizedMsg = fault.LocalizedMessage
if fault.Fault != nil {
return as(
fault.Fault,
target,
targetVal,
targetType,
localizedMsg)
}
}
return false
case types.BaseMethodFault:
if fault := tErr.GetMethodFault(); fault != nil {
if fault.FaultCause != nil {
*localizedMsg = fault.FaultCause.LocalizedMessage
return as(
fault.FaultCause,
target,
targetVal,
targetType,
localizedMsg)
}
}
return false
case hasFault:
if fault := tErr.Fault(); fault != nil {
return as(fault, target, targetVal, targetType, localizedMsg)
}
return false
case unwrappableError:
if err = tErr.Unwrap(); err == nil {
return false
}
case unwrappableErrorSlice:
for _, err := range tErr.Unwrap() {
if err == nil {
continue
}
return as(err, target, targetVal, targetType, localizedMsg)
}
return false
default:
return false
}
}
}
// Is reports whether any fault in err's tree matches target.
//
// The tree is inspected according to the object type. If the object implements
// Golang's error interface, the Unwrap() error or Unwrap() []error methods are
// repeatedly checked for additional errors. If the object implements GoVmomi's
// BaseMethodFault or HasLocalizedMethodFault interfaces, the object is checked
// for an underlying FaultCause. When err wraps multiple errors or faults, err
// is examined followed by a depth-first traversal of its children.
//
// An error is considered to match a target if it is equal to that target or if
// it implements a method IsFault(BaseMethodFault) bool such that
// IsFault(BaseMethodFault) returns true.
//
// An error type might provide an IsFault method so it can be treated as
// equivalent to an existing fault. For example, if MyFault defines:
//
// func (m MyFault) IsFault(target BaseMethodFault) bool {
// return target == &types.NotSupported{}
// }
//
// then IsFault(MyError{}, &types.NotSupported{}) returns true. An IsFault
// method should only shallowly compare err and the target and not unwrap
// either.
func Is(err any, target types.BaseMethodFault) bool {
if target == nil {
return err == target
}
isComparable := reflect.TypeOf(target).Comparable()
return is(err, target, isComparable)
}
func is(err any, target types.BaseMethodFault, targetComparable bool) bool {
for {
if targetComparable && err == target {
return true
}
if tErr, ok := err.(hasIsFault); ok && tErr.IsFault(target) {
return true
}
switch tErr := err.(type) {
case types.HasLocalizedMethodFault:
fault := tErr.GetLocalizedMethodFault()
if fault == nil {
return false
}
err = fault.Fault
case types.BaseMethodFault:
if reflect.ValueOf(err).Type() == reflect.ValueOf(target).Type() {
return true
}
fault := tErr.GetMethodFault()
if fault == nil {
return false
}
err = fault.FaultCause
case hasFault:
if err = tErr.Fault(); err == nil {
return false
}
case unwrappableError:
if err = tErr.Unwrap(); err == nil {
return false
}
case unwrappableErrorSlice:
for _, err := range tErr.Unwrap() {
if is(err, target, targetComparable) {
return true
}
}
return false
default:
return false
}
}
}
// OnFaultFn is called for every fault encountered when inspecting an error
// or fault for a fault tree. The In function returns when the entire tree is
// inspected or the OnFaultFn returns true.
type OnFaultFn func(
fault types.BaseMethodFault,
localizedMessage string,
localizableMessages []types.LocalizableMessage) bool
// In invokes onFaultFn for each fault in err's tree.
//
// The tree is inspected according to the object type. If the object implements
// Golang's error interface, the Unwrap() error or Unwrap() []error methods are
// repeatedly checked for additional errors. If the object implements GoVmomi's
// BaseMethodFault or HasLocalizedMethodFault interfaces, the object is checked
// for an underlying FaultCause. When err wraps multiple errors or faults, err
// is examined followed by a depth-first traversal of its children.
//
// This function panics if err does not implement error, types.BaseMethodFault,
// types.HasLocalizedMethodFault, Fault() types.BaseMethodFault, or if onFaultFn
// is nil.
func In(err any, onFaultFn OnFaultFn) {
if onFaultFn == nil {
panic("fault: onFaultFn must not be nil")
}
switch tErr := err.(type) {
case types.HasLocalizedMethodFault:
inFault(tErr.GetLocalizedMethodFault(), onFaultFn)
case types.BaseMethodFault:
inFault(&types.LocalizedMethodFault{Fault: tErr}, onFaultFn)
case hasFault:
if fault := tErr.Fault(); fault != nil {
inFault(&types.LocalizedMethodFault{Fault: fault}, onFaultFn)
}
case unwrappableError:
In(tErr.Unwrap(), onFaultFn)
case unwrappableErrorSlice:
for _, uErr := range tErr.Unwrap() {
if uErr == nil {
continue
}
In(uErr, onFaultFn)
}
case error:
// No-op
default:
panic("fault: err must implement error, types.BaseMethodFault, or " +
"types.HasLocalizedMethodFault")
}
}
func inFault(
localizedMethodFault *types.LocalizedMethodFault,
onFaultFn OnFaultFn) {
if localizedMethodFault == nil {
return
}
fault := localizedMethodFault.Fault
if fault == nil {
return
}
var (
faultCause *types.LocalizedMethodFault
faultMessages []types.LocalizableMessage
)
if methodFault := fault.GetMethodFault(); methodFault != nil {
faultCause = methodFault.FaultCause
faultMessages = methodFault.FaultMessage
}
if onFaultFn(fault, localizedMethodFault.LocalizedMessage, faultMessages) {
return
}
// Check the fault's children.
inFault(faultCause, onFaultFn)
}
type hasFault interface {
Fault() types.BaseMethodFault
}
type hasAsFault interface {
AsFault(target any) (string, bool)
}
type hasIsFault interface {
IsFault(target types.BaseMethodFault) bool
}
type unwrappableError interface {
Unwrap() error
}
type unwrappableErrorSlice interface {
Unwrap() []error
}
var baseMethodFaultType = reflect.TypeOf((*types.BaseMethodFault)(nil)).Elem()