From d52c01e26ae20c2136590d77f35c277c6c3d1bff Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Tue, 19 Nov 2024 10:42:37 +0100 Subject: [PATCH] Fix error serialize implementation --- crates/radicle-tauri/src/commands/auth.rs | 3 +- crates/radicle-tauri/src/commands/cob.rs | 14 +-- .../radicle-tauri/src/commands/cob/draft.rs | 3 +- .../radicle-tauri/src/commands/cob/issue.rs | 10 +- .../radicle-tauri/src/commands/cob/patch.rs | 12 +- crates/radicle-tauri/src/commands/diff.rs | 7 +- crates/radicle-tauri/src/commands/repo.rs | 8 +- crates/radicle-tauri/src/commands/thread.rs | 4 +- crates/radicle-tauri/src/error.rs | 113 ------------------ crates/radicle-tauri/src/lib.rs | 8 +- crates/radicle-types/src/error.rs | 30 +++-- 11 files changed, 51 insertions(+), 161 deletions(-) delete mode 100644 crates/radicle-tauri/src/error.rs diff --git a/crates/radicle-tauri/src/commands/auth.rs b/crates/radicle-tauri/src/commands/auth.rs index 07f356f97..3ff3a9569 100644 --- a/crates/radicle-tauri/src/commands/auth.rs +++ b/crates/radicle-tauri/src/commands/auth.rs @@ -1,6 +1,7 @@ +use radicle_types::error::Error; use radicle_types::traits::auth::Auth; -use crate::{error::Error, AppState}; +use crate::AppState; #[tauri::command] pub(crate) fn authenticate(ctx: tauri::State) -> Result<(), Error> { diff --git a/crates/radicle-tauri/src/commands/cob.rs b/crates/radicle-tauri/src/commands/cob.rs index 786a44715..608d93baf 100644 --- a/crates/radicle-tauri/src/commands/cob.rs +++ b/crates/radicle-tauri/src/commands/cob.rs @@ -2,10 +2,11 @@ use radicle::cob; use radicle::git; use radicle::identity; use radicle_types as types; +use radicle_types::error::Error; use radicle_types::traits::cobs::Cobs; use radicle_types::traits::thread::Thread; -use crate::{error, AppState}; +use crate::AppState; pub mod draft; pub mod issue; @@ -16,8 +17,8 @@ pub async fn get_file_by_oid( ctx: tauri::State<'_, AppState>, rid: identity::RepoId, oid: git::Oid, -) -> Result { - ctx.get_embed(rid, oid).map_err(error::Error::from) +) -> Result { + ctx.get_embed(rid, oid) } #[tauri::command] @@ -26,8 +27,8 @@ pub async fn save_embed( rid: identity::RepoId, name: &str, bytes: &[u8], -) -> Result { - ctx.save_embed(rid, name, bytes).map_err(error::Error::from) +) -> Result { + ctx.save_embed(rid, name, bytes) } #[tauri::command] @@ -36,7 +37,6 @@ pub fn activity_by_id( rid: identity::RepoId, type_name: cob::TypeName, id: git::Oid, -) -> Result, error::Error> { +) -> Result, Error> { ctx.activity_by_id(rid, type_name, id) - .map_err(error::Error::from) } diff --git a/crates/radicle-tauri/src/commands/cob/draft.rs b/crates/radicle-tauri/src/commands/cob/draft.rs index a3ae8b7fe..7c4906fd7 100644 --- a/crates/radicle-tauri/src/commands/cob/draft.rs +++ b/crates/radicle-tauri/src/commands/cob/draft.rs @@ -2,9 +2,9 @@ use radicle::cob; use radicle::git; use radicle::identity; +use radicle_types::error::Error; use radicle_types::traits::cobs::Cobs; -use crate::error::Error; use crate::AppState; /// Puts a draft of a Collaborative Object (COB) out of the draft state by updating the reference to the new object ID (OID). @@ -19,5 +19,4 @@ pub fn publish_draft( type_name: cob::TypeName, ) -> Result<(), Error> { ctx.publish_draft(rid, cob_id, type_name) - .map_err(Error::from) } diff --git a/crates/radicle-tauri/src/commands/cob/issue.rs b/crates/radicle-tauri/src/commands/cob/issue.rs index 212a3a33b..8077be3e9 100644 --- a/crates/radicle-tauri/src/commands/cob/issue.rs +++ b/crates/radicle-tauri/src/commands/cob/issue.rs @@ -2,10 +2,10 @@ use radicle::git; use radicle::identity; use radicle_types as types; +use radicle_types::error::Error; use radicle_types::traits::issue::Issues; use radicle_types::traits::issue::IssuesMut; -use crate::error::Error; use crate::AppState; #[tauri::command] @@ -15,7 +15,7 @@ pub fn create_issue( new: types::cobs::issue::NewIssue, opts: types::cobs::CobOptions, ) -> Result { - ctx.create_issue(rid, new, opts).map_err(Error::from) + ctx.create_issue(rid, new, opts) } #[tauri::command] @@ -27,7 +27,6 @@ pub fn edit_issue( opts: types::cobs::CobOptions, ) -> Result { ctx.edit_issue(rid, cob_id, action, opts) - .map_err(Error::from) } #[tauri::command] @@ -36,7 +35,7 @@ pub(crate) fn list_issues( rid: identity::RepoId, status: Option, ) -> Result, Error> { - ctx.list_issues(rid, status).map_err(Error::from) + ctx.list_issues(rid, status) } #[tauri::command] @@ -45,7 +44,7 @@ pub(crate) fn issue_by_id( rid: identity::RepoId, id: git::Oid, ) -> Result, Error> { - ctx.issue_by_id(rid, id).map_err(Error::from) + ctx.issue_by_id(rid, id) } #[tauri::command] @@ -55,5 +54,4 @@ pub(crate) fn comment_threads_by_issue_id( id: git::Oid, ) -> Result>, Error> { ctx.comment_threads_by_issue_id(rid, id) - .map_err(Error::from) } diff --git a/crates/radicle-tauri/src/commands/cob/patch.rs b/crates/radicle-tauri/src/commands/cob/patch.rs index b3e0822c1..38b4874d3 100644 --- a/crates/radicle-tauri/src/commands/cob/patch.rs +++ b/crates/radicle-tauri/src/commands/cob/patch.rs @@ -4,10 +4,10 @@ use radicle::identity; use radicle::patch; use radicle_types as types; +use radicle_types::error::Error; use radicle_types::traits::patch::Patches; use radicle_types::traits::patch::PatchesMut; -use crate::error::Error; use crate::AppState; #[tauri::command] @@ -19,7 +19,6 @@ pub async fn list_patches( take: Option, ) -> Result>, Error> { ctx.list_patches(rid, status, skip, take) - .map_err(Error::from) } #[tauri::command] @@ -28,7 +27,7 @@ pub fn patch_by_id( rid: identity::RepoId, id: git::Oid, ) -> Result, Error> { - ctx.get_patch(rid, id).map_err(Error::from) + ctx.get_patch(rid, id) } #[tauri::command] @@ -37,7 +36,7 @@ pub fn revisions_by_patch( rid: identity::RepoId, id: git::Oid, ) -> Result>, Error> { - ctx.revisions_by_patch(rid, id).map_err(Error::from) + ctx.revisions_by_patch(rid, id) } #[tauri::command] @@ -48,7 +47,6 @@ pub fn revision_by_patch_and_id( revision_id: git::Oid, ) -> Result, Error> { ctx.revision_by_id(rid, id, revision_id) - .map_err(Error::from) } #[tauri::command] @@ -60,7 +58,6 @@ pub fn create_draft_review( labels: Vec, ) -> Result { ctx.create_draft_review(rid, revision_id, cob_id, labels) - .map_err(Error::from) } /// Creates a new review comment on a draft review for a specific patch. @@ -76,7 +73,6 @@ pub fn create_draft_review_comment( new: types::cobs::thread::CreateReviewComment, ) -> Result<(), Error> { ctx.create_draft_review_comment(rid, cob_id, new) - .map_err(Error::from) } /// Edits a draft review for a specific patch revision in a repository. @@ -91,7 +87,6 @@ pub fn edit_draft_review( edit: types::cobs::patch::ReviewEdit, ) -> Result<(), Error> { ctx.edit_draft_review(rid, cob_id, edit) - .map_err(Error::from) } #[tauri::command] @@ -113,5 +108,4 @@ pub fn edit_patch( opts: types::cobs::CobOptions, ) -> Result { ctx.edit_patch(rid, cob_id, action, opts) - .map_err(Error::from) } diff --git a/crates/radicle-tauri/src/commands/diff.rs b/crates/radicle-tauri/src/commands/diff.rs index 0ef291bfb..6028cc114 100644 --- a/crates/radicle-tauri/src/commands/diff.rs +++ b/crates/radicle-tauri/src/commands/diff.rs @@ -1,15 +1,16 @@ use radicle_surf as surf; use radicle::identity; +use radicle_types::error::Error; use radicle_types::traits::repo::Repo; -use crate::{error, AppState}; +use crate::AppState; #[tauri::command] pub async fn get_diff( ctx: tauri::State<'_, AppState>, rid: identity::RepoId, options: radicle_types::cobs::diff::Options, -) -> Result { - ctx.get_diff(rid, options).map_err(error::Error::from) +) -> Result { + ctx.get_diff(rid, options) } diff --git a/crates/radicle-tauri/src/commands/repo.rs b/crates/radicle-tauri/src/commands/repo.rs index c1ffaa87c..a7b4f1dc1 100644 --- a/crates/radicle-tauri/src/commands/repo.rs +++ b/crates/radicle-tauri/src/commands/repo.rs @@ -2,14 +2,14 @@ use radicle::git; use radicle::identity::RepoId; use radicle_types as types; +use radicle_types::error::Error; use radicle_types::traits::repo::Repo; -use crate::error::Error; use crate::AppState; #[tauri::command] pub fn list_repos(ctx: tauri::State) -> Result, Error> { - ctx.list_repos().map_err(Error::from) + ctx.list_repos() } #[tauri::command] @@ -17,7 +17,7 @@ pub fn repo_by_id( ctx: tauri::State, rid: RepoId, ) -> Result { - ctx.repo_by_id(rid).map_err(Error::from) + ctx.repo_by_id(rid) } #[tauri::command] @@ -27,5 +27,5 @@ pub async fn diff_stats( base: git::Oid, head: git::Oid, ) -> Result { - ctx.diff_stats(rid, base, head).map_err(Error::from) + ctx.diff_stats(rid, base, head) } diff --git a/crates/radicle-tauri/src/commands/thread.rs b/crates/radicle-tauri/src/commands/thread.rs index f1ff354b3..016bf56c2 100644 --- a/crates/radicle-tauri/src/commands/thread.rs +++ b/crates/radicle-tauri/src/commands/thread.rs @@ -1,9 +1,9 @@ use radicle::identity; use radicle_types as types; +use radicle_types::error::Error; use radicle_types::traits::thread::Thread; -use crate::error::Error; use crate::AppState; #[tauri::command] @@ -14,7 +14,6 @@ pub fn create_issue_comment( opts: types::cobs::CobOptions, ) -> Result, Error> { ctx.create_issue_comment(rid, new, opts) - .map_err(Error::from) } #[tauri::command] @@ -25,5 +24,4 @@ pub fn create_patch_comment( opts: types::cobs::CobOptions, ) -> Result, Error> { ctx.create_patch_comment(rid, new, opts) - .map_err(Error::from) } diff --git a/crates/radicle-tauri/src/error.rs b/crates/radicle-tauri/src/error.rs deleted file mode 100644 index a5400fe6f..000000000 --- a/crates/radicle-tauri/src/error.rs +++ /dev/null @@ -1,113 +0,0 @@ -use serde::Serialize; - -/// Errors relating to the API backend. -#[derive(Debug, thiserror::Error)] -pub enum Error { - /// Profile error. - #[error(transparent)] - Profile(#[from] radicle::profile::Error), - - /// Node database error. - #[error(transparent)] - Database(#[from] radicle::node::db::Error), - - /// Policy store error. - #[error(transparent)] - PolicyStore(#[from] radicle::node::policy::store::Error), - - /// Cob patch cache error. - #[error(transparent)] - CachePatch(#[from] radicle::cob::patch::cache::Error), - - /// Cob issue cache error. - #[error(transparent)] - CacheIssue(#[from] radicle::cob::issue::cache::Error), - - /// Repository error. - #[error(transparent)] - Repository(#[from] radicle::storage::RepositoryError), - - /// Crypto error. - #[error(transparent)] - Crypto(#[from] radicle::crypto::ssh::keystore::Error), - - /// SSH Agent error. - #[error(transparent)] - Agent(#[from] radicle::crypto::ssh::agent::Error), - - /// Radicle Git error. - #[error(transparent)] - Git(#[from] radicle::git::Error), - - /// Git2 error. - #[error(transparent)] - Git2(#[from] radicle::git::raw::Error), - - /// Diff error. - #[error(transparent)] - Diff(#[from] radicle_surf::diff::git::error::Diff), - - /// Storage error. - #[error(transparent)] - Storage(#[from] radicle::storage::Error), - - /// Types error. - #[error(transparent)] - Types(#[from] radicle_types::error::Error), - - /// Surf error. - #[error(transparent)] - Surf(#[from] radicle_surf::Error), - - /// Issue error. - #[error(transparent)] - Issue(#[from] radicle::issue::Error), - - /// Patch error. - #[error(transparent)] - Patch(#[from] radicle::patch::Error), - - /// An error with a hint. - #[error("{err} {hint}")] - WithHint { - err: anyhow::Error, - hint: &'static str, - }, - - /// Node error. - #[error(transparent)] - Node(#[from] radicle::node::Error), -} - -#[derive(Serialize)] -struct ErrorWrapperWithHint { - err: String, - hint: String, -} - -#[derive(Serialize)] -struct ErrorWrapper { - err: String, -} - -impl Serialize for Error { - fn serialize(&self, serializer: S) -> Result - where - S: serde::ser::Serializer, - { - if let Error::WithHint { err, hint } = self { - let error_wrapper = ErrorWrapperWithHint { - err: err.to_string(), - hint: hint.to_string(), - }; - - return error_wrapper.serialize(serializer); - } - - let wrapper = ErrorWrapper { - err: self.to_string(), - }; - - wrapper.serialize(serializer) - } -} diff --git a/crates/radicle-tauri/src/lib.rs b/crates/radicle-tauri/src/lib.rs index ef61f8655..551f0a476 100644 --- a/crates/radicle-tauri/src/lib.rs +++ b/crates/radicle-tauri/src/lib.rs @@ -1,11 +1,11 @@ mod commands; -mod error; use tauri::Emitter; use tauri::Manager; use radicle::node::Handle; use radicle::Node; +use radicle_types::error::Error; use radicle_types::traits::auth::Auth; use radicle_types::traits::cobs::Cobs; use radicle_types::traits::issue::{Issues, IssuesMut}; @@ -44,12 +44,12 @@ pub fn run() { builder .setup(|app| { let profile: radicle::Profile = match radicle::Profile::load() { - Ok(profile) => Ok::(profile), - Err(radicle::profile::Error::NotFound(path)) => Err(error::Error::WithHint { + Ok(profile) => Ok::(profile), + Err(radicle::profile::Error::NotFound(path)) => Err(Error::WithHint { err: anyhow::anyhow!("Radicle profile not found in '{}'.", path.display()), hint: "To setup your radicle profile, run `rad auth`.", }), - Err(e) => Err(error::Error::WithHint { + Err(e) => Err(Error::WithHint { err: e.into(), hint: "Could not load radicle profile", }), diff --git a/crates/radicle-types/src/error.rs b/crates/radicle-types/src/error.rs index a4f83f541..e7ea7624a 100644 --- a/crates/radicle-types/src/error.rs +++ b/crates/radicle-types/src/error.rs @@ -94,19 +94,31 @@ impl Serialize for Error { where S: serde::ser::Serializer, { - if let Error::WithHint { err, hint } = self { - let error_wrapper = ErrorWrapperWithHint { + match self { + Error::WithHint { err, hint } => ErrorWrapperWithHint { err: err.to_string(), hint: hint.to_string(), - }; - - return error_wrapper.serialize(serializer); + } + .serialize(serializer), + err => ErrorWrapper { + err: err.to_string(), + } + .serialize(serializer), } + } +} - let wrapper = ErrorWrapper { - err: self.to_string(), - }; +#[cfg(test)] +#[allow(clippy::unwrap_used)] +mod test { + use super::Error; + use anyhow::anyhow; - wrapper.serialize(serializer) + #[test] + fn serialize_errors() { + assert_eq!(serde_json::to_string(&Error::WithHint { + err: anyhow!("Not able to find your keys in the ssh agent"), + hint: "Make sure to run rad auth in your terminal to add your keys to the ssh-agent.", + }).unwrap(),"{\"err\":\"Not able to find your keys in the ssh agent\",\"hint\":\"Make sure to run rad auth in your terminal to add your keys to the ssh-agent.\"}"); } }