Skip to content

Commit 57092cd

Browse files
committed
improve error message
1 parent 40609eb commit 57092cd

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

src/db.rs

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ use crate::{
55
densemap, densemap::DenseMap, graph::BuildId, graph::FileId, graph::Graph, graph::Hashes,
66
hash::BuildHash,
77
};
8-
use anyhow::{anyhow, bail};
8+
use anyhow::bail;
99
use std::collections::HashMap;
1010
use std::fs::File;
1111
use std::io::BufReader;
1212
use std::io::Read;
1313
use std::io::Write;
1414
use std::path::Path;
15+
use std::path::PathBuf;
1516

1617
const VERSION: u32 = 1;
1718

@@ -303,21 +304,75 @@ impl<'a> Reader<'a> {
303304
}
304305
}
305306

307+
#[derive(Debug)]
308+
pub struct OpenError {
309+
path: PathBuf,
310+
source: OpenErrorKind,
311+
}
312+
313+
impl std::fmt::Display for OpenError {
314+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
315+
write!(f, "failed to open {}", self.path.display())
316+
}
317+
}
318+
319+
impl std::error::Error for OpenError {
320+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
321+
Some(&self.source)
322+
}
323+
}
324+
325+
#[derive(Debug)]
326+
enum OpenErrorKind {
327+
OpenDB(std::io::Error),
328+
ReadDB(anyhow::Error),
329+
CreateDB(std::io::Error),
330+
}
331+
332+
impl std::fmt::Display for OpenErrorKind {
333+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
334+
match self {
335+
OpenErrorKind::OpenDB(_) => write!(f, "failed to open"),
336+
OpenErrorKind::ReadDB(_) => write!(f, "failed to read"),
337+
OpenErrorKind::CreateDB(_) => write!(f, "failed to create"),
338+
}
339+
}
340+
}
341+
342+
impl std::error::Error for OpenErrorKind {
343+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
344+
match self {
345+
OpenErrorKind::OpenDB(err) => Some(err),
346+
OpenErrorKind::ReadDB(err) => Some(err.as_ref()),
347+
OpenErrorKind::CreateDB(err) => Some(err),
348+
}
349+
}
350+
}
351+
306352
/// Opens or creates an on-disk database, loading its state into the provided Graph.
307-
pub fn open(path: &Path, graph: &mut Graph, hashes: &mut Hashes) -> anyhow::Result<Writer> {
353+
pub fn open(path: &Path, graph: &mut Graph, hashes: &mut Hashes) -> Result<Writer, OpenError> {
308354
match std::fs::OpenOptions::new()
309355
.read(true)
310356
.append(true)
311357
.open(path)
312358
{
313359
Ok(mut f) => {
314-
let ids = Reader::read(&mut f, graph, hashes)?;
360+
let ids = Reader::read(&mut f, graph, hashes).map_err(|err| OpenError {
361+
path: path.to_path_buf(),
362+
source: OpenErrorKind::ReadDB(err),
363+
})?;
315364
Ok(Writer::from_opened(ids, f))
316365
}
317366
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
318-
let w = Writer::create(path)?;
367+
let w = Writer::create(path).map_err(|err| OpenError {
368+
path: path.to_path_buf(),
369+
source: OpenErrorKind::CreateDB(err),
370+
})?;
319371
Ok(w)
320372
}
321-
Err(err) => Err(anyhow!(err)),
373+
Err(err) => Err(OpenError {
374+
path: path.to_path_buf(),
375+
source: OpenErrorKind::OpenDB(err),
376+
}),
322377
}
323378
}

src/load.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,10 @@ pub fn read(build_filename: &str) -> anyhow::Result<State> {
259259
if let Some(builddir) = &loader.builddir {
260260
db_path = Path::new(&builddir).join(db_path);
261261
if let Some(parent) = db_path.parent() {
262-
std::fs::create_dir_all(parent)?;
262+
std::fs::create_dir_all(parent).map_err(|e| anyhow::anyhow!(e))?;
263263
}
264264
};
265-
db::open(&db_path, &mut loader.graph, &mut hashes)
265+
db::open(&db_path, &mut loader.graph, &mut hashes).map_err(|e| anyhow::anyhow!(e))
266266
})
267267
.map_err(|err| anyhow!("load .n2_db: {}", err))?;
268268
Ok(State {

0 commit comments

Comments
 (0)