Skip to content

Add context to more errors #571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/fs/canonicalize.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::io;
use crate::path::{Path, PathBuf};
use crate::task::spawn_blocking;
use crate::utils::Context as _;

/// Returns the canonical form of a path.
///
Expand Down Expand Up @@ -32,5 +33,10 @@ use crate::task::spawn_blocking;
/// ```
pub async fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
let path = path.as_ref().to_owned();
spawn_blocking(move || std::fs::canonicalize(&path).map(Into::into)).await
spawn_blocking(move || {
std::fs::canonicalize(&path)
.map(Into::into)
.context(|| format!("could not canonicalize `{}`", path.display()))
})
.await
}
7 changes: 6 additions & 1 deletion src/fs/copy.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::io;
use crate::path::Path;
use crate::task::spawn_blocking;
use crate::utils::Context as _;

/// Copies the contents and permissions of a file to a new location.
///
Expand Down Expand Up @@ -41,5 +42,9 @@ use crate::task::spawn_blocking;
pub async fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> {
let from = from.as_ref().to_owned();
let to = to.as_ref().to_owned();
spawn_blocking(move || std::fs::copy(&from, &to)).await
spawn_blocking(move || {
std::fs::copy(&from, &to)
.context(|| format!("could not copy `{}` to `{}`", from.display(), to.display()))
})
.await
}
7 changes: 6 additions & 1 deletion src/fs/create_dir.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::io;
use crate::path::Path;
use crate::task::spawn_blocking;
use crate::utils::Context as _;

/// Creates a new directory.
///
Expand Down Expand Up @@ -34,5 +35,9 @@ use crate::task::spawn_blocking;
/// ```
pub async fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
let path = path.as_ref().to_owned();
spawn_blocking(move || std::fs::create_dir(path)).await
spawn_blocking(move || {
std::fs::create_dir(&path)
.context(|| format!("could not create directory `{}`", path.display()))
})
.await
}
7 changes: 6 additions & 1 deletion src/fs/create_dir_all.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::io;
use crate::path::Path;
use crate::task::spawn_blocking;
use crate::utils::Context as _;

/// Creates a new directory and all of its parents if they are missing.
///
Expand Down Expand Up @@ -29,5 +30,9 @@ use crate::task::spawn_blocking;
/// ```
pub async fn create_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
let path = path.as_ref().to_owned();
spawn_blocking(move || std::fs::create_dir_all(path)).await
spawn_blocking(move || {
std::fs::create_dir_all(&path)
.context(|| format!("could not create directory path `{}`", path.display()))
})
.await
}
7 changes: 3 additions & 4 deletions src/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use std::sync::{Arc, Mutex};

use crate::fs::{Metadata, Permissions};
use crate::future;
use crate::utils::Context as _;
use crate::io::{self, Read, Seek, SeekFrom, Write};
use crate::path::Path;
use crate::prelude::*;
use crate::task::{self, spawn_blocking, Context, Poll, Waker};
use crate::utils::Context as _;

