...
1
2
3 package hcn
4
5 import (
6 "errors"
7 "fmt"
8
9 "github.com/Microsoft/hcsshim/internal/hcs"
10 "github.com/Microsoft/hcsshim/internal/hcserror"
11 "github.com/Microsoft/hcsshim/internal/interop"
12 "github.com/sirupsen/logrus"
13 "golang.org/x/sys/windows"
14 )
15
16 var (
17 errInvalidNetworkID = errors.New("invalid network ID")
18 errInvalidEndpointID = errors.New("invalid endpoint ID")
19 errInvalidNamespaceID = errors.New("invalid namespace ID")
20 errInvalidLoadBalancerID = errors.New("invalid load balancer ID")
21 errInvalidRouteID = errors.New("invalid route ID")
22 )
23
24 func checkForErrors(methodName string, hr error, resultBuffer *uint16) error {
25 errorFound := false
26
27 if hr != nil {
28 errorFound = true
29 }
30
31 result := ""
32 if resultBuffer != nil {
33 result = interop.ConvertAndFreeCoTaskMemString(resultBuffer)
34 if result != "" {
35 errorFound = true
36 }
37 }
38
39 if errorFound {
40 returnError := new(hr, methodName, result)
41 logrus.Debugf(returnError.Error())
42 return returnError
43 }
44
45 return nil
46 }
47
48 type ErrorCode uint32
49
50
51 const (
52 ERROR_NOT_FOUND = ErrorCode(windows.ERROR_NOT_FOUND)
53 HCN_E_PORT_ALREADY_EXISTS ErrorCode = ErrorCode(windows.HCN_E_PORT_ALREADY_EXISTS)
54 )
55
56 type HcnError struct {
57 *hcserror.HcsError
58 code ErrorCode
59 }
60
61 func (e *HcnError) Error() string {
62 return e.HcsError.Error()
63 }
64
65 func CheckErrorWithCode(err error, code ErrorCode) bool {
66 hcnError, ok := err.(*HcnError)
67 if ok {
68 return hcnError.code == code
69 }
70 return false
71 }
72
73 func IsElementNotFoundError(err error) bool {
74 return CheckErrorWithCode(err, ERROR_NOT_FOUND)
75 }
76
77 func IsPortAlreadyExistsError(err error) bool {
78 return CheckErrorWithCode(err, HCN_E_PORT_ALREADY_EXISTS)
79 }
80
81 func new(hr error, title string, rest string) error {
82 err := &HcnError{}
83 hcsError := hcserror.New(hr, title, rest)
84 err.HcsError = hcsError.(*hcserror.HcsError)
85 err.code = ErrorCode(hcserror.Win32FromError(hr))
86 return err
87 }
88
89
90
91
92
93
94
95 type NetworkNotFoundError struct {
96 NetworkName string
97 NetworkID string
98 }
99
100 func (e NetworkNotFoundError) Error() string {
101 if e.NetworkName != "" {
102 return fmt.Sprintf("Network name %q not found", e.NetworkName)
103 }
104 return fmt.Sprintf("Network ID %q not found", e.NetworkID)
105 }
106
107
108 type EndpointNotFoundError struct {
109 EndpointName string
110 EndpointID string
111 }
112
113 func (e EndpointNotFoundError) Error() string {
114 if e.EndpointName != "" {
115 return fmt.Sprintf("Endpoint name %q not found", e.EndpointName)
116 }
117 return fmt.Sprintf("Endpoint ID %q not found", e.EndpointID)
118 }
119
120
121 type NamespaceNotFoundError struct {
122 NamespaceID string
123 }
124
125 func (e NamespaceNotFoundError) Error() string {
126 return fmt.Sprintf("Namespace ID %q not found", e.NamespaceID)
127 }
128
129
130 type LoadBalancerNotFoundError struct {
131 LoadBalancerId string
132 }
133
134 func (e LoadBalancerNotFoundError) Error() string {
135 return fmt.Sprintf("LoadBalancer %q not found", e.LoadBalancerId)
136 }
137
138
139 type RouteNotFoundError struct {
140 RouteId string
141 }
142
143 func (e RouteNotFoundError) Error() string {
144 return fmt.Sprintf("SDN Route %q not found", e.RouteId)
145 }
146
147
148
149 func IsNotFoundError(err error) bool {
150 switch pe := err.(type) {
151 case NetworkNotFoundError:
152 return true
153 case EndpointNotFoundError:
154 return true
155 case NamespaceNotFoundError:
156 return true
157 case LoadBalancerNotFoundError:
158 return true
159 case RouteNotFoundError:
160 return true
161 case *hcserror.HcsError:
162 return pe.Err == hcs.ErrElementNotFound
163 }
164 return false
165 }
166
View as plain text