Skip to content

Commit 87040b8

Browse files
committed
Use thiserror crate to derive std::error::Error for error enums
1 parent ca84e29 commit 87040b8

File tree

3 files changed

+46
-23
lines changed

3 files changed

+46
-23
lines changed

Cargo.lock

+23-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ serde_json = "1.0"
99
serde = { version = "1.0", features = ["derive"] }
1010
indexmap = { version = "1.9.3", features = ["serde"] }
1111
yansi = "0.5.1"
12-
derive_more = { version = "1.0", features = ["display", "from", "into"] }
12+
derive_more = { version = "1.0", features = ["display", "into"] }
13+
thiserror = "2.0.3"
1314
pipe-trait = "0.4.0"
1415
clap = { version = "4.3.2", features = ["derive"] }
1516
lets_find_up = "0.0.3"

src/error.rs

+21-20
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,71 @@
11
use crate::shell_quoted::ShellQuoted;
2-
use derive_more::{Display, From};
32
use std::{env::JoinPathsError, io, num::NonZeroI32, path::PathBuf};
43

54
/// Error types emitted by `pn` itself.
6-
#[derive(Debug, Display)]
5+
#[derive(Debug, thiserror::Error)]
76
pub enum PnError {
87
/// Script not found when running `pn run`.
9-
#[display("Missing script: {name}")]
8+
#[error("Missing script: {name}")]
109
MissingScript { name: String },
1110

1211
/// Script ran by `pn run` exits with non-zero status code.
13-
#[display("Command failed with exit code {status}")]
12+
#[error("Command failed with exit code {status}")]
1413
ScriptError { name: String, status: NonZeroI32 },
1514

1615
/// Subprocess finishes but without a status code.
17-
#[display("Command ended unexpectedly: {command}")]
16+
#[error("Command ended unexpectedly: {command}")]
1817
UnexpectedTermination { command: ShellQuoted },
1918

2019
/// Fail to spawn a subprocess.
21-
#[display("Failed to spawn process: {_0}")]
22-
SpawnProcessError(io::Error),
20+
#[error("Failed to spawn process: {0}")]
21+
SpawnProcessError(#[source] io::Error),
2322

2423
/// Fail to wait for the subprocess to finish.
25-
#[display("Failed to wait for the process: {_0}")]
26-
WaitProcessError(io::Error),
24+
#[error("Failed to wait for the process: {0}")]
25+
WaitProcessError(#[source] io::Error),
2726

2827
/// The program receives --workspace-root outside a workspace.
29-
#[display("--workspace-root may only be used in a workspace")]
28+
#[error("--workspace-root may only be used in a workspace")]
3029
NotInWorkspace,
3130

3231
/// No package manifest.
33-
#[display("File not found: {file:?}")]
32+
#[error("File not found: {file:?}")]
3433
NoPkgManifest { file: PathBuf },
3534

3635
/// Error related to filesystem operation.
37-
#[display("{path:?}: {error}")]
38-
FsError { path: PathBuf, error: io::Error },
36+
#[error("{path:?}: {error}")]
37+
FsError { path: PathBuf, #[source] error: io::Error },
3938

4039
/// Error emitted by [`lets_find_up`]'s functions.
41-
#[display("Failed to find {file_name:?} from {start_dir:?} upward: {error}")]
40+
#[error("Failed to find {file_name:?} from {start_dir:?} upward: {error}")]
4241
FindUpError {
4342
start_dir: PathBuf,
4443
file_name: &'static str,
44+
#[source]
4545
error: io::Error,
4646
},
4747

4848
/// An error is encountered when write to stdout.
49-
#[display("Failed to write to stdout: {_0}")]
49+
#[error("Failed to write to stdout: {0}")]
5050
WriteStdoutError(io::Error),
5151

5252
/// Parse JSON error.
53-
#[display("Failed to parse {file:?}: {message}")]
53+
#[error("Failed to parse {file:?}: {message}")]
5454
ParseJsonError { file: PathBuf, message: String },
5555

5656
/// Failed to prepend `node_modules/.bin` to `PATH`.
57-
#[display("Cannot add `node_modules/.bin` to PATH: {_0}")]
57+
#[error("Cannot add `node_modules/.bin` to PATH: {0}")]
5858
NodeBinPathError(JoinPathsError),
5959
}
6060

6161
/// The main error type.
62-
#[derive(Debug, Display, From)]
62+
#[derive(Debug, thiserror::Error)]
6363
pub enum MainError {
6464
/// Errors emitted by `pn` itself.
65-
Pn(PnError),
65+
#[error(transparent)]
66+
Pn(#[from] PnError),
6667

6768
/// The subprocess that takes control exits with non-zero status code.
68-
#[from(ignore)]
69+
#[error("{0}")]
6970
Sub(NonZeroI32),
7071
}

0 commit comments

Comments
 (0)