Skip to content

Commit 18cbf4a

Browse files
author
Pascal Hertleif
committed
Add context to more errors
cf. #569
1 parent b3d30de commit 18cbf4a

26 files changed

+191
-67
lines changed

src/fs/canonicalize.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::io;
22
use crate::path::{Path, PathBuf};
33
use crate::task::spawn_blocking;
4+
use crate::utils::Context as _;
45

56
/// Returns the canonical form of a path.
67
///
@@ -32,5 +33,10 @@ use crate::task::spawn_blocking;
3233
/// ```
3334
pub async fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
3435
let path = path.as_ref().to_owned();
35-
spawn_blocking(move || std::fs::canonicalize(&path).map(Into::into)).await
36+
spawn_blocking(move || {
37+
std::fs::canonicalize(&path)
38+
.map(Into::into)
39+
.context(|| format!("could not canonicalize `{}`", path.display()))
40+
})
41+
.await
3642
}

src/fs/copy.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::io;
22
use crate::path::Path;
33
use crate::task::spawn_blocking;
4+
use crate::utils::Context as _;
45

56
/// Copies the contents and permissions of a file to a new location.
67
///
@@ -41,5 +42,9 @@ use crate::task::spawn_blocking;
4142
pub async fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> {
4243
let from = from.as_ref().to_owned();
4344
let to = to.as_ref().to_owned();
44-
spawn_blocking(move || std::fs::copy(&from, &to)).await
45+
spawn_blocking(move || {
46+
std::fs::copy(&from, &to)
47+
.context(|| format!("could not copy `{}` to `{}`", from.display(), to.display()))
48+
})
49+
.await
4550
}

src/fs/create_dir.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::io;
22
use crate::path::Path;
33
use crate::task::spawn_blocking;
4+
use crate::utils::Context as _;
45

56
/// Creates a new directory.
67
///
@@ -34,5 +35,9 @@ use crate::task::spawn_blocking;
3435
/// ```
3536
pub async fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
3637
let path = path.as_ref().to_owned();
37-
spawn_blocking(move || std::fs::create_dir(path)).await
38+
spawn_blocking(move || {
39+
std::fs::create_dir(&path)
40+
.context(|| format!("could not create directory `{}`", path.display()))
41+
})
42+
.await
3843
}

src/fs/create_dir_all.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::io;
22
use crate::path::Path;
33
use crate::task::spawn_blocking;
4+
use crate::utils::Context as _;
45

56
/// Creates a new directory and all of its parents if they are missing.
67
///
@@ -29,5 +30,9 @@ use crate::task::spawn_blocking;
2930
/// ```
3031
pub async fn create_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
3132
let path = path.as_ref().to_owned();
32-
spawn_blocking(move || std::fs::create_dir_all(path)).await
33+
spawn_blocking(move || {
34+
std::fs::create_dir_all(&path)
35+
.context(|| format!("could not create directory path `{}`", path.display()))
36+
})
37+
.await
3338
}

src/fs/file.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ use std::sync::{Arc, Mutex};
99

1010
use crate::fs::{Metadata, Permissions};
1111
use crate::future;
12-
use crate::utils::Context as _;
1312
use crate::io::{self, Read, Seek, SeekFrom, Write};
1413
use crate::path::Path;
1514
use crate::prelude::*;
1615
use crate::task::{self, spawn_blocking, Context, Poll, Waker};
16+
use crate::utils::Context as _;
1717

