|
| 1 | +/* |
| 2 | + Copyright 2024 The CloudEvents Authors |
| 3 | + SPDX-License-Identifier: Apache-2.0 |
| 4 | +*/ |
| 5 | + |
| 6 | +package errors |
| 7 | + |
| 8 | +import ( |
| 9 | + "fmt" |
| 10 | + "strings" |
| 11 | +) |
| 12 | + |
| 13 | +type errorKind int |
| 14 | + |
| 15 | +const ( |
| 16 | + parseError = iota |
| 17 | + mathError |
| 18 | + castError |
| 19 | + missingAttributeError |
| 20 | + missingFunctionError |
| 21 | + functionEvaluationError |
| 22 | + dummyLastError // always add new error classes ABOVE this error |
| 23 | +) |
| 24 | + |
| 25 | +type cesqlError struct { |
| 26 | + kind errorKind |
| 27 | + message string |
| 28 | +} |
| 29 | + |
| 30 | +func (eKind errorKind) String() string { |
| 31 | + switch eKind { |
| 32 | + case parseError: |
| 33 | + return "parse error" |
| 34 | + case mathError: |
| 35 | + return "math error" |
| 36 | + case castError: |
| 37 | + return "cast error" |
| 38 | + case missingAttributeError: |
| 39 | + return "missing attribute error" |
| 40 | + case missingFunctionError: |
| 41 | + return "missing function error" |
| 42 | + case functionEvaluationError: |
| 43 | + return "function evaluation error" |
| 44 | + default: |
| 45 | + return "generic error" |
| 46 | + } |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | +func (cerr cesqlError) Error() string { |
| 51 | + return fmt.Sprintf("%s: %s", cerr.kind, cerr.message) |
| 52 | +} |
| 53 | + |
| 54 | +func NewParseError(errs []error) error { |
| 55 | + if len(errs) == 0 { |
| 56 | + return nil |
| 57 | + } |
| 58 | + |
| 59 | + errorMessages := make([]string, 0, len(errs)) |
| 60 | + for _, err := range errs { |
| 61 | + errorMessages = append(errorMessages, err.Error()) |
| 62 | + } |
| 63 | + |
| 64 | + return cesqlError{ |
| 65 | + kind: parseError, |
| 66 | + message: strings.Join(errorMessages, "|"), |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func IsParseError(err error) bool { |
| 71 | + if cesqlErr, ok := err.(cesqlError); ok { |
| 72 | + return cesqlErr.kind == parseError |
| 73 | + } |
| 74 | + return false |
| 75 | +} |
| 76 | + |
| 77 | +func IsMathError(err error) bool { |
| 78 | + if cesqlErr, ok := err.(cesqlError); ok { |
| 79 | + return cesqlErr.kind == mathError |
| 80 | + } |
| 81 | + return false |
| 82 | +} |
| 83 | + |
| 84 | +func NewMathError(message string) error { |
| 85 | + return cesqlError{ |
| 86 | + kind: mathError, |
| 87 | + message: message, |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func IsCastError(err error) bool { |
| 92 | + if cesqlErr, ok := err.(cesqlError); ok { |
| 93 | + return cesqlErr.kind == castError |
| 94 | + } |
| 95 | + return false |
| 96 | +} |
| 97 | + |
| 98 | +func NewCastError(err error) error { |
| 99 | + return cesqlError{ |
| 100 | + kind: castError, |
| 101 | + message: err.Error(), |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func IsMissingAttributeError(err error) bool { |
| 106 | + if cesqlErr, ok := err.(cesqlError); ok { |
| 107 | + return cesqlErr.kind == missingAttributeError |
| 108 | + } |
| 109 | + return false |
| 110 | +} |
| 111 | + |
| 112 | +func NewMissingAttributeError(attribute string) error { |
| 113 | + return cesqlError{ |
| 114 | + kind: missingAttributeError, |
| 115 | + message: attribute, |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func IsMissingFunctionError(err error) bool { |
| 120 | + if cesqlErr, ok := err.(cesqlError); ok { |
| 121 | + return cesqlErr.kind == missingFunctionError |
| 122 | + } |
| 123 | + return false |
| 124 | +} |
| 125 | + |
| 126 | +func NewMissingFunctionError(function string) error { |
| 127 | + return cesqlError{ |
| 128 | + kind: missingFunctionError, |
| 129 | + message: function, |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func IsFunctionEvaluationError(err error) bool { |
| 134 | + if cesqlErr, ok := err.(cesqlError); ok { |
| 135 | + return cesqlErr.kind == functionEvaluationError |
| 136 | + } |
| 137 | + return false |
| 138 | +} |
| 139 | + |
| 140 | +func NewFunctionEvaluationError(err error) error { |
| 141 | + return cesqlError{ |
| 142 | + kind: functionEvaluationError, |
| 143 | + message: err.Error(), |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func IsGenericError(err error) bool { |
| 148 | + if cesqlErr, ok := err.(cesqlError); ok { |
| 149 | + return cesqlErr.kind < 0 || cesqlErr.kind >= dummyLastError |
| 150 | + } |
| 151 | + return false |
| 152 | +} |
0 commit comments