error.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. Copyright (c) 2014 VMware, Inc. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package soap
  14. import (
  15. "crypto/x509"
  16. "encoding/json"
  17. "errors"
  18. "fmt"
  19. "reflect"
  20. "strings"
  21. "github.com/vmware/govmomi/vim25/types"
  22. )
  23. type regularError struct {
  24. err error
  25. }
  26. func (r regularError) Error() string {
  27. return r.err.Error()
  28. }
  29. type soapFaultError struct {
  30. fault *Fault
  31. }
  32. func (s soapFaultError) Error() string {
  33. msg := s.fault.String
  34. if msg == "" {
  35. if s.fault.Detail.Fault == nil {
  36. msg = "unknown fault"
  37. } else {
  38. msg = reflect.TypeOf(s.fault.Detail.Fault).Name()
  39. }
  40. }
  41. return fmt.Sprintf("%s: %s", s.fault.Code, msg)
  42. }
  43. func (s soapFaultError) MarshalJSON() ([]byte, error) {
  44. out := struct {
  45. Fault *Fault
  46. }{
  47. Fault: s.fault,
  48. }
  49. return json.Marshal(out)
  50. }
  51. type vimFaultError struct {
  52. fault types.BaseMethodFault
  53. }
  54. func (v vimFaultError) Error() string {
  55. typ := reflect.TypeOf(v.fault)
  56. for typ.Kind() == reflect.Ptr {
  57. typ = typ.Elem()
  58. }
  59. return typ.Name()
  60. }
  61. func (v vimFaultError) Fault() types.BaseMethodFault {
  62. return v.fault
  63. }
  64. func Wrap(err error) error {
  65. switch err.(type) {
  66. case regularError:
  67. return err
  68. case soapFaultError:
  69. return err
  70. case vimFaultError:
  71. return err
  72. }
  73. return WrapRegularError(err)
  74. }
  75. func WrapRegularError(err error) error {
  76. return regularError{err}
  77. }
  78. func IsRegularError(err error) bool {
  79. _, ok := err.(regularError)
  80. return ok
  81. }
  82. func ToRegularError(err error) error {
  83. return err.(regularError).err
  84. }
  85. func WrapSoapFault(f *Fault) error {
  86. return soapFaultError{f}
  87. }
  88. func IsSoapFault(err error) bool {
  89. _, ok := err.(soapFaultError)
  90. return ok
  91. }
  92. func ToSoapFault(err error) *Fault {
  93. return err.(soapFaultError).fault
  94. }
  95. func WrapVimFault(v types.BaseMethodFault) error {
  96. return vimFaultError{v}
  97. }
  98. func IsVimFault(err error) bool {
  99. _, ok := err.(vimFaultError)
  100. return ok
  101. }
  102. func ToVimFault(err error) types.BaseMethodFault {
  103. return err.(vimFaultError).fault
  104. }
  105. func IsCertificateUntrusted(err error) bool {
  106. // golang 1.20 introduce a new type to wrap 509 errors. So instead of
  107. // casting the type, now we check the error chain contains the
  108. // x509 error or not.
  109. x509UnknownAuthorityErr := &x509.UnknownAuthorityError{}
  110. ok := errors.As(err, x509UnknownAuthorityErr)
  111. if ok {
  112. return true
  113. }
  114. x509HostNameErr := &x509.HostnameError{}
  115. ok = errors.As(err, x509HostNameErr)
  116. if ok {
  117. return true
  118. }
  119. // The err variable may not be a special type of x509 or HTTP
  120. // error that can be validated by a type assertion. The err variable is
  121. // in fact be an *errors.errorString.
  122. msgs := []string{
  123. "certificate is not trusted",
  124. "certificate signed by unknown authority",
  125. }
  126. for _, msg := range msgs {
  127. if strings.HasSuffix(err.Error(), msg) {
  128. return true
  129. }
  130. }
  131. return false
  132. }