~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/govmomi/vim25/soap/error.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2014 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 soap
 
18
 
 
19
import (
 
20
        "fmt"
 
21
        "reflect"
 
22
 
 
23
        "github.com/juju/govmomi/vim25/types"
 
24
)
 
25
 
 
26
type regularError struct {
 
27
        err error
 
28
}
 
29
 
 
30
func (r regularError) Error() string {
 
31
        return r.err.Error()
 
32
}
 
33
 
 
34
type soapFaultError struct {
 
35
        fault *Fault
 
36
}
 
37
 
 
38
func (s soapFaultError) Error() string {
 
39
        return fmt.Sprintf("%s: %s", s.fault.Code, s.fault.String)
 
40
}
 
41
 
 
42
type vimFaultError struct {
 
43
        fault types.BaseMethodFault
 
44
}
 
45
 
 
46
func (v vimFaultError) Error() string {
 
47
        typ := reflect.TypeOf(v.fault)
 
48
        for typ.Kind() == reflect.Ptr {
 
49
                typ = typ.Elem()
 
50
        }
 
51
 
 
52
        return typ.Name()
 
53
}
 
54
 
 
55
func (v vimFaultError) Fault() types.BaseMethodFault {
 
56
        return v.fault
 
57
}
 
58
 
 
59
func Wrap(err error) error {
 
60
        switch err.(type) {
 
61
        case regularError:
 
62
                return err
 
63
        case soapFaultError:
 
64
                return err
 
65
        case vimFaultError:
 
66
                return err
 
67
        }
 
68
 
 
69
        return WrapRegularError(err)
 
70
}
 
71
 
 
72
func WrapRegularError(err error) error {
 
73
        return regularError{err}
 
74
}
 
75
 
 
76
func IsRegularError(err error) bool {
 
77
        _, ok := err.(regularError)
 
78
        return ok
 
79
}
 
80
 
 
81
func ToRegularError(err error) error {
 
82
        return err.(regularError).err
 
83
}
 
84
 
 
85
func WrapSoapFault(f *Fault) error {
 
86
        return soapFaultError{f}
 
87
}
 
88
 
 
89
func IsSoapFault(err error) bool {
 
90
        _, ok := err.(soapFaultError)
 
91
        return ok
 
92
}
 
93
 
 
94
func ToSoapFault(err error) *Fault {
 
95
        return err.(soapFaultError).fault
 
96
}
 
97
 
 
98
func WrapVimFault(v types.BaseMethodFault) error {
 
99
        return vimFaultError{v}
 
100
}
 
101
 
 
102
func IsVimFault(err error) bool {
 
103
        _, ok := err.(vimFaultError)
 
104
        return ok
 
105
}
 
106
 
 
107
func ToVimFault(err error) types.BaseMethodFault {
 
108
        return err.(vimFaultError).fault
 
109
}