Skip to content
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

improve error message #1

Merged
merged 1 commit into from
Sep 6, 2024
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
65 changes: 60 additions & 5 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ use crate::{
densemap, densemap::DenseMap, graph::BuildId, graph::FileId, graph::Graph, graph::Hashes,
hash::BuildHash,
};
use anyhow::{anyhow, bail};
use anyhow::bail;
use std::collections::HashMap;
use std::fs::File;
use std::io::BufReader;
use std::io::Read;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;

const VERSION: u32 = 1;

Expand Down Expand Up @@ -303,21 +304,75 @@ impl<'a> Reader<'a> {
}
}

#[derive(Debug)]
pub struct OpenError {
path: PathBuf,
source: OpenErrorKind,
}

impl std::fmt::Display for OpenError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "failed to open {}", self.path.display())
}
}

impl std::error::Error for OpenError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.source)
}
}

#[derive(Debug)]
enum OpenErrorKind {
OpenDB(std::io::Error),
ReadDB(anyhow::Error),
CreateDB(std::io::Error),
}

impl std::fmt::Display for OpenErrorKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
OpenErrorKind::OpenDB(_) => write!(f, "failed to open"),
OpenErrorKind::ReadDB(_) => write!(f, "failed to read"),
OpenErrorKind::CreateDB(_) => write!(f, "failed to create"),
}
}
}

impl std::error::Error for OpenErrorKind {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
OpenErrorKind::OpenDB(err) => Some(err),
OpenErrorKind::ReadDB(err) => Some(err.as_ref()),
OpenErrorKind::CreateDB(err) => Some(err),
}
}
}

/// Opens or creates an on-disk database, loading its state into the provided Graph.
pub fn open(path: &Path, graph: &mut Graph, hashes: &mut Hashes) -> anyhow::Result<Writer> {
pub fn open(path: &Path, graph: &mut Graph, hashes: &mut Hashes) -> Result<Writer, OpenError> {
match std::fs::OpenOptions::new()
.read(true)
.append(true)
.open(path)
{
Ok(mut f) => {
let ids = Reader::read(&mut f, graph, hashes)?;
let ids = Reader::read(&mut f, graph, hashes).map_err(|err| OpenError {
path: path.to_path_buf(),
source: OpenErrorKind::ReadDB(err),
})?;
Ok(Writer::from_opened(ids, f))
}
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
let w = Writer::create(path)?;
let w = Writer::create(path).map_err(|err| OpenError {
path: path.to_path_buf(),
source: OpenErrorKind::CreateDB(err),
})?;
Ok(w)
}
Err(err) => Err(anyhow!(err)),
Err(err) => Err(OpenError {
path: path.to_path_buf(),
source: OpenErrorKind::OpenDB(err),
}),
}
}
4 changes: 2 additions & 2 deletions src/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,10 @@ pub fn read(build_filename: &str) -> anyhow::Result<State> {
if let Some(builddir) = &loader.builddir {
db_path = Path::new(&builddir).join(db_path);
if let Some(parent) = db_path.parent() {
std::fs::create_dir_all(parent)?;
std::fs::create_dir_all(parent).map_err(|e| anyhow::anyhow!(e))?;
}
};
db::open(&db_path, &mut loader.graph, &mut hashes)
db::open(&db_path, &mut loader.graph, &mut hashes).map_err(|e| anyhow::anyhow!(e))
})
.map_err(|err| anyhow!("load .n2_db: {}", err))?;
Ok(State {
Expand Down
Loading