-
Notifications
You must be signed in to change notification settings - Fork 162
/
errors.go
81 lines (64 loc) · 1.65 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package cloud
import (
"fmt"
"regexp"
)
const (
VMNotFoundError = "Bosh::Clouds::VMNotFound"
DiskNotFoundError = "Bosh::Clouds::DiskNotFound"
StemcellNotFoundError = "Bosh::Clouds::StemcellNotFound"
NotImplementedError = "Bosh::Clouds::NotImplemented"
)
type Error interface {
error
Method() string
Type() string
Message() string
OkToRetry() bool
}
type cpiError struct {
method string
cmdError CmdError
}
func NewCPIError(method string, cmdError CmdError) Error {
if mapsToNotImplementedError(method, cmdError) {
cmdError = newNotImplementedCmdError(method, cmdError)
}
return cpiError{
method: method,
cmdError: cmdError,
}
}
func (e cpiError) Error() string {
return fmt.Sprintf("CPI '%s' method responded with error: %s", e.method, e.cmdError)
}
func (e cpiError) Method() string {
return e.method
}
func (e cpiError) Type() string {
return e.cmdError.Type
}
func (e cpiError) Message() string {
return e.cmdError.Message
}
func (e cpiError) OkToRetry() bool {
return e.cmdError.OkToRetry
}
func mapsToNotImplementedError(method string, cmdError CmdError) bool {
matched, _ := regexp.MatchString("^Invalid Method:", cmdError.Message)
if cmdError.Type == "Bosh::Clouds::CloudError" && matched {
return true
}
matched, _ = regexp.MatchString("^Method is not known, got", cmdError.Message)
if cmdError.Type == "InvalidCall" && matched {
return true
}
return false
}
func newNotImplementedCmdError(method string, cmdError CmdError) CmdError {
return CmdError{
NotImplementedError,
fmt.Sprintf("CPI error '%s' with message '%s' in '%s' CPI method", cmdError.Type, cmdError.Message, method),
cmdError.OkToRetry,
}
}