/// An open file on the filesystem.
///
Expand Down Expand Up @@ -114,8 +114,7 @@ impl File {
pub async fn open<P: AsRef<Path>>(path: P) -> io::Result<File> {
let path = path.as_ref().to_owned();
let file = spawn_blocking(move || {
std::fs::File::open(&path)
.context(|| format!("Could not open {}", path.display()))
std::fs::File::open(&path).context(|| format!("could not open `{}`", path.display()))
})
.await?;
Ok(File::new(file, true))
Expand Down Expand Up @@ -154,7 +153,7 @@ impl File {
let path = path.as_ref().to_owned();
let file = spawn_blocking(move || {
std::fs::File::create(&path)
.context(|| format!("Could not create {}", path.display()))
.context(|| format!("could not create `{}`", path.display()))
})
.await?;
Ok(File::new(file, true))
Expand Down
12 changes: 11 additions & 1 deletion src/fs/hard_link.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::io;
use crate::path::Path;
use crate::task::spawn_blocking;
use crate::utils::Context as _;

/// Creates a hard link on the filesystem.
///
Expand Down Expand Up @@ -32,5 +33,14 @@ use crate::task::spawn_blocking;
pub async fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()> {
let from = from.as_ref().to_owned();
let to = to.as_ref().to_owned();
spawn_blocking(move || std::fs::hard_link(&from, &to)).await
spawn_blocking(move || {
std::fs::hard_link(&from, &to).context(|| {
format!(
"could not create a hard link from `{}` to `{}`",
from.display(),
to.display()
)
})
})
.await
}
6 changes: 5 additions & 1 deletion src/fs/read.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::io;
use crate::path::Path;
use crate::task::spawn_blocking;
use crate::utils::Context as _;

/// Reads the entire contents of a file as raw bytes.
///
Expand Down Expand Up @@ -36,5 +37,8 @@ use crate::task::spawn_blocking;
/// ```
pub async fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
let path = path.as_ref().to_owned();
spawn_blocking(move || std::fs::read(path)).await
spawn_blocking(move || {
std::fs::read(&path).context(|| format!("could not read file `{}`", path.display()))
})
.await
}
12 changes: 8 additions & 4 deletions src/fs/read_dir.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::pin::Pin;
use std::future::Future;
use std::pin::Pin;

use crate::fs::DirEntry;
use crate::io;
use crate::path::Path;
use crate::stream::Stream;
use crate::task::{spawn_blocking, Context, JoinHandle, Poll};
use crate::utils::Context as _;

/// Returns a stream of entries in a directory.
///
Expand Down Expand Up @@ -45,9 +46,12 @@ use crate::task::{spawn_blocking, Context, JoinHandle, Poll};
/// ```
pub async fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
let path = path.as_ref().to_owned();
spawn_blocking(move || std::fs::read_dir(path))
.await
.map(ReadDir::new)
spawn_blocking(move || {
std::fs::read_dir(&path)
.context(|| format!("could not read directory `{}`", path.display()))
})
.await
.map(ReadDir::new)
}

/// A stream of entries in a directory.
Expand Down
8 changes: 7 additions & 1 deletion src/fs/read_link.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::io;
use crate::path::{Path, PathBuf};
use crate::task::spawn_blocking;
use crate::utils::Context as _;

/// Reads a symbolic link and returns the path it points to.
///
Expand Down Expand Up @@ -28,5 +29,10 @@ use crate::task::spawn_blocking;
/// ```
pub async fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
let path = path.as_ref().to_owned();
spawn_blocking(move || std::fs::read_link(path).map(Into::into)).await
spawn_blocking(move || {
std::fs::read_link(&path)
.map(Into::into)
.context(|| format!("could not read link `{}`", path.display()))
})
.await
}
7 changes: 6 additions & 1 deletion src/fs/read_to_string.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::io;
use crate::path::Path;
use crate::task::spawn_blocking;
use crate::utils::Context as _;

/// Reads the entire contents of a file as a string.
///
Expand Down Expand Up @@ -37,5 +38,9 @@ use crate::task::spawn_blocking;
/// ```
pub async fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
let path = path.as_ref().to_owned();
spawn_blocking(move || std::fs::read_to_string(path)).await
spawn_blocking(move || {
std::fs::read_to_string(&path)
.context(|| format!("could not read file `{}`", path.display()))
})
.await
}
7 changes: 6 additions & 1 deletion src/fs/remove_dir.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::io;
use crate::path::Path;
use crate::task::spawn_blocking;
use crate::utils::Context as _;

/// Removes an empty directory.
///
Expand Down Expand Up @@ -29,5 +30,9 @@ use crate::task::spawn_blocking;
/// ```
pub async fn remove_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
let path = path.as_ref().to_owned();
spawn_blocking(move || std::fs::remove_dir(path)).await
spawn_blocking(move || {
std::fs::remove_dir(&path)
.context(|| format!("could not remove directory `{}`", path.display()))
})
.await
}
7 changes: 6 additions & 1 deletion src/fs/remove_dir_all.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::io;
use crate::path::Path;
use crate::task::spawn_blocking;
use crate::utils::Context as _;

