radicle: Switch error type for repository methods
This simplifies error handling for the next commit.
This commit is contained in:
parent
2b9a014243
commit
991505ec9d
|
|
@ -129,6 +129,9 @@ impl IntoResponse for Error {
|
|||
Error::Storage(err) if err.is_not_found() => {
|
||||
(StatusCode::NOT_FOUND, Some(err.to_string()))
|
||||
}
|
||||
Error::Repository(err) if err.is_not_found() => {
|
||||
(StatusCode::NOT_FOUND, Some(err.to_string()))
|
||||
}
|
||||
Error::StorageRef(err) if err.is_not_found() => {
|
||||
(StatusCode::NOT_FOUND, Some(err.to_string()))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -54,9 +54,9 @@ pub enum Error {
|
|||
/// Invalid reference name.
|
||||
#[error("invalid ref: {0}")]
|
||||
InvalidRef(#[from] radicle::git::fmt::Error),
|
||||
/// Storage error.
|
||||
/// Repository error.
|
||||
#[error(transparent)]
|
||||
Storage(#[from] radicle::storage::Error),
|
||||
Repository(#[from] radicle::storage::RepositoryError),
|
||||
/// Fetch error.
|
||||
#[error(transparent)]
|
||||
Fetch(#[from] fetch::Error),
|
||||
|
|
|
|||
|
|
@ -105,6 +105,17 @@ pub enum RepositoryError {
|
|||
Refs(#[from] refs::Error),
|
||||
}
|
||||
|
||||
impl RepositoryError {
|
||||
pub fn is_not_found(&self) -> bool {
|
||||
match self {
|
||||
Self::Storage(e) if e.is_not_found() => true,
|
||||
Self::Git(e) if git_ext::is_not_found_err(e) => true,
|
||||
Self::GitExt(git_ext::Error::NotFound(_)) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Storage error.
|
||||
#[derive(Error, Debug)]
|
||||
pub enum Error {
|
||||
|
|
@ -377,7 +388,7 @@ pub trait ReadStorage {
|
|||
/// Insert this repository into the inventory.
|
||||
fn insert(&self, rid: RepoId);
|
||||
/// Open or create a read-only repository.
|
||||
fn repository(&self, rid: RepoId) -> Result<Self::Repository, Error>;
|
||||
fn repository(&self, rid: RepoId) -> Result<Self::Repository, RepositoryError>;
|
||||
/// Get a repository's identity if it exists.
|
||||
fn get(&self, rid: RepoId) -> Result<Option<Doc<Verified>>, RepositoryError> {
|
||||
match self.repository(rid) {
|
||||
|
|
@ -393,7 +404,7 @@ pub trait WriteStorage: ReadStorage {
|
|||
type RepositoryMut: WriteRepository;
|
||||
|
||||
/// Open a read-write repository.
|
||||
fn repository_mut(&self, rid: RepoId) -> Result<Self::RepositoryMut, Error>;
|
||||
fn repository_mut(&self, rid: RepoId) -> Result<Self::RepositoryMut, RepositoryError>;
|
||||
/// Create a read-write repository.
|
||||
fn create(&self, rid: RepoId) -> Result<Self::RepositoryMut, Error>;
|
||||
|
||||
|
|
@ -642,7 +653,7 @@ where
|
|||
self.deref().get(rid)
|
||||
}
|
||||
|
||||
fn repository(&self, rid: RepoId) -> Result<Self::Repository, Error> {
|
||||
fn repository(&self, rid: RepoId) -> Result<Self::Repository, RepositoryError> {
|
||||
self.deref().repository(rid)
|
||||
}
|
||||
}
|
||||
|
|
@ -654,7 +665,7 @@ where
|
|||
{
|
||||
type RepositoryMut = S::RepositoryMut;
|
||||
|
||||
fn repository_mut(&self, rid: RepoId) -> Result<Self::RepositoryMut, Error> {
|
||||
fn repository_mut(&self, rid: RepoId) -> Result<Self::RepositoryMut, RepositoryError> {
|
||||
self.deref().repository_mut(rid)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -145,7 +145,7 @@ impl ReadStorage for Storage {
|
|||
}
|
||||
}
|
||||
|
||||
fn repository(&self, rid: RepoId) -> Result<Self::Repository, Error> {
|
||||
fn repository(&self, rid: RepoId) -> Result<Self::Repository, RepositoryError> {
|
||||
Repository::open(paths::repository(self, &rid), rid)
|
||||
}
|
||||
}
|
||||
|
|
@ -153,7 +153,7 @@ impl ReadStorage for Storage {
|
|||
impl WriteStorage for Storage {
|
||||
type RepositoryMut = Repository;
|
||||
|
||||
fn repository_mut(&self, rid: RepoId) -> Result<Self::RepositoryMut, Error> {
|
||||
fn repository_mut(&self, rid: RepoId) -> Result<Self::RepositoryMut, RepositoryError> {
|
||||
Repository::open(paths::repository(self, &rid), rid)
|
||||
}
|
||||
|
||||
|
|
@ -295,7 +295,7 @@ impl Storage {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn inspect(&self) -> Result<(), Error> {
|
||||
pub fn inspect(&self) -> Result<(), RepositoryError> {
|
||||
for r in self.repositories()? {
|
||||
let rid = r.rid;
|
||||
let repo = self.repository(rid)?;
|
||||
|
|
@ -386,7 +386,7 @@ pub enum Validation {
|
|||
|
||||
impl Repository {
|
||||
/// Open an existing repository.
|
||||
pub fn open<P: AsRef<Path>>(path: P, id: RepoId) -> Result<Self, Error> {
|
||||
pub fn open<P: AsRef<Path>>(path: P, id: RepoId) -> Result<Self, RepositoryError> {
|
||||
let backend = git2::Repository::open_bare(path.as_ref())?;
|
||||
|
||||
Ok(Self { id, backend })
|
||||
|
|
|
|||
|
|
@ -81,10 +81,12 @@ impl ReadStorage for MockStorage {
|
|||
|
||||
fn insert(&self, _rid: RepoId) {}
|
||||
|
||||
fn repository(&self, rid: RepoId) -> Result<Self::Repository, Error> {
|
||||
fn repository(&self, rid: RepoId) -> Result<Self::Repository, RepositoryError> {
|
||||
self.repos
|
||||
.get(&rid)
|
||||
.ok_or_else(|| Error::Io(io::Error::from(io::ErrorKind::NotFound)))
|
||||
.ok_or_else(|| {
|
||||
RepositoryError::Storage(Error::Io(io::Error::from(io::ErrorKind::NotFound)))
|
||||
})
|
||||
.cloned()
|
||||
}
|
||||
}
|
||||
|
|
@ -92,10 +94,12 @@ impl ReadStorage for MockStorage {
|
|||
impl WriteStorage for MockStorage {
|
||||
type RepositoryMut = MockRepository;
|
||||
|
||||
fn repository_mut(&self, rid: RepoId) -> Result<Self::RepositoryMut, Error> {
|
||||
fn repository_mut(&self, rid: RepoId) -> Result<Self::RepositoryMut, RepositoryError> {
|
||||
self.repos
|
||||
.get(&rid)
|
||||
.ok_or(Error::Io(io::ErrorKind::NotFound.into()))
|
||||
.ok_or(RepositoryError::Storage(Error::Io(io::Error::from(
|
||||
io::ErrorKind::NotFound,
|
||||
))))
|
||||
.cloned()
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue