|
| 1 | +/* |
| 2 | +Copyright 2019 Cortex Labs, Inc. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package k8s |
| 18 | + |
| 19 | +type ErrorKind int |
| 20 | + |
| 21 | +const ( |
| 22 | + ErrUnknown ErrorKind = iota |
| 23 | + ErrParseQuantity ErrorKind = iota |
| 24 | +) |
| 25 | + |
| 26 | +var errorKinds = []string{ |
| 27 | + "err_unknown", |
| 28 | + "err_parse_quantity", |
| 29 | +} |
| 30 | + |
| 31 | +var _ = [1]int{}[int(ErrParseQuantity)-(len(errorKinds)-1)] // Ensure list length matches |
| 32 | + |
| 33 | +func (t ErrorKind) String() string { |
| 34 | + return errorKinds[t] |
| 35 | +} |
| 36 | + |
| 37 | +// MarshalText satisfies TextMarshaler |
| 38 | +func (t ErrorKind) MarshalText() ([]byte, error) { |
| 39 | + return []byte(t.String()), nil |
| 40 | +} |
| 41 | + |
| 42 | +// UnmarshalText satisfies TextUnmarshaler |
| 43 | +func (t *ErrorKind) UnmarshalText(text []byte) error { |
| 44 | + enum := string(text) |
| 45 | + for i := 0; i < len(errorKinds); i++ { |
| 46 | + if enum == errorKinds[i] { |
| 47 | + *t = ErrorKind(i) |
| 48 | + return nil |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + *t = ErrUnknown |
| 53 | + return nil |
| 54 | +} |
| 55 | + |
| 56 | +// UnmarshalBinary satisfies BinaryUnmarshaler |
| 57 | +// Needed for msgpack |
| 58 | +func (t *ErrorKind) UnmarshalBinary(data []byte) error { |
| 59 | + return t.UnmarshalText(data) |
| 60 | +} |
| 61 | + |
| 62 | +// MarshalBinary satisfies BinaryMarshaler |
| 63 | +func (t ErrorKind) MarshalBinary() ([]byte, error) { |
| 64 | + return []byte(t.String()), nil |
| 65 | +} |
| 66 | + |
| 67 | +type Error struct { |
| 68 | + Kind ErrorKind |
| 69 | + message string |
| 70 | +} |
| 71 | + |
| 72 | +func (e Error) Error() string { |
| 73 | + return e.message |
| 74 | +} |
| 75 | + |
| 76 | +func ErrorParseQuantity(qtyStr string) error { |
| 77 | + return Error{ |
| 78 | + Kind: ErrParseQuantity, |
| 79 | + message: qtyStr + ": invalid kubernetes quantity, some valid examples are 1, 200m, 500Mi, 2G (see here for more information: https://docs.cortex.dev/deployments/compute)", |
| 80 | + } |
| 81 | +} |
0 commit comments