|
1 | 1 | package errors
|
2 | 2 |
|
| 3 | +import "fmt" |
| 4 | + |
| 5 | +// ErrorType is the enum representation for built-in error types |
| 6 | +type ErrorType int8 |
| 7 | + |
3 | 8 | const (
|
4 | 9 | // InternalError is the default error type
|
5 |
| - InternalError = "InternalError" |
| 10 | + InternalError ErrorType = iota |
6 | 11 | // IOError is an IO error such as file error
|
7 |
| - IOError = "IOError" |
| 12 | + IOError |
8 | 13 | // ArgumentError is for an argument-related error
|
9 |
| - ArgumentError = "ArgumentError" |
| 14 | + ArgumentError |
10 | 15 | // NameError is for a constant-related error
|
11 |
| - NameError = "NameError" |
| 16 | + NameError |
12 | 17 | // StopIteration is raised when there are no more elements in an iterator
|
13 |
| - StopIteration = "StopIteration" |
| 18 | + StopIteration |
14 | 19 | // TypeError is for a type-related error
|
15 |
| - TypeError = "TypeError" |
| 20 | + TypeError |
16 | 21 | // NoMethodError is for an intentionally unsupported-method error
|
17 |
| - NoMethodError = "NoMethodError" |
| 22 | + NoMethodError |
18 | 23 | // ConstantAlreadyInitializedError means user re-declares twice
|
19 |
| - ConstantAlreadyInitializedError = "ConstantAlreadyInitializedError" |
| 24 | + ConstantAlreadyInitializedError |
20 | 25 | // HTTPError is returned when when a request fails to return a proper response
|
21 |
| - HTTPError = "HTTPError" |
| 26 | + HTTPError |
22 | 27 | // ZeroDivisionError is for zero-division by Integer/Float/Decimal value
|
23 |
| - ZeroDivisionError = "ZeroDivisionError" |
| 28 | + ZeroDivisionError |
24 | 29 | // ChannelCloseError is for accessing to the closed channel
|
25 |
| - ChannelCloseError = "ChannelCloseError" |
| 30 | + ChannelCloseError |
26 | 31 | // NotImplementedError means the method is missing
|
27 |
| - NotImplementedError = "NotImplementedError" |
| 32 | + NotImplementedError |
| 33 | + |
| 34 | + // EndOfErrorTypeConst is an anchor for getting all error types' enum values, see AllErrorTypes |
| 35 | + EndOfErrorTypeConst |
28 | 36 | )
|
29 | 37 |
|
| 38 | +var errorTypesMap = map[ErrorType]string{ |
| 39 | + InternalError: "InternalError", |
| 40 | + IOError: "IOError", |
| 41 | + ArgumentError: "ArgumentError", |
| 42 | + NameError: "NameError", |
| 43 | + StopIteration: "StopIteration", |
| 44 | + TypeError: "TypeError", |
| 45 | + NoMethodError: "NoMethodError", |
| 46 | + ConstantAlreadyInitializedError: "ConstantAlreadyInitializedError", |
| 47 | + HTTPError: "HTTPError", |
| 48 | + ZeroDivisionError: "ZeroDivisionError", |
| 49 | + ChannelCloseError: "ChannelCloseError", |
| 50 | + NotImplementedError: "NotImplementedError", |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +// AllErrorTypes returns all error types defined in this package in their enum format. |
| 55 | +func AllErrorTypes() []ErrorType { |
| 56 | + ts := make([]ErrorType, EndOfErrorTypeConst) |
| 57 | + for i := 0; i < int(EndOfErrorTypeConst); i++ { |
| 58 | + ts[i] = ErrorType(i) |
| 59 | + } |
| 60 | + return ts |
| 61 | +} |
| 62 | + |
| 63 | +// GetErrorName receives an ErrorType enum and returns the corresponding error name. |
| 64 | +func GetErrorName(t ErrorType) string { |
| 65 | + v, ok := errorTypesMap[t] |
| 66 | + |
| 67 | + if ok { |
| 68 | + return v |
| 69 | + } |
| 70 | + |
| 71 | + panic(fmt.Errorf("expect to find ErrorType %d's name", t)) |
| 72 | +} |
| 73 | + |
30 | 74 | /*
|
31 | 75 | Here defines different error message formats for different types of errors
|
32 | 76 | */
|
|
0 commit comments