/// Removes a directory and all of its contents.
///
Expand Down Expand Up @@ -29,5 +30,9 @@ use crate::task::spawn_blocking;
/// ```
pub async fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
let path = path.as_ref().to_owned();
spawn_blocking(move || std::fs::remove_dir_all(path)).await
spawn_blocking(move || {
std::fs::remove_dir_all(&path)
.context(|| format!("could not remove directory `{}`", path.display()))
})
.await
}
7 changes: 6 additions & 1 deletion src/fs/remove_file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::io;
use crate::path::Path;
use crate::task::spawn_blocking;
use crate::utils::Context as _;

/// Removes a file.
///
Expand Down Expand Up @@ -29,5 +30,9 @@ use crate::task::spawn_blocking;
/// ```
pub async fn remove_file<P: AsRef<Path>>(path: P) -> io::Result<()> {
let path = path.as_ref().to_owned();
spawn_blocking(move || std::fs::remove_file(path)).await
spawn_blocking(move || {
std::fs::remove_file(&path)
.context(|| format!("could not remove file `{}`", path.display()))
})
.await
}
12 changes: 11 additions & 1 deletion src/fs/rename.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::io;
use crate::path::Path;
use crate::task::spawn_blocking;
use crate::utils::Context as _;

/// Renames a file or directory to a new location.
///
Expand Down Expand Up @@ -34,5 +35,14 @@ use crate::task::spawn_blocking;
pub async fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()> {
let from = from.as_ref().to_owned();
let to = to.as_ref().to_owned();
spawn_blocking(move || std::fs::rename(&from, &to)).await
spawn_blocking(move || {
std::fs::rename(&from, &to).context(|| {
format!(
"could not rename `{}` to `{}`",
from.display(),
to.display()
)
})
})
.await
}
7 changes: 6 additions & 1 deletion src/fs/write.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::io;
use crate::path::Path;
use crate::task::spawn_blocking;
use crate::utils::Context as _;

/// Writes a slice of bytes as the new contents of a file.
///
Expand Down Expand Up @@ -33,5 +34,9 @@ use crate::task::spawn_blocking;
pub async fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> io::Result<()> {
let path = path.as_ref().to_owned();
let contents = contents.as_ref().to_owned();
spawn_blocking(move || std::fs::write(path, contents)).await
spawn_blocking(move || {
std::fs::write(&path, contents)
.context(|| format!("could not write to file `{}`", path.display()))
})
.await
}
7 changes: 4 additions & 3 deletions src/io/copy.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::pin::Pin;
use std::future::Future;
use std::pin::Pin;

use pin_project_lite::pin_project;

use crate::io::{self, BufRead, BufReader, Read, Write};
use crate::task::{Context, Poll};
use crate::utils::Context as _;

/// Copies the entire contents of a reader into a writer.
///
Expand Down Expand Up @@ -90,7 +91,7 @@ where
writer,
amt: 0,
};
future.await
future.await.context(|| String::from("io::copy failed"))
}

/// Copies the entire contents of a reader into a writer.
Expand Down Expand Up @@ -177,5 +178,5 @@ where
writer,
amt: 0,
};
future.await
future.await.context(|| String::from("io::copy failed"))
}
4 changes: 3 additions & 1 deletion src/io/stdin.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::future::Future;
use std::pin::Pin;
use std::sync::Mutex;
use std::future::Future;

use crate::future;
use crate::io::{self, Read};
use crate::task::{spawn_blocking, Context, JoinHandle, Poll};
use crate::utils::Context as _;

cfg_unstable! {
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -162,6 +163,7 @@ impl Stdin {
}
})
.await
.context(|| String::from("could not read line on stdin"))
}

/// Locks this handle to the standard input stream, returning a readable guard.
Expand Down
Loading