Skip to content

Commit 6a1911e

Browse files
committed
treewide: rewrite from error_chain to thiserror
1 parent 86a44fa commit 6a1911e

File tree

12 files changed

+359
-310
lines changed

12 files changed

+359
-310
lines changed

Cargo.lock

Lines changed: 1 addition & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ name = "nix-locate"
2828
[dependencies]
2929
bincode = { version = "2.0.1", features = ["serde"] }
3030
byteorder = "1.5.0"
31-
error-chain = "0.12.4"
3231
futures = "0.3.30"
3332
grep = "0.3.1"
3433
atty = "0.2.14"
@@ -44,6 +43,7 @@ separator = "0.4.1"
4443
serde = { version = "1.0.198", features = [ "derive" ] }
4544
serde_bytes = "0.11.14"
4645
serde_json = "1.0.116"
46+
thiserror = "2.0.12"
4747
tokio-retry = "0.3.0"
4848
xdg = "3.0.0"
4949
xml-rs = "0.8.20"

src/bin/nix-channel-index.rs

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::path::PathBuf;
66
use std::process;
77

88
use clap::Parser;
9-
use error_chain::ChainedError;
109
use futures::{future, StreamExt};
1110
use nix_index::files::{FileNode, FileType};
1211
use nix_index::hydra::Fetcher;
@@ -16,9 +15,11 @@ use rusqlite::Connection;
1615