1818
/// An open file on the filesystem.
1919
///
@@ -114,8 +114,7 @@ impl File {
114114
pub async fn open<P: AsRef<Path>>(path: P) -> io::Result<File> {
115115
let path = path.as_ref().to_owned();
116116
let file = spawn_blocking(move || {
117-
std::fs::File::open(&path)
118-
.context(|| format!("Could not open {}", path.display()))
117+
std::fs::File::open(&path).context(|| format!("Could not open `{}`", path.display()))
119118
})
120119
.await?;
121120
Ok(File::new(file, true))
@@ -154,7 +153,7 @@ impl File {
154153
let path = path.as_ref().to_owned();
155154
let file = spawn_blocking(move || {
156155
std::fs::File::create(&path)
157-
.context(|| format!("Could not create {}", path.display()))
156+
.context(|| format!("Could not create `{}`", path.display()))
158157
})
159158
.await?;
160159
Ok(File::new(file, true))

src/fs/hard_link.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::io;
22
use crate::path::Path;
33
use crate::task::spawn_blocking;
4+
use crate::utils::Context as _;
45

56
/// Creates a hard link on the filesystem.
67
///
@@ -32,5 +33,14 @@ use crate::task::spawn_blocking;
3233
pub async fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()> {
3334
let from = from.as_ref().to_owned();
3435
let to = to.as_ref().to_owned();
35-
spawn_blocking(move || std::fs::hard_link(&from, &to)).await
36+
spawn_blocking(move || {
37+
std::fs::hard_link(&from, &to).context(|| {
38+
format!(
39+
"could not create a hard link from `{}` to `{}`",
40+
from.display(),
41+
to.display()
42+
)
43+
})
44+
})
45+
.await
3646
}

src/fs/open_options.rs

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ use crate::task::spawn_blocking;
3434
/// #
3535
/// use async_std::fs::OpenOptions;
3636
///
37-
/// let file = OpenOptions::new()
38-
/// .read(true)
39-
/// .open("a.txt")
40-
/// .await?;
37+
/// let file = OpenOptions::new().read(true).open("a.txt").await?;
4138
/// #
4239
/// # Ok(()) }) }
4340
/// ```
@@ -73,10 +70,7 @@ impl OpenOptions {
7370
/// #
7471
/// use async_std::fs::OpenOptions;
7572
///
76-
/// let file = OpenOptions::new()
77-
/// .read(true)
78-
/// .open("a.txt")
79-
/// .await?;
73+
/// let file = OpenOptions::new().read(true).open("a.txt").await?;
8074
/// #
8175
/// # Ok(()) }) }
8276
/// ```
@@ -95,10 +89,7 @@ impl OpenOptions {
9589
/// #
9690
/// use async_std::fs::OpenOptions;
9791
///
98-
/// let file = OpenOptions::new()
99-
/// .read(true)
100-
/// .open("a.txt")
101-
/// .await?;
92+
/// let file = OpenOptions::new().read(true).open("a.txt").await?;
10293
/// #
10394
/// # Ok(()) }) }
10495
/// ```
@@ -121,10 +112,7 @@ impl OpenOptions {
121112
/// #
122113
/// use async_std::fs::OpenOptions;
123114
///
124-
/// let file = OpenOptions::new()
125-
/// .write(true)
126-
/// .open("a.txt")
127-
/// .await?;
115+
/// let file = OpenOptions::new().write(true).open("a.txt").await?;
128116
/// #
129117
/// # Ok(()) }) }
130118
/// ```
@@ -145,10 +133,7 @@ impl OpenOptions {
145133
/// #
146134
/// use async_std::fs::OpenOptions;
147135
///
148-
/// let file = OpenOptions::new()
149-
/// .append(true)
150-
/// .open("a.txt")
151-
/// .await?;
136+
/// let file = OpenOptions::new().append(true).open("a.txt").await?;
152137
/// #
153138
/// # Ok(()) }) }
154139
/// ```
@@ -274,10 +259,7 @@ impl OpenOptions {
274259
/// #
275260
/// use async_std::fs::OpenOptions;
276261
///
277-
/// let file = OpenOptions::new()
278-
/// .read(true)
279-
/// .open("a.txt")
280-
/// .await?;
262+
/// let file = OpenOptions::new().read(true).open("a.txt").await?;
281263
/// #
282264
/// # Ok(()) }) }
283265
/// ```

src/fs/read.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::io;
22
use crate::path::Path;
33
use crate::task::spawn_blocking;
4+
use crate::utils::Context as _;
45

56
/// Reads the entire contents of a file as raw bytes.
67
///
@@ -36,5 +37,8 @@ use crate::task::spawn_blocking;
3637
/// ```
3738
pub async fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
3839
let path = path.as_ref().to_owned();
39-
spawn_blocking(move || std::fs::read(path)).await
40+
spawn_blocking(move || {
41+
std::fs::read(&path).context(|| format!("could not read file `{}`", path.display()))
42+
})
43+
.await
4044
}

src/fs/read_dir.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
use std::pin::Pin;
21
use std::future::Future;
2+
use std::pin::Pin;
33

44
use crate::fs::DirEntry;
55
use crate::io;
66
use crate::path::Path;
77
use crate::stream::Stream;
88
use crate::task::{spawn_blocking, Context, JoinHandle, Poll};
9+
use crate::utils::Context as _;
910

1011
/// Returns a stream of entries in a directory.
1112
///
@@ -45,9 +46,12 @@ use crate::task::{spawn_blocking, Context, JoinHandle, Poll};
4546
/// ```
4647
pub async fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
4748
let path = path.as_ref().to_owned();
48-
spawn_blocking(move || std::fs::read_dir(path))
49-
.await
50-
.map(ReadDir::new)
49+
spawn_blocking(move || {
50+
std::fs::read_dir(&path)
51+
.context(|| format!("could not read directory `{}`", path.display()))
52+
})
53+
.await
54+
.map(ReadDir::new)
5155
}
5256

5357
/// A stream of entries in a directory.

src/fs/read_link.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::io;
22
use crate::path::{Path, PathBuf};
33
use crate::task::spawn_blocking;
4+
use crate::utils::Context as _;
45

56
/// Reads a symbolic link and returns the path it points to.
67
///
@@ -28,5 +29,10 @@ use crate::task::spawn_blocking;
2829
/// ```
2930
pub async fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
3031
let path = path.as_ref().to_owned();
31-
spawn_blocking(move || std::fs::read_link(path).map(Into::into)).await
32+
spawn_blocking(move || {
33+
std::fs::read_link(&path)
34+
.map(Into::into)
35+
.context(|| format!("could not read link `{}`", path.display()))
36+
})
37+
.await
3238
}

src/fs/read_to_string.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::io;
22
use crate::path::Path;
33
use crate::task::spawn_blocking;
4+
use crate::utils::Context as _;
45

56
/// Reads the entire contents of a file as a string.
67
///
@@ -37,5 +38,9 @@ use crate::task::spawn_blocking;
3738
/// ```
3839
pub async fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
3940
let path = path.as_ref().to_owned();
40-
spawn_blocking(move || std::fs::read_to_string(path)).await
41+
spawn_blocking(move || {
42+
std::fs::read_to_string(&path)
43+
.context(|| format!("could not read file `{}`", path.display()))
44+
})
45+
.await
4146
}

src/fs/remove_dir.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::io;
22
use crate::path::Path;
33
use crate::task::spawn_blocking;
4+
use crate::utils::Context as _;
45

56
/// Removes an empty directory.
67
///
@@ -29,5 +30,9 @@ use crate::task::spawn_blocking;
2930
/// ```
3031
pub async fn remove_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
3132
let path = path.as_ref().to_owned();
32-
spawn_blocking(move || std::fs::remove_dir(path)).await
33+
spawn_blocking(move || {
34+
std::fs::remove_dir(&path)
35+
.context(|| format!("could not remove directory `{}`", path.display()))
36+
})
37+
.await
3338
}

src/fs/remove_dir_all.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::io;
22
use crate::path::Path;
33
use crate::task::spawn_blocking;
4+
use crate::utils::Context as _;
45

56
/// Removes a directory and all of its contents.
67
///
@@ -29,5 +30,9 @@ use crate::task::spawn_blocking;
2930
/// ```
3031
pub async fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
3132
let path = path.as_ref().to_owned();
32-
spawn_blocking(move || std::fs::remove_dir_all(path)).await
33+
spawn_blocking(move || {
34+
std::fs::remove_dir_all(&path)
35+
.context(|| format!("could not remove directory `{}`", path.display()))
36+
})
37+
.await
3338
}

src/fs/remove_file.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::io;
22
use crate::path::Path;
33
use crate::task::spawn_blocking;
4+
use crate::utils::Context as _;
45

56
/// Removes a file.
67
///
@@ -29,5 +30,9 @@ use crate::task::spawn_blocking;
2930
/// ```
3031
pub async fn remove_file<P: AsRef<Path>>(path: P) -> io::Result<()> {
3132
let path = path.as_ref().to_owned();
32-
spawn_blocking(move || std::fs::remove_file(path)).await
33+
spawn_blocking(move || {
34+
std::fs::remove_file(&path)
35+
.context(|| format!("could not remove file `{}`", path.display()))
36+
})
37+
.await
3338
}

src/fs/rename.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::io;
22
use crate::path::Path;
33
use crate::task::spawn_blocking;
4+
use crate::utils::Context as _;
45

56
/// Renames a file or directory to a new location.
67
///
@@ -34,5 +35,14 @@ use crate::task::spawn_blocking;
3435
pub async fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()> {
3536
let from = from.as_ref().to_owned();
3637
let to = to.as_ref().to_owned();
37-
spawn_blocking(move || std::fs::rename(&from, &to)).await
38+
spawn_blocking(move || {
39+
std::fs::rename(&from, &to).context(|| {
40+
format!(
41+
"could not rename `{}` to `{}`",
42+
from.display(),
43+
to.display()
44+
)
45+
})
46+
})
47+
.await
3848
}

src/fs/write.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::io;
22
use crate::path::Path;
33
use crate::task::spawn_blocking;
4+
use crate::utils::Context as _;
45

56
/// Writes a slice of bytes as the new contents of a file.
67
///
@@ -33,5 +34,9 @@ use crate::task::spawn_blocking;
3334
pub async fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> io::Result<()> {
3435
let path = path.as_ref().to_owned();
3536
let contents = contents.as_ref().to_owned();
36-
spawn_blocking(move || std::fs::write(path, contents)).await
37+
spawn_blocking(move || {
38+
std::fs::write(&path, contents)
39+
.context(|| format!("could not write to file `{}`", path.display()))
40+
})
41+
.await
3742
}

src/future/poll_fn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use std::pin::Pin;
21
use std::future::Future;
2+
use std::pin::Pin;
33

44
use crate::task::{Context, Poll};
55

0 commit comments

Comments
 (0)