|
1 |
| -use crate::messages::ExitReason; |
| 1 | +use crate::{messages::ExitReason, prelude::*}; |
2 | 2 |
|
3 |
| -#[derive(Debug, PartialEq)] |
| 3 | +#[derive(Debug, PartialEq, Copy, Clone)] |
4 | 4 | pub enum LambdaRequestError {
|
5 | 5 | RouterLambdaInvokeInvalid,
|
6 | 6 | RouterUnreachable,
|
@@ -64,53 +64,22 @@ impl From<&LambdaRequestError> for ExitReason {
|
64 | 64 |
|
65 | 65 | impl LambdaRequestError {
|
66 | 66 | pub fn is_fatal(&self) -> bool {
|
67 |
| - use LambdaRequestError::*; |
68 |
| - |
69 |
| - let fatal_errors = [ |
70 |
| - // Add other fatal errors here |
71 |
| - AppConnectionUnreachable, |
72 |
| - ]; |
73 |
| - |
74 |
| - fatal_errors.contains(self) |
75 |
| - } |
76 |
| - |
77 |
| - pub fn worse(self, other: Self) -> Self { |
78 |
| - use LambdaRequestError::*; |
79 |
| - |
80 |
| - let ordered_reasons = [ |
81 |
| - RouterLambdaInvokeInvalid, |
82 |
| - RouterUnreachable, |
83 |
| - RouterConnectionError, |
84 |
| - AppConnectionUnreachable, |
85 |
| - AppConnectionError, |
86 |
| - ChannelErrorOther, |
87 |
| - ]; |
88 |
| - |
89 |
| - for reason in ordered_reasons { |
90 |
| - if self == reason || other == reason { |
91 |
| - return reason; |
92 |
| - } |
93 |
| - } |
94 |
| - |
95 |
| - self // If no match is found, return the current value |
| 67 | + // Add other fatal errors to the match pattern. For example: |
| 68 | + // matches!(self, Self::Variant1 | Self::Variant2) |
| 69 | + matches!(self, Self::AppConnectionUnreachable) |
96 | 70 | }
|
| 71 | +} |
97 | 72 |
|
98 |
| - // This function will cause a compile-time error if a new variant is added to the enum |
99 |
| - // but not added to the match expression. |
100 |
| - #[allow(dead_code)] |
101 |
| - fn ensure_all_variants_handled(variant: Self) { |
102 |
| - use LambdaRequestError::*; |
103 |
| - |
104 |
| - match variant { |
105 |
| - // HEY - If you add here you need to add to the `worse` function array above |
106 |
| - RouterLambdaInvokeInvalid => {} |
107 |
| - RouterUnreachable => {} |
108 |
| - RouterConnectionError => {} |
109 |
| - AppConnectionUnreachable => {} |
110 |
| - AppConnectionError => {} |
111 |
| - ChannelErrorOther => {} |
| 73 | +impl Severity for LambdaRequestError { |
| 74 | + fn severity(&self) -> usize { |
| 75 | + match self { |
| 76 | + Self::RouterLambdaInvokeInvalid => 0, |
| 77 | + Self::RouterUnreachable => 1, |
| 78 | + Self::RouterConnectionError => 2, |
| 79 | + Self::AppConnectionUnreachable => 3, |
| 80 | + Self::AppConnectionError => 4, |
| 81 | + Self::ChannelErrorOther => 5, |
112 | 82 | }
|
113 |
| - // HEY - If you add here you need to add to the `worse` function array above |
114 | 83 | }
|
115 | 84 | }
|
116 | 85 |
|
@@ -166,46 +135,46 @@ mod tests {
|
166 | 135 |
|
167 | 136 | #[test]
|
168 | 137 | fn test_worse() {
|
169 |
| - use LambdaRequestError::*; |
| 138 | + use LambdaRequestError as E; |
170 | 139 |
|
171 | 140 | // Test that RouterUnreachable is considered worse than AppConnectionError
|
172 |
| - let error1 = RouterUnreachable; |
173 |
| - let error2 = AppConnectionError; |
174 |
| - assert_eq!(error1.worse(error2), RouterUnreachable); |
| 141 | + let error1 = E::RouterUnreachable; |
| 142 | + let error2 = E::AppConnectionError; |
| 143 | + assert_eq!(error1.worse(error2), E::RouterUnreachable); |
175 | 144 |
|
176 | 145 | // Test that AppConnectionError is considered worse than ChannelErrorOther
|
177 |
| - let error1 = AppConnectionError; |
178 |
| - let error2 = ChannelErrorOther; |
179 |
| - assert_eq!(error1.worse(error2), AppConnectionError); |
| 146 | + let error1 = E::AppConnectionError; |
| 147 | + let error2 = E::ChannelErrorOther; |
| 148 | + assert_eq!(error1.worse(error2), E::AppConnectionError); |
180 | 149 |
|
181 | 150 | // Test that when both errors are the same, that error is returned
|
182 |
| - let error1 = ChannelErrorOther; |
183 |
| - let error2 = ChannelErrorOther; |
184 |
| - assert_eq!(error1.worse(error2), ChannelErrorOther); |
| 151 | + let error1 = E::ChannelErrorOther; |
| 152 | + let error2 = E::ChannelErrorOther; |
| 153 | + assert_eq!(error1.worse(error2), E::ChannelErrorOther); |
185 | 154 | }
|
186 | 155 |
|
187 | 156 | #[test]
|
188 | 157 | fn test_is_fatal() {
|
189 |
| - use LambdaRequestError::*; |
| 158 | + use LambdaRequestError as E; |
190 | 159 |
|
191 | 160 | // Test that AppConnectionUnreachable is considered fatal
|
192 |
| - let error = AppConnectionUnreachable; |
| 161 | + let error = E::AppConnectionUnreachable; |
193 | 162 | assert!(error.is_fatal());
|
194 | 163 |
|
195 | 164 | // Test that RouterUnreachable is not considered fatal
|
196 |
| - let error = RouterUnreachable; |
| 165 | + let error = E::RouterUnreachable; |
197 | 166 | assert!(!error.is_fatal());
|
198 | 167 |
|
199 | 168 | // Test that RouterConnectionError is not considered fatal
|
200 |
| - let error = RouterConnectionError; |
| 169 | + let error = E::RouterConnectionError; |
201 | 170 | assert!(!error.is_fatal());
|
202 | 171 |
|
203 | 172 | // Test that AppConnectionError is not considered fatal
|
204 |
| - let error = AppConnectionError; |
| 173 | + let error = E::AppConnectionError; |
205 | 174 | assert!(!error.is_fatal());
|
206 | 175 |
|
207 | 176 | // Test that ChannelErrorOther is not considered fatal
|
208 |
| - let error = ChannelErrorOther; |
| 177 | + let error = E::ChannelErrorOther; |
209 | 178 | assert!(!error.is_fatal());
|
210 | 179 | }
|
211 | 180 | }
|
0 commit comments