/build/source/nativelink-error/src/lib.rs
Line | Count | Source |
1 | | // Copyright 2024 The NativeLink Authors. All rights reserved. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | use core::convert::Into; |
16 | | |
17 | | use nativelink_metric::{ |
18 | | MetricFieldData, MetricKind, MetricPublishKnownKindData, MetricsComponent, |
19 | | }; |
20 | | use prost_types::TimestampError; |
21 | | use serde::{Deserialize, Serialize}; |
22 | | // Reexport of tonic's error codes which we use as "nativelink_error::Code". |
23 | | pub use tonic::Code; |
24 | | |
25 | | #[macro_export] |
26 | | macro_rules! make_err { |
27 | | ($code:expr, $($arg:tt)+) => {{ |
28 | | $crate::Error::new( |
29 | | $code, |
30 | | format!("{}", format_args!($($arg)+)), |
31 | | ) |
32 | | }}; |
33 | | } |
34 | | |
35 | | #[macro_export] |
36 | | macro_rules! make_input_err { |
37 | | ($($arg:tt)+) => {{ |
38 | | $crate::make_err!($crate::Code::InvalidArgument, $($arg)+) |
39 | | }}; |
40 | | } |
41 | | |
42 | | #[macro_export] |
43 | | macro_rules! error_if { |
44 | | ($cond:expr, $($arg:tt)+) => {{ |
45 | | if $cond { |
46 | | Err($crate::make_err!($crate::Code::InvalidArgument, $($arg)+))?; |
47 | | } |
48 | | }}; |
49 | | } |
50 | | |
51 | | #[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)] |
52 | | pub struct Error { |
53 | | #[serde(with = "CodeDef")] |
54 | | pub code: Code, |
55 | | pub messages: Vec<String>, |
56 | | } |
57 | | |
58 | | impl MetricsComponent for Error { |
59 | 0 | fn publish( |
60 | 0 | &self, |
61 | 0 | kind: MetricKind, |
62 | 0 | field_metadata: MetricFieldData, |
63 | 0 | ) -> Result<MetricPublishKnownKindData, nativelink_metric::Error> { |
64 | 0 | self.to_string().publish(kind, field_metadata) |
65 | 0 | } |
66 | | } |
67 | | |
68 | | impl Error { |
69 | | #[must_use] |
70 | 146 | pub const fn new_with_messages(code: Code, messages: Vec<String>) -> Self { |
71 | 146 | Self { code, messages } |
72 | 146 | } |
73 | | |
74 | | #[must_use] |
75 | 145 | pub fn new(code: Code, msg: String) -> Self { |
76 | 145 | if msg.is_empty() { Branch (76:12): [True: 0, False: 145]
Branch (76:12): [Folded - Ignored]
|
77 | 0 | Self::new_with_messages(code, vec![]) |
78 | | } else { |
79 | 145 | Self::new_with_messages(code, vec![msg]) |
80 | | } |
81 | 145 | } |
82 | | |
83 | | #[inline] |
84 | | #[must_use] |
85 | 3 | pub fn append<S: Into<String>>(mut self, msg: S) -> Self { |
86 | 3 | self.messages.push(msg.into()); |
87 | 3 | self |
88 | 3 | } |
89 | | |
90 | | #[must_use] |
91 | 2 | pub fn merge<E: Into<Self>>(mut self, other: E) -> Self { |
92 | 2 | let mut other: Self = other.into(); |
93 | | // This will help with knowing which messages are tied to different errors. |
94 | 2 | self.messages.push("---".to_string()); |
95 | 2 | self.messages.append(&mut other.messages); |
96 | 2 | self |
97 | 2 | } |
98 | | |
99 | | #[must_use] |
100 | 17 | pub fn merge_option<T: Into<Self>, U: Into<Self>>( |
101 | 17 | this: Option<T>, |
102 | 17 | other: Option<U>, |
103 | 17 | ) -> Option<Self> { |
104 | 17 | if let Some(this4 ) = this { Branch (104:16): [Folded - Ignored]
Branch (104:16): [Folded - Ignored]
Branch (104:16): [True: 4, False: 13]
|
105 | 4 | if let Some(other0 ) = other { Branch (105:20): [Folded - Ignored]
Branch (105:20): [Folded - Ignored]
Branch (105:20): [True: 0, False: 4]
|
106 | 0 | return Some(this.into().merge(other)); |
107 | 4 | } |
108 | 4 | return Some(this.into()); |
109 | 13 | } |
110 | 13 | other.map(Into::into) |
111 | 17 | } |
112 | | |
113 | | #[must_use] |
114 | 1 | pub fn to_std_err(self) -> std::io::Error { |
115 | 1 | std::io::Error::new(self.code.into_error_kind(), self.messages.join(" : ")) |
116 | 1 | } |
117 | | |
118 | | #[must_use] |
119 | 6 | pub fn message_string(&self) -> String { |
120 | 6 | self.messages.join(" : ") |
121 | 6 | } |
122 | | } |
123 | | |
124 | | impl core::error::Error for Error {} |
125 | | |
126 | | impl From<Error> for nativelink_proto::google::rpc::Status { |
127 | 6 | fn from(val: Error) -> Self { |
128 | 6 | Self { |
129 | 6 | code: val.code as i32, |
130 | 6 | message: val.message_string(), |
131 | 6 | details: vec![], |
132 | 6 | } |
133 | 6 | } |
134 | | } |
135 | | |
136 | | impl From<nativelink_proto::google::rpc::Status> for Error { |
137 | 2 | fn from(val: nativelink_proto::google::rpc::Status) -> Self { |
138 | 2 | Self { |
139 | 2 | code: val.code.into(), |
140 | 2 | messages: vec![val.message], |
141 | 2 | } |
142 | 2 | } |
143 | | } |
144 | | |
145 | | impl core::fmt::Display for Error { |
146 | 15 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
147 | | // A manual impl to reduce the noise of frequently empty fields. |
148 | 15 | let mut builder = f.debug_struct("Error"); |
149 | | |
150 | 15 | builder.field("code", &self.code); |
151 | | |
152 | 15 | if !self.messages.is_empty() { Branch (152:12): [True: 15, False: 0]
Branch (152:12): [Folded - Ignored]
|
153 | 15 | builder.field("messages", &self.messages); |
154 | 15 | }0 |
155 | | |
156 | 15 | builder.finish() |
157 | 15 | } |
158 | | } |
159 | | |
160 | | impl From<prost::DecodeError> for Error { |
161 | 0 | fn from(err: prost::DecodeError) -> Self { |
162 | 0 | make_err!(Code::Internal, "{}", err.to_string()) |
163 | 0 | } |
164 | | } |
165 | | |
166 | | impl From<prost::EncodeError> for Error { |
167 | 0 | fn from(err: prost::EncodeError) -> Self { |
168 | 0 | make_err!(Code::Internal, "{}", err.to_string()) |
169 | 0 | } |
170 | | } |
171 | | |
172 | | impl From<prost::UnknownEnumValue> for Error { |
173 | 0 | fn from(err: prost::UnknownEnumValue) -> Self { |
174 | 0 | make_err!(Code::Internal, "{}", err.to_string()) |
175 | 0 | } |
176 | | } |
177 | | |
178 | | impl From<core::num::TryFromIntError> for Error { |
179 | 0 | fn from(err: core::num::TryFromIntError) -> Self { |
180 | 0 | make_err!(Code::InvalidArgument, "{}", err.to_string()) |
181 | 0 | } |
182 | | } |
183 | | |
184 | | impl From<tokio::task::JoinError> for Error { |
185 | 0 | fn from(err: tokio::task::JoinError) -> Self { |
186 | 0 | make_err!(Code::Internal, "{}", err.to_string()) |
187 | 0 | } |
188 | | } |
189 | | |
190 | | impl From<serde_json5::Error> for Error { |
191 | 0 | fn from(err: serde_json5::Error) -> Self { |
192 | 0 | make_err!(Code::Internal, "{}", err.to_string()) |
193 | 0 | } |
194 | | } |
195 | | |
196 | | impl From<core::num::ParseIntError> for Error { |
197 | 0 | fn from(err: core::num::ParseIntError) -> Self { |
198 | 0 | make_err!(Code::InvalidArgument, "{}", err.to_string()) |
199 | 0 | } |
200 | | } |
201 | | |
202 | | impl From<core::convert::Infallible> for Error { |
203 | 0 | fn from(_err: core::convert::Infallible) -> Self { |
204 | | // Infallible is an error type that can never happen. |
205 | 0 | unreachable!(); |
206 | | } |
207 | | } |
208 | | |
209 | | impl From<TimestampError> for Error { |
210 | 0 | fn from(err: TimestampError) -> Self { |
211 | 0 | make_err!(Code::InvalidArgument, "{}", err) |
212 | 0 | } |
213 | | } |
214 | | |
215 | | impl From<std::io::Error> for Error { |
216 | 12 | fn from(err: std::io::Error) -> Self { |
217 | 12 | Self { |
218 | 12 | code: err.kind().into_code(), |
219 | 12 | messages: vec![err.to_string()], |
220 | 12 | } |
221 | 12 | } |
222 | | } |
223 | | |
224 | | impl From<fred::error::Error> for Error { |
225 | 0 | fn from(error: fred::error::Error) -> Self { |
226 | | use fred::error::ErrorKind::{ |
227 | | Auth, Backpressure, Canceled, Cluster, Config, IO, InvalidArgument, InvalidCommand, |
228 | | NotFound, Parse, Protocol, Routing, Sentinel, Timeout, Tls, Unknown, Url, |
229 | | }; |
230 | | |
231 | | // Conversions here are based on https://grpc.github.io/grpc/core/md_doc_statuscodes.html. |
232 | 0 | let code = match error.kind() { |
233 | 0 | Config | InvalidCommand | InvalidArgument | Url => Code::InvalidArgument, |
234 | 0 | IO | Protocol | Tls | Cluster | Parse | Sentinel | Routing => Code::Internal, |
235 | 0 | Auth => Code::PermissionDenied, |
236 | 0 | Canceled => Code::Aborted, |
237 | 0 | Unknown => Code::Unknown, |
238 | 0 | Timeout => Code::DeadlineExceeded, |
239 | 0 | NotFound => Code::NotFound, |
240 | 0 | Backpressure => Code::Unavailable, |
241 | | }; |
242 | | |
243 | 0 | make_err!(code, "{error}") |
244 | 0 | } |
245 | | } |
246 | | |
247 | | impl From<tonic::Status> for Error { |
248 | 2 | fn from(status: tonic::Status) -> Self { |
249 | 2 | make_err!(status.code(), "{}", status.to_string()) |
250 | 2 | } |
251 | | } |
252 | | |
253 | | impl From<Error> for tonic::Status { |
254 | 13 | fn from(val: Error) -> Self { |
255 | 13 | Self::new(val.code, val.messages.join(" : ")) |
256 | 13 | } |
257 | | } |
258 | | |
259 | | pub trait ResultExt<T> { |
260 | | /// # Errors |
261 | | /// |
262 | | /// Will return `Err` if we can't convert the error. |
263 | | fn err_tip_with_code<F, S>(self, tip_fn: F) -> Result<T, Error> |
264 | | where |
265 | | Self: Sized, |
266 | | S: ToString, |
267 | | F: (FnOnce(&Error) -> (Code, S)) + Sized; |
268 | | |
269 | | /// # Errors |
270 | | /// |
271 | | /// Will return `Err` if we can't convert the error. |
272 | | #[inline] |
273 | 276k | fn err_tip<F, S>(self, tip_fn: F) -> Result<T, Error> |
274 | 276k | where |
275 | 276k | Self: Sized, |
276 | 276k | S: ToString, |
277 | 276k | F: (FnOnce() -> S) + Sized, |
278 | | { |
279 | 276k | self.err_tip_with_code(|e| (e.code110 , tip_fn()110 )) |
280 | 276k | } |
281 | | |
282 | | /// # Errors |
283 | | /// |
284 | | /// Will return `Err` if we can't merge the errors. |
285 | 0 | fn merge<U>(self, _other: Result<U, Error>) -> Result<U, Error> |
286 | 0 | where |
287 | 0 | Self: Sized, |
288 | | { |
289 | 0 | unreachable!(); |
290 | | } |
291 | | } |
292 | | |
293 | | impl<T, E: Into<Error>> ResultExt<T> for Result<T, E> { |
294 | | #[inline] |
295 | 275k | fn err_tip_with_code<F, S>(self, tip_fn: F) -> Result<T, Error> |
296 | 275k | where |
297 | 275k | Self: Sized, |
298 | 275k | S: ToString, |
299 | 275k | F: (FnOnce(&Error) -> (Code, S)) + Sized, |
300 | | { |
301 | 275k | self.map_err(|e| {99 |
302 | 99 | let mut error: Error = e.into(); |
303 | 99 | let (code, message) = tip_fn(&error); |
304 | 99 | error.code = code; |
305 | 99 | error.messages.push(message.to_string()); |
306 | 99 | error |
307 | 99 | }) |
308 | 275k | } |
309 | | |
310 | 5.69k | fn merge<U>(self, other: Result<U, Error>) -> Result<U, Error> |
311 | 5.69k | where |
312 | 5.69k | Self: Sized, |
313 | | { |
314 | 5.69k | if let Err(e17 ) = self { Branch (314:16): [Folded - Ignored]
Branch (314:16): [True: 0, False: 0]
Branch (314:16): [True: 1, False: 5]
Branch (314:16): [True: 3, False: 39]
Branch (314:16): [True: 4, False: 259]
Branch (314:16): [True: 0, False: 1]
Branch (314:16): [True: 0, False: 1]
Branch (314:16): [True: 0, False: 0]
Branch (314:16): [True: 0, False: 0]
Branch (314:16): [True: 1, False: 17]
Branch (314:16): [True: 0, False: 0]
Branch (314:16): [True: 0, False: 1.23k]
Branch (314:16): [True: 0, False: 1]
Branch (314:16): [True: 3, False: 3.95k]
Branch (314:16): [True: 0, False: 0]
Branch (314:16): [True: 0, False: 1]
Branch (314:16): [True: 0, False: 0]
Branch (314:16): [True: 0, False: 10]
Branch (314:16): [True: 0, False: 1]
Branch (314:16): [True: 2, False: 1]
Branch (314:16): [True: 0, False: 2]
Branch (314:16): [True: 0, False: 0]
Branch (314:16): [True: 0, False: 0]
Branch (314:16): [True: 0, False: 0]
Branch (314:16): [True: 0, False: 0]
Branch (314:16): [True: 0, False: 0]
Branch (314:16): [True: 0, False: 25]
Branch (314:16): [True: 0, False: 4]
Branch (314:16): [True: 1, False: 3]
Branch (314:16): [True: 0, False: 0]
Branch (314:16): [True: 0, False: 0]
Branch (314:16): [True: 0, False: 0]
Branch (314:16): [Folded - Ignored]
Branch (314:16): [True: 0, False: 0]
Branch (314:16): [True: 1, False: 4]
Branch (314:16): [True: 0, False: 0]
Branch (314:16): [True: 0, False: 2]
Branch (314:16): [True: 0, False: 0]
Branch (314:16): [True: 0, False: 0]
Branch (314:16): [True: 0, False: 0]
Branch (314:16): [True: 0, False: 46]
Branch (314:16): [True: 0, False: 5]
Branch (314:16): [True: 0, False: 4]
Branch (314:16): [True: 0, False: 1]
Branch (314:16): [True: 0, False: 5]
Branch (314:16): [True: 0, False: 0]
Branch (314:16): [True: 1, False: 46]
Branch (314:16): [True: 0, False: 4]
Branch (314:16): [True: 0, False: 0]
Branch (314:16): [True: 0, False: 0]
Branch (314:16): [True: 0, False: 0]
Branch (314:16): [True: 0, False: 0]
|
315 | 17 | let mut e: Error = e.into(); |
316 | 17 | if let Err(other_err15 ) = other { Branch (316:20): [Folded - Ignored]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 1]
Branch (316:20): [True: 3, False: 0]
Branch (316:20): [True: 4, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 1, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 3, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 2, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 1, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [Folded - Ignored]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 1, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 1]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
Branch (316:20): [True: 0, False: 0]
|
317 | 15 | let mut other_err: Error = other_err; |
318 | 15 | // This will help with knowing which messages are tied to different errors. |
319 | 15 | e.messages.push("---".to_string()); |
320 | 15 | e.messages.append(&mut other_err.messages); |
321 | 15 | }2 |
322 | 17 | return Err(e); |
323 | 5.67k | } |
324 | 5.67k | other |
325 | 5.69k | } |
326 | | } |
327 | | |
328 | | impl<T> ResultExt<T> for Option<T> { |
329 | | #[inline] |
330 | 5.51k | fn err_tip_with_code<F, S>(self, tip_fn: F) -> Result<T, Error> |
331 | 5.51k | where |
332 | 5.51k | Self: Sized, |
333 | 5.51k | S: ToString, |
334 | 5.51k | F: (FnOnce(&Error) -> (Code, S)) + Sized, |
335 | | { |
336 | 5.51k | self.ok_or_else(|| {18 |
337 | 18 | let mut error = Error { |
338 | 18 | code: Code::Internal, |
339 | 18 | messages: vec![], |
340 | 18 | }; |
341 | 18 | let (code, message) = tip_fn(&error); |
342 | 18 | error.code = code; |
343 | 18 | error.messages.push(message.to_string()); |
344 | 18 | error |
345 | 18 | }) |
346 | 5.51k | } |
347 | | } |
348 | | |
349 | | trait CodeExt { |
350 | | fn into_error_kind(self) -> std::io::ErrorKind; |
351 | | } |
352 | | |
353 | | impl CodeExt for Code { |
354 | 1 | fn into_error_kind(self) -> std::io::ErrorKind { |
355 | 1 | match self { |
356 | 0 | Self::Aborted => std::io::ErrorKind::Interrupted, |
357 | 0 | Self::AlreadyExists => std::io::ErrorKind::AlreadyExists, |
358 | 0 | Self::DeadlineExceeded => std::io::ErrorKind::TimedOut, |
359 | 0 | Self::InvalidArgument => std::io::ErrorKind::InvalidInput, |
360 | 0 | Self::NotFound => std::io::ErrorKind::NotFound, |
361 | 0 | Self::PermissionDenied => std::io::ErrorKind::PermissionDenied, |
362 | 0 | Self::Unavailable => std::io::ErrorKind::ConnectionRefused, |
363 | 1 | _ => std::io::ErrorKind::Other, |
364 | | } |
365 | 1 | } |
366 | | } |
367 | | |
368 | | trait ErrorKindExt { |
369 | | fn into_code(self) -> Code; |
370 | | } |
371 | | |
372 | | impl ErrorKindExt for std::io::ErrorKind { |
373 | 12 | fn into_code(self) -> Code { |
374 | 12 | match self { |
375 | 11 | Self::NotFound => Code::NotFound, |
376 | 0 | Self::PermissionDenied => Code::PermissionDenied, |
377 | | Self::ConnectionRefused | Self::ConnectionReset | Self::ConnectionAborted => { |
378 | 0 | Code::Unavailable |
379 | | } |
380 | 0 | Self::AlreadyExists => Code::AlreadyExists, |
381 | 0 | Self::InvalidInput | Self::InvalidData => Code::InvalidArgument, |
382 | 0 | Self::TimedOut => Code::DeadlineExceeded, |
383 | 0 | Self::Interrupted => Code::Aborted, |
384 | | Self::NotConnected |
385 | | | Self::AddrInUse |
386 | | | Self::AddrNotAvailable |
387 | | | Self::BrokenPipe |
388 | | | Self::WouldBlock |
389 | | | Self::WriteZero |
390 | | | Self::Other |
391 | 1 | | Self::UnexpectedEof => Code::Internal, |
392 | 0 | _ => Code::Unknown, |
393 | | } |
394 | 12 | } |
395 | | } |
396 | | |
397 | | // Serde definition for tonic::Code. See: https://serde.rs/remote-derive.html |
398 | | #[derive(Debug, Clone, Copy, Serialize, Deserialize)] |
399 | | #[serde(remote = "Code")] |
400 | | pub enum CodeDef { |
401 | | Ok = 0, |
402 | | Cancelled = 1, |
403 | | Unknown = 2, |
404 | | InvalidArgument = 3, |
405 | | DeadlineExceeded = 4, |
406 | | NotFound = 5, |
407 | | AlreadyExists = 6, |
408 | | PermissionDenied = 7, |
409 | | ResourceExhausted = 8, |
410 | | FailedPrecondition = 9, |
411 | | Aborted = 10, |
412 | | OutOfRange = 11, |
413 | | Unimplemented = 12, |
414 | | Internal = 13, |
415 | | Unavailable = 14, |
416 | | DataLoss = 15, |
417 | | Unauthenticated = 16, |
418 | | // NOTE: Additional codes must be added to stores.rs in ErrorCodes and also |
419 | | // in both match statements in retry.rs. |
420 | | } |