@@ -21,6 +21,7 @@ package v1alpha1
21
21
import (
22
22
"context"
23
23
"errors"
24
+ "fmt"
24
25
"math"
25
26
"strings"
26
27
"time"
@@ -92,12 +93,14 @@ const (
92
93
MaxTotalScore int64 = math .MaxInt64
93
94
)
94
95
95
- // Status indicates the result of running a plugin. It consists of a code and a
96
- // message. When the status code is not `Success`, the reasons should explain why.
96
+ // Status indicates the result of running a plugin. It consists of a code, a
97
+ // message and (optionally) an error. When the status code is not `Success`,
98
+ // the reasons should explain why.
97
99
// NOTE: A nil Status is also considered as Success.
98
100
type Status struct {
99
101
code Code
100
102
reasons []string
103
+ err error
101
104
}
102
105
103
106
// Code returns code of the Status.
@@ -143,15 +146,31 @@ func (s *Status) AsError() error {
143
146
if s .IsSuccess () {
144
147
return nil
145
148
}
146
- return errors .New (s .Message ())
149
+ if s .err != nil {
150
+ return s .err
151
+ }
152
+ return fmt .Errorf ("%s: %s" , s .code .String (), s .Message ())
147
153
}
148
154
149
155
// NewStatus makes a Status out of the given arguments and returns its pointer.
150
156
func NewStatus (code Code , reasons ... string ) * Status {
151
- return & Status {
157
+ s := & Status {
152
158
code : code ,
153
159
reasons : reasons ,
154
160
}
161
+ if code == Error {
162
+ s .err = errors .New (s .Message ())
163
+ }
164
+ return s
165
+ }
166
+
167
+ // AsStatus wraps an error in a Status.
168
+ func AsStatus (err error ) * Status {
169
+ return & Status {
170
+ code : Error ,
171
+ reasons : []string {err .Error ()},
172
+ err : err ,
173
+ }
155
174
}
156
175
157
176
// PluginToStatus maps plugin name to status. Currently used to identify which Filter plugin
@@ -166,10 +185,10 @@ func (p PluginToStatus) Merge() *Status {
166
185
}
167
186
168
187
finalStatus := NewStatus (Success )
169
- var hasError , hasUnschedulableAndUnresolvable , hasUnschedulable bool
188
+ var hasUnschedulableAndUnresolvable , hasUnschedulable bool
170
189
for _ , s := range p {
171
190
if s .Code () == Error {
172
- hasError = true
191
+ finalStatus . err = s . AsError ()
173
192
} else if s .Code () == UnschedulableAndUnresolvable {
174
193
hasUnschedulableAndUnresolvable = true
175
194
} else if s .Code () == Unschedulable {
@@ -181,7 +200,7 @@ func (p PluginToStatus) Merge() *Status {
181
200
}
182
201
}
183
202
184
- if hasError {
203
+ if finalStatus . err != nil {
185
204
finalStatus .code = Error
186
205
} else if hasUnschedulableAndUnresolvable {
187
206
finalStatus .code = UnschedulableAndUnresolvable
0 commit comments