Coverage Report

Created: 2025-11-18 18:43

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