Skip to content

Commit d843746

Browse files
committed
Add sources to auto error conversion
1 parent edeef58 commit d843746

4 files changed

Lines changed: 60 additions & 21 deletions

File tree

nativelink-error/BUILD.bazel

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ load(
44
"rust_doc_test",
55
"rust_library",
66
"rust_test",
7+
"rust_test_suite",
78
)
89

910
rust_library(
@@ -29,6 +30,18 @@ rust_library(
2930
],
3031
)
3132

33+
rust_test_suite(
34+
name = "integration",
35+
timeout = "short",
36+
srcs = [
37+
"tests/error_tests.rs",
38+
],
39+
deps = [
40+
"//nativelink-error",
41+
"@crates//:walkdir",
42+
],
43+
)
44+
3245
rust_test(
3346
name = "unit_test",
3447
timeout = "short",

nativelink-error/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ lints.workspace = true
55
autobenches = false
66
autobins = false
77
autoexamples = false
8-
autotests = false
98
edition = "2024"
109
name = "nativelink-error"
1110
version = "1.0.0-rc4"

nativelink-error/src/lib.rs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ impl Error {
8383
}
8484
}
8585

86+
#[must_use]
87+
pub fn from_std_err(code: Code, mut err: &dyn core::error::Error) -> Self {
88+
let mut messages = vec![format!("{err}")];
89+
while let Some(src) = err.source() {
90+
messages.push(format!("{src}"));
91+
err = src;
92+
}
93+
messages.reverse();
94+
Self::new_with_messages(code, messages)
95+
}
96+
8697
#[inline]
8798
#[must_use]
8899
pub fn append<S: Into<String>>(mut self, msg: S) -> Self {
@@ -162,37 +173,37 @@ impl core::fmt::Display for Error {
162173

163174
impl From<prost::DecodeError> for Error {
164175
fn from(err: prost::DecodeError) -> Self {
165-
make_err!(Code::Internal, "{}", err.to_string())
176+
Self::from_std_err(Code::Internal, &err)
166177
}
167178
}
168179

169180
impl From<prost::EncodeError> for Error {
170181
fn from(err: prost::EncodeError) -> Self {
171-
make_err!(Code::Internal, "{}", err.to_string())
182+
Self::from_std_err(Code::Internal, &err)
172183
}
173184
}
174185

175186
impl From<prost::UnknownEnumValue> for Error {
176187
fn from(err: prost::UnknownEnumValue) -> Self {
177-
make_err!(Code::Internal, "{}", err.to_string())
188+
Self::from_std_err(Code::Internal, &err)
178189
}
179190
}
180191

181192
impl From<core::num::TryFromIntError> for Error {
182193
fn from(err: core::num::TryFromIntError) -> Self {
183-
make_err!(Code::InvalidArgument, "{}", err.to_string())
194+
Self::from_std_err(Code::InvalidArgument, &err)
184195
}
185196
}
186197

187198
impl From<tokio::task::JoinError> for Error {
188199
fn from(err: tokio::task::JoinError) -> Self {
189-
make_err!(Code::Internal, "{}", err.to_string())
200+
Self::from_std_err(Code::Internal, &err)
190201
}
191202
}
192203

193204
impl<T> From<PoisonError<MutexGuard<'_, T>>> for Error {
194205
fn from(err: PoisonError<MutexGuard<'_, T>>) -> Self {
195-
make_err!(Code::Internal, "{}", err.to_string())
206+
Self::from_std_err(Code::Internal, &err)
196207
}
197208
}
198209

@@ -218,7 +229,7 @@ impl From<serde_json5::Error> for Error {
218229

219230
impl From<core::num::ParseIntError> for Error {
220231
fn from(err: core::num::ParseIntError) -> Self {
221-
make_err!(Code::InvalidArgument, "{}", err.to_string())
232+
Self::from_std_err(Code::InvalidArgument, &err)
222233
}
223234
}
224235

@@ -231,19 +242,19 @@ impl From<core::convert::Infallible> for Error {
231242

232243
impl From<TimestampError> for Error {
233244
fn from(err: TimestampError) -> Self {
234-
make_err!(Code::InvalidArgument, "{}", err)
245+
Self::from_std_err(Code::InvalidArgument, &err)
235246
}
236247
}
237248

238249
impl From<AcquireError> for Error {
239250
fn from(err: AcquireError) -> Self {
240-
make_err!(Code::Internal, "{}", err)
251+
Self::from_std_err(Code::Internal, &err)
241252
}
242253
}
243254

244255
impl From<Utf8Error> for Error {
245256
fn from(err: Utf8Error) -> Self {
246-
make_err!(Code::Internal, "{}", err)
257+
Self::from_std_err(Code::Internal, &err)
247258
}
248259
}
249260

@@ -295,32 +306,32 @@ impl From<Error> for tonic::Status {
295306
}
296307

297308
impl From<walkdir::Error> for Error {
298-
fn from(value: walkdir::Error) -> Self {
299-
Self::new(Code::Internal, value.to_string())
309+
fn from(err: walkdir::Error) -> Self {
310+
Self::from_std_err(Code::Internal, &err)
300311
}
301312
}
302313

303314
impl From<uuid::Error> for Error {
304-
fn from(value: uuid::Error) -> Self {
305-
Self::new(Code::Internal, value.to_string())
315+
fn from(err: uuid::Error) -> Self {
316+
Self::from_std_err(Code::Internal, &err)
306317
}
307318
}
308319

309320
impl From<rustls_pki_types::pem::Error> for Error {
310-
fn from(value: rustls_pki_types::pem::Error) -> Self {
311-
Self::new(Code::Internal, value.to_string())
321+
fn from(err: rustls_pki_types::pem::Error) -> Self {
322+
Self::from_std_err(Code::Internal, &err)
312323
}
313324
}
314325

315326
impl From<tokio::time::error::Elapsed> for Error {
316-
fn from(value: tokio::time::error::Elapsed) -> Self {
317-
Self::new(Code::DeadlineExceeded, value.to_string())
327+
fn from(err: tokio::time::error::Elapsed) -> Self {
328+
Self::from_std_err(Code::DeadlineExceeded, &err)
318329
}
319330
}
320331

321332
impl From<url::ParseError> for Error {
322-
fn from(value: url::ParseError) -> Self {
323-
Self::new(Code::Internal, value.to_string())
333+
fn from(err: url::ParseError) -> Self {
334+
Self::from_std_err(Code::DeadlineExceeded, &err)
324335
}
325336
}
326337

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use nativelink_error::Error;
2+
use walkdir::WalkDir;
3+
4+
#[test]
5+
fn walkdir_source_error() {
6+
for entry in WalkDir::new("/bad/path") {
7+
let err: Error = entry.unwrap_err().into();
8+
assert_eq!(
9+
err.messages,
10+
vec![
11+
"No such file or directory (os error 2)",
12+
"IO error for operation on /bad/path: No such file or directory (os error 2)"
13+
]
14+
);
15+
}
16+
}

0 commit comments

Comments
 (0)