cob: Make errors more consistent across COBs

Though these are perhaps not the final error types, we name them the same
across COBs and have them work the same way.

All COB modules now have a `Error` enum.

In a future change, we may want the `FromHistory` trait to support
multiple error types, eg. one for `apply` and one for `validate`.
This commit is contained in:
Alexis Sellier 2023-05-29 11:33:29 +02:00
parent eeaf380067
commit aa8394a838
No known key found for this signature in database
5 changed files with 21 additions and 27 deletions

View File

@ -92,7 +92,7 @@ pub enum ApplyError {
Redacted(EntryId), Redacted(EntryId),
/// Error applying an op to the proposal thread. /// Error applying an op to the proposal thread.
#[error("thread apply failed: {0}")] #[error("thread apply failed: {0}")]
Thread(#[from] thread::OpError), Thread(#[from] thread::Error),
/// Error validating the state. /// Error validating the state.
#[error("validation failed: {0}")] #[error("validation failed: {0}")]
Validate(&'static str), Validate(&'static str),

View File

@ -39,7 +39,7 @@ pub enum Error {
#[error("description missing")] #[error("description missing")]
DescriptionMissing, DescriptionMissing,
#[error("thread apply failed: {0}")] #[error("thread apply failed: {0}")]
Thread(#[from] thread::OpError), Thread(#[from] thread::Error),
#[error("store: {0}")] #[error("store: {0}")]
Store(#[from] store::Error), Store(#[from] store::Error),
} }

View File

@ -50,7 +50,7 @@ pub type RevisionIx = usize;
/// Error applying an operation onto a state. /// Error applying an operation onto a state.
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum ApplyError { pub enum Error {
/// Causal dependency missing. /// Causal dependency missing.
/// ///
/// This error indicates that the operations are not being applied /// This error indicates that the operations are not being applied
@ -62,7 +62,7 @@ pub enum ApplyError {
Missing(EntryId), Missing(EntryId),
/// Error applying an op to the patch thread. /// Error applying an op to the patch thread.
#[error("thread apply failed: {0}")] #[error("thread apply failed: {0}")]
Thread(#[from] thread::OpError), Thread(#[from] thread::Error),
/// Error loading the identity document committed to by an operation. /// Error loading the identity document committed to by an operation.
#[error("identity doc failed to load: {0}")] #[error("identity doc failed to load: {0}")]
Doc(#[from] DocError), Doc(#[from] DocError),
@ -78,13 +78,7 @@ pub enum ApplyError {
/// Validation error. /// Validation error.
#[error("validation failed: {0}")] #[error("validation failed: {0}")]
Validate(&'static str), Validate(&'static str),
} /// Store error.
/// Error updating or creating patches.
#[derive(Error, Debug)]
pub enum Error {
#[error("apply failed: {0}")]
Apply(#[from] ApplyError),
#[error("store: {0}")] #[error("store: {0}")]
Store(#[from] store::Error), Store(#[from] store::Error),
} }
@ -341,7 +335,7 @@ impl Patch {
impl store::FromHistory for Patch { impl store::FromHistory for Patch {
type Action = Action; type Action = Action;
type Error = ApplyError; type Error = Error;
fn type_name() -> &'static TypeName { fn type_name() -> &'static TypeName {
&*TYPENAME &*TYPENAME
@ -349,10 +343,10 @@ impl store::FromHistory for Patch {
fn validate(&self) -> Result<(), Self::Error> { fn validate(&self) -> Result<(), Self::Error> {
if self.revisions.is_empty() { if self.revisions.is_empty() {
return Err(ApplyError::Validate("no revisions found")); return Err(Error::Validate("no revisions found"));
} }
if self.title().is_empty() { if self.title().is_empty() {
return Err(ApplyError::Validate("empty title")); return Err(Error::Validate("empty title"));
} }
Ok(()) Ok(())
} }
@ -361,7 +355,7 @@ impl store::FromHistory for Patch {
&mut self, &mut self,
ops: impl IntoIterator<Item = Op>, ops: impl IntoIterator<Item = Op>,
repo: &R, repo: &R,
) -> Result<(), ApplyError> { ) -> Result<(), Error> {
for op in ops { for op in ops {
let id = op.id; let id = op.id;
let author = Author::new(op.author); let author = Author::new(op.author);
@ -392,7 +386,7 @@ impl store::FromHistory for Patch {
if let Some(Redactable::Present(revision)) = self.revisions.get_mut(&revision) { if let Some(Redactable::Present(revision)) = self.revisions.get_mut(&revision) {
revision.description.set(description, op.clock); revision.description.set(description, op.clock);
} else { } else {
return Err(ApplyError::Missing(revision)); return Err(Error::Missing(revision));
} }
} }
Action::Revision { Action::Revision {
@ -422,7 +416,7 @@ impl store::FromHistory for Patch {
if let Some(revision) = self.revisions.get_mut(&revision) { if let Some(revision) = self.revisions.get_mut(&revision) {
revision.merge(Redactable::Redacted); revision.merge(Redactable::Redacted);
} else { } else {
return Err(ApplyError::Missing(revision)); return Err(Error::Missing(revision));
} }
} }
Action::Review { Action::Review {
@ -443,7 +437,7 @@ impl store::FromHistory for Patch {
), ),
); );
} else { } else {
return Err(ApplyError::Missing(revision)); return Err(Error::Missing(revision));
} }
} }
Action::Merge { revision, commit } => { Action::Merge { revision, commit } => {
@ -453,7 +447,7 @@ impl store::FromHistory for Patch {
match self.target() { match self.target() {
MergeTarget::Delegates => { MergeTarget::Delegates => {
if !doc.is_delegate(&op.author) { if !doc.is_delegate(&op.author) {
return Err(ApplyError::InvalidMerge(op.id)); return Err(Error::InvalidMerge(op.id));
} }
let proj = doc.project()?; let proj = doc.project()?;
let branch = git::refs::branch(proj.default_branch()); let branch = git::refs::branch(proj.default_branch());
@ -519,7 +513,7 @@ impl store::FromHistory for Patch {
} }
} }
} else { } else {
return Err(ApplyError::Missing(revision)); return Err(Error::Missing(revision));
} }
} }
Action::Thread { revision, action } => { Action::Thread { revision, action } => {
@ -538,7 +532,7 @@ impl store::FromHistory for Patch {
repo, repo,
)?; )?;
} else { } else {
return Err(ApplyError::Missing(revision)); return Err(Error::Missing(revision));
} }
} }
} }

View File

@ -192,7 +192,7 @@ impl<G: Signer> Actor<G, super::patch::Action> {
base: git::Oid, base: git::Oid,
oid: git::Oid, oid: git::Oid,
repo: &R, repo: &R,
) -> Result<Patch, patch::ApplyError> { ) -> Result<Patch, patch::Error> {
Patch::from_ops( Patch::from_ops(
[ [
self.op(patch::Action::Revision { self.op(patch::Action::Revision {

View File

@ -21,7 +21,7 @@ pub static TYPENAME: Lazy<cob::TypeName> =
/// Error applying an operation onto a state. /// Error applying an operation onto a state.
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum OpError { pub enum Error {
/// Causal dependency missing. /// Causal dependency missing.
/// ///
/// This error indicates that the operations are not being applied /// This error indicates that the operations are not being applied
@ -261,7 +261,7 @@ impl Thread {
impl cob::store::FromHistory for Thread { impl cob::store::FromHistory for Thread {
type Action = Action; type Action = Action;
type Error = OpError; type Error = Error;
fn type_name() -> &'static radicle_cob::TypeName { fn type_name() -> &'static radicle_cob::TypeName {
&*TYPENAME &*TYPENAME
@ -269,7 +269,7 @@ impl cob::store::FromHistory for Thread {
fn validate(&self) -> Result<(), Self::Error> { fn validate(&self) -> Result<(), Self::Error> {
if self.comments.is_empty() { if self.comments.is_empty() {
return Err(OpError::Validate("no comments found")); return Err(Error::Validate("no comments found"));
} }
Ok(()) Ok(())
} }
@ -278,7 +278,7 @@ impl cob::store::FromHistory for Thread {
&mut self, &mut self,
ops: impl IntoIterator<Item = Op<Action>>, ops: impl IntoIterator<Item = Op<Action>>,
_repo: &R, _repo: &R,
) -> Result<(), OpError> { ) -> Result<(), Error> {
for op in ops.into_iter() { for op in ops.into_iter() {
let id = op.id; let id = op.id;
let author = op.author; let author = op.author;
@ -304,7 +304,7 @@ impl cob::store::FromHistory for Thread {
if let Some(Redactable::Present(comment)) = self.comments.get_mut(&id) { if let Some(Redactable::Present(comment)) = self.comments.get_mut(&id) {
comment.edit(op.clock, body, timestamp); comment.edit(op.clock, body, timestamp);
} else { } else {
return Err(OpError::Missing(id)); return Err(Error::Missing(id));
} }
} }
Action::Redact { id } => { Action::Redact { id } => {