-
Notifications
You must be signed in to change notification settings - Fork 56
/
errors.go
152 lines (125 loc) · 3.02 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package garden
import (
"encoding/json"
"errors"
"net/http"
)
type errType string
const (
unrecoverableErrType = "UnrecoverableError"
serviceUnavailableErrType = "ServiceUnavailableError"
containerNotFoundErrType = "ContainerNotFoundError"
processNotFoundErrType = "ProcessNotFoundError"
executableNotFoundError = "ExecutableNotFoundError"
)
type Error struct {
Err error
}
func NewError(err string) *Error {
return &Error{Err: errors.New(err)}
}
type marshalledError struct {
Type errType
Message string
Handle string
ProcessID string
Binary string
}
func (m Error) Error() string {
return m.Err.Error()
}
func (m Error) StatusCode() int {
switch m.Err.(type) {
case ContainerNotFoundError:
return http.StatusNotFound
case ProcessNotFoundError:
return http.StatusNotFound
}
return http.StatusInternalServerError
}
func (m Error) MarshalJSON() ([]byte, error) {
var errorType errType
handle := ""
processID := ""
switch err := m.Err.(type) {
case ContainerNotFoundError:
errorType = containerNotFoundErrType
handle = err.Handle
case ProcessNotFoundError:
errorType = processNotFoundErrType
processID = err.ProcessID
case ExecutableNotFoundError:
errorType = executableNotFoundError
case ServiceUnavailableError:
errorType = serviceUnavailableErrType
case UnrecoverableError:
errorType = unrecoverableErrType
}
return json.Marshal(marshalledError{
Type: errorType,
Message: m.Err.Error(),
Handle: handle,
ProcessID: processID,
})
}
func (m *Error) UnmarshalJSON(data []byte) error {
var result marshalledError
if err := json.Unmarshal(data, &result); err != nil {
return err
}
switch result.Type {
case unrecoverableErrType:
m.Err = UnrecoverableError{result.Message}
case serviceUnavailableErrType:
m.Err = ServiceUnavailableError{result.Message}
case containerNotFoundErrType:
m.Err = ContainerNotFoundError{result.Handle}
case processNotFoundErrType:
m.Err = ProcessNotFoundError{ProcessID: result.ProcessID}
case executableNotFoundError:
m.Err = ExecutableNotFoundError{Message: result.Message}
default:
m.Err = errors.New(result.Message)
}
return nil
}
func NewUnrecoverableError(symptom string) error {
return UnrecoverableError{
Symptom: symptom,
}
}
type UnrecoverableError struct {
Symptom string
}
func (err UnrecoverableError) Error() string {
return err.Symptom
}
type ContainerNotFoundError struct {
Handle string
}
func (err ContainerNotFoundError) Error() string {
return "unknown handle: " + err.Handle
}
func NewServiceUnavailableError(cause string) error {
return ServiceUnavailableError{
Cause: cause,
}
}
type ServiceUnavailableError struct {
Cause string
}
func (err ServiceUnavailableError) Error() string {
return err.Cause
}
type ProcessNotFoundError struct {
ProcessID string
}
func (err ProcessNotFoundError) Error() string {
return "unknown process: " + err.ProcessID
}
type ExecutableNotFoundError struct {
Message string
}
func (err ExecutableNotFoundError) Error() string {
return err.Message
}