1716
/// The main function of this module: creates a new command-not-found database.
1817
async fn update_index(args: &Args) -> Result<()> {
19-
let fetcher = Fetcher::new(CACHE_URL.to_string()).map_err(ErrorKind::ParseProxy)?;
20-
let connection =
21-
Connection::open_in_memory().map_err(|_| ErrorKind::CreateDatabase(args.output.clone()))?;
18+
let fetcher = Fetcher::new(CACHE_URL.to_string()).map_err(Error::ParseProxy)?;
19+
let connection = Connection::open_in_memory().map_err(|e| Error::CreateDatabase {
20+
path: args.output.clone(),
21+
source: Box::new(e),
22+
})?;
2223

2324
connection
2425
.execute(
@@ -32,10 +33,15 @@ async fn update_index(args: &Args) -> Result<()> {
3233
"#,
3334
(),
3435
)
35-
.map_err(|_| ErrorKind::CreateDatabase(args.output.clone()))?;
36-
37-
let debug_connection = Connection::open_in_memory()
38-
.map_err(|_| ErrorKind::CreateDatabase(args.debug_output.clone()))?;
36+
.map_err(|e| Error::CreateDatabase {
37+
path: args.output.clone(),
38+
source: Box::new(e),
39+
})?;
40+
41+
let debug_connection = Connection::open_in_memory().map_err(|e| Error::CreateDatabase {
42+
path: args.debug_output.clone(),
43+
source: Box::new(e),
44+
})?;
3945
debug_connection
4046
.execute(
4147
r#"
@@ -48,7 +54,10 @@ async fn update_index(args: &Args) -> Result<()> {
4854
"#,
4955
(),
5056
)
51-
.map_err(|_| ErrorKind::CreateDatabase(args.debug_output.clone()))?;
57+
.map_err(|e| Error::CreateDatabase {
58+
path: args.debug_output.clone(),
59+
source: Box::new(e),
60+
})?;
5261

5362
let systems = match &args.systems {
5463
Some(systems) => systems.iter().map(|x| Some(x.as_str())).collect(),
@@ -62,7 +71,7 @@ async fn update_index(args: &Args) -> Result<()> {
6271
// Treat request errors as if the file list were missing
6372
let files = files.map(|r| {
6473
r.unwrap_or_else(|e| {
65-
eprint!("\n{}", e.display_chain());
74+
eprint!("\n{:?}", e);
6675
None
6776
})
6877
});
@@ -117,7 +126,7 @@ async fn update_index(args: &Args) -> Result<()> {
117126
"insert or replace into Programs(name, system, package) values (?, ?, ?)",
118127
(binary, system, attr),
119128
)
120-
.map_err(|_| ErrorKind::CreateDatabase(args.output.clone()))?;
129+
.map_err(|e| Error::CreateDatabase { path: args.output.clone(), source: Box::new(e) })?;
121130
}
122131

123132
if let Ok(debuginfo) = path.strip_prefix("/lib/debug/.build-id") {
@@ -139,7 +148,7 @@ async fn update_index(args: &Args) -> Result<()> {
139148
"insert or replace into DebugInfo(build_id, url, filename) values (?, ?, ?)",
140149
(build_id, format!("../{}", nar), path.to_string_lossy().strip_prefix('/')),
141150
)
142-
.map_err(|_| ErrorKind::CreateDatabase(args.debug_output.clone()))?;
151+
.map_err(|e| Error::CreateDatabase { path: args.debug_output.clone(), source: Box::new(e) })?;
143152
}
144153
}
145154
}
@@ -150,11 +159,17 @@ async fn update_index(args: &Args) -> Result<()> {
150159

151160
connection
152161
.backup("main", &args.output, None)
153-
.map_err(|_| ErrorKind::CreateDatabase(args.output.clone()))?;
162+
.map_err(|e| Error::CreateDatabase {
163+
path: args.output.clone(),
164+
source: Box::new(e),
165+
})?;
154166

155167
debug_connection
156168
.backup("main", &args.debug_output, None)
157-
.map_err(|_| ErrorKind::CreateDatabase(args.debug_output.clone()))?;
169+
.map_err(|e| Error::CreateDatabase {
170+
path: args.debug_output.clone(),
171+
source: Box::new(e),
172+
})?;
158173

159174
Ok(())
160175
}
@@ -192,15 +207,7 @@ async fn main() {
192207
let args = Args::parse();
193208

194209
if let Err(e) = update_index(&args).await {
195-
eprintln!("error: {}", e);
196-
197-
for e in e.iter().skip(1) {
198-
eprintln!("caused by: {}", e);
199-
}
200-
201-
if let Some(backtrace) = e.backtrace() {
202-
eprintln!("backtrace: {:?}", backtrace);
203-
}
210+
eprintln!("error: {:?}", e);
204211
process::exit(2);
205212
}
206213
}

src/bin/nix-index.rs

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::path::PathBuf;
66
use std::process;
77

88
use clap::Parser;
9-
use error_chain::ChainedError;
109
use futures::future::Either;
1110
use futures::{future, StreamExt};
1211
use nix_index::database::Writer;
@@ -31,7 +30,7 @@ async fn update_index(args: &Args) -> Result<()> {
3130
};
3231

3332
eprintln!("+ querying available packages");
34-
let fetcher = Fetcher::new(CACHE_URL.to_string()).map_err(ErrorKind::ParseProxy)?;
33+
let fetcher = Fetcher::new(CACHE_URL.to_string()).map_err(Error::ParseProxy)?;
3534
let (files, watch) = match cached {
3635
Some((f, w)) => (Either::Left(f), w),
3736
None => {
@@ -49,7 +48,7 @@ async fn update_index(args: &Args) -> Result<()> {
4948
// Treat request errors as if the file list were missing
5049
let files = files.map(|r| {
5150
r.unwrap_or_else(|e| {
52-
eprint!("\n{}", e.display_chain());
51+
eprint!("\n{:?}", e);
5352
None
5453
})
5554
});
@@ -76,10 +75,17 @@ async fn update_index(args: &Args) -> Result<()> {
7675
eprint!(" (filtering by `{}`)", args.filter_prefix);
7776
}
7877
eprint!("\r");
79-
fs::create_dir_all(&args.database)
80-
.chain_err(|| ErrorKind::CreateDatabaseDir(args.database.clone()))?;
81-
let mut db = Writer::create(args.database.join("files"), args.compression_level)
82-
.chain_err(|| ErrorKind::CreateDatabase(args.database.clone()))?;
78+
fs::create_dir_all(&args.database).map_err(|e| Error::CreateDatabaseDir {
79+
path: args.database.clone(),
80+
source: e,
81+
})?;
82+
let mut db =
83+
Writer::create(args.database.join("files"), args.compression_level).map_err(|e| {
84+
Error::CreateDatabase {
85+
path: args.database.clone(),
86+
source: Box::new(e),
87+
}
88+
})?;
8389

8490
let mut results: Vec<(StorePath, String, FileTree)> = Vec::new();
8591
while let Some(entry) = files.next().await {
@@ -88,21 +94,30 @@ async fn update_index(args: &Args) -> Result<()> {
8894
}
8995
let (path, _, files) = entry;
9096
db.add(path, files, args.filter_prefix.as_bytes())
91-
.chain_err(|| ErrorKind::WriteDatabase(args.database.clone()))?;
97+
.map_err(|e| Error::WriteDatabase {
98+
path: args.database.clone(),
99+
source: e,
100+
})?;
92101
}
93102
eprintln!();
94103

95104
if args.path_cache {
96105
eprintln!("+ writing path cache");
97-
let mut output = io::BufWriter::new(
98-
File::create("paths.cache").chain_err(|| ErrorKind::WritePathsCache)?,
99-
);
100-
bincode::serde::encode_into_std_write(&results, &mut output, bincode::config::standard()).chain_err(|| ErrorKind::WritePathsCache)?;
106+
let mut output = io::BufWriter::new(File::create("paths.cache").map_err(|e| {
107+
Error::WritePathsCache {
108+
source: Box::new(e),
109+
}
110+
})?);
111+
bincode::serde::encode_into_std_write(&results, &mut output, bincode::config::standard())
112+
.map_err(|e| Error::WritePathsCache {
113+
source: Box::new(e),
114+
})?;
101115
}
102116

103-
let index_size = db
104-
.finish()
105-
.chain_err(|| ErrorKind::WriteDatabase(args.database.clone()))?;
117+
let index_size = db.finish().map_err(|e| Error::WriteDatabase {
118+
path: args.database.clone(),
119+
source: e,
120+
})?;
106121
eprintln!("+ wrote index of {} bytes", index_size.separated_string());
107122

108123
Ok(())
@@ -160,15 +175,7 @@ async fn main() {
160175
let args = Args::parse();
161176

162177
if let Err(e) = update_index(&args).await {
163-
eprintln!("error: {}", e);
164-
165-
for e in e.iter().skip(1) {
166-
eprintln!("caused by: {}", e);
167-
}
168-
169-
if let Some(backtrace) = e.backtrace() {
170-
eprintln!("backtrace: {:?}", backtrace);
171-
}
178+
eprintln!("error: {:?}", e);
172179
process::exit(2);
173180
}
174181
}

0 commit comments

Comments
 (0)