forked from cloudfoundry/bbs
/
errors.go
186 lines (152 loc) · 3.85 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package models
import (
"encoding/json"
"errors"
"fmt"
)
func NewError(errType Error_Type, msg string) *Error {
return &Error{
Type: errType,
Message: msg,
}
}
func ConvertError(err error) *Error {
if err == nil {
return nil
}
modelErr, ok := err.(*Error)
if !ok {
modelErr = NewError(Error_UnknownError, err.Error())
}
return modelErr
}
func (err *Error) ToError() error {
if err == nil {
return nil
}
return err
}
func (err *Error) Equal(other error) bool {
if e, ok := other.(*Error); ok {
if err == nil && e != nil {
return false
}
return e.GetType() == err.GetType()
}
return false
}
func (err *Error) Error() string {
return err.GetMessage()
}
func (d *Error_Type) UnmarshalJSON(data []byte) error {
var name string
if err := json.Unmarshal(data, &name); err != nil {
return err
}
if v, found := Error_Type_value[name]; found {
*d = Error_Type(v)
return nil
}
return fmt.Errorf("invalid presence: %s", name)
}
func (d Error_Type) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
}
var (
ErrResourceNotFound = &Error{
Type: Error_ResourceNotFound,
Message: "the requested resource could not be found",
}
ErrResourceExists = &Error{
Type: Error_ResourceExists,
Message: "the requested resource already exists",
}
ErrResourceConflict = &Error{
Type: Error_ResourceConflict,
Message: "the requested resource is in a conflicting state",
}
ErrDeadlock = &Error{
Type: Error_Deadlock,
Message: "the request failed due to deadlock",
}
ErrBadRequest = &Error{
Type: Error_InvalidRequest,
Message: "the request received is invalid",
}
ErrUnknownError = &Error{
Type: Error_UnknownError,
Message: "the request failed for an unknown reason",
}
ErrDeserialize = &Error{
Type: Error_Deserialize,
Message: "could not deserialize record",
}
ErrFailedToOpenEnvelope = &Error{
Type: Error_FailedToOpenEnvelope,
Message: "could not open envelope",
}
ErrActualLRPCannotBeClaimed = &Error{
Type: Error_ActualLRPCannotBeClaimed,
Message: "cannot claim actual LRP",
}
ErrActualLRPCannotBeStarted = &Error{
Type: Error_ActualLRPCannotBeStarted,
Message: "cannot start actual LRP",
}
ErrActualLRPCannotBeCrashed = &Error{
Type: Error_ActualLRPCannotBeCrashed,
Message: "cannot crash actual LRP",
}
ErrActualLRPCannotBeFailed = &Error{
Type: Error_ActualLRPCannotBeFailed,
Message: "cannot fail actual LRP",
}
ErrActualLRPCannotBeRemoved = &Error{
Type: Error_ActualLRPCannotBeRemoved,
Message: "cannot remove actual LRP",
}
ErrActualLRPCannotBeUnclaimed = &Error{
Type: Error_ActualLRPCannotBeUnclaimed,
Message: "cannot unclaim actual LRP",
}
ErrGUIDGeneration = &Error{
Type: Error_GUIDGeneration,
Message: "cannot generate random guid",
}
ErrLockCollision = &Error{
Type: Error_LockCollision,
Message: "lock already exists",
}
)
type ErrInvalidField struct {
Field string
}
func (err ErrInvalidField) Error() string {
return "Invalid field: " + err.Field
}
type ErrInvalidModification struct {
InvalidField string
}
func (err ErrInvalidModification) Error() string {
return "attempt to make invalid change to field: " + err.InvalidField
}
// DEPRECATED
var ErrActualLRPGroupInvalid = errors.New("ActualLRPGroup invalid")
func NewTaskTransitionError(from, to Task_State) *Error {
return &Error{
Type: Error_InvalidStateTransition,
Message: fmt.Sprintf("Cannot transition from %s to %s", from.String(), to.String()),
}
}
func NewRunningOnDifferentCellError(expectedCellId, actualCellId string) *Error {
return &Error{
Type: Error_RunningOnDifferentCell,
Message: fmt.Sprintf("Running on cell %s not %s", actualCellId, expectedCellId),
}
}
func NewUnrecoverableError(err error) *Error {
return &Error{
Type: Error_Unrecoverable,
Message: fmt.Sprint("Unrecoverable Error: ", err),
}
}