Fix error serialize implementation

This commit is contained in:
Sebastian Martinez
2024-11-19 10:42:37 +01:00
parent 9ff061793d
commit d52c01e26a
11 changed files with 51 additions and 161 deletions
+2 -1
View File
@@ -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<AppState>) -> Result<(), Error> {
+7 -7
View File
@@ -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<String, error::Error> {
ctx.get_embed(rid, oid).map_err(error::Error::from)
) -> Result<String, Error> {
ctx.get_embed(rid, oid)
}
#[tauri::command]
@@ -26,8 +27,8 @@ pub async fn save_embed(
rid: identity::RepoId,
name: &str,
bytes: &[u8],
) -> Result<git::Oid, error::Error> {
ctx.save_embed(rid, name, bytes).map_err(error::Error::from)
) -> Result<git::Oid, Error> {
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<Vec<types::cobs::issue::Operation>, error::Error> {
) -> Result<Vec<types::cobs::issue::Operation>, Error> {
ctx.activity_by_id(rid, type_name, id)
.map_err(error::Error::from)
}
@@ -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)
}
@@ -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<types::cobs::issue::Issue, Error> {
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<types::cobs::issue::Issue, Error> {
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<types::cobs::query::IssueStatus>,
) -> Result<Vec<types::cobs::issue::Issue>, 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<Option<types::cobs::issue::Issue>, 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<Option<Vec<types::cobs::thread::Thread>>, Error> {
ctx.comment_threads_by_issue_id(rid, id)
.map_err(Error::from)
}
@@ -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<usize>,
) -> Result<types::cobs::PaginatedQuery<Vec<types::cobs::patch::Patch>>, 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<Option<types::cobs::patch::Patch>, 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<Option<Vec<types::cobs::patch::Revision>>, 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<Option<types::cobs::patch::Revision>, 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<cob::Label>,
) -> Result<patch::ReviewId, Error> {
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<types::cobs::patch::Patch, Error> {
ctx.edit_patch(rid, cob_id, action, opts)
.map_err(Error::from)
}
+4 -3
View File
@@ -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<surf::diff::Diff, error::Error> {
ctx.get_diff(rid, options).map_err(error::Error::from)
) -> Result<surf::diff::Diff, Error> {
ctx.get_diff(rid, options)
}
+4 -4
View File
@@ -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<AppState>) -> Result<Vec<types::repo::RepoInfo>, 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<AppState>,
rid: RepoId,
) -> Result<types::repo::RepoInfo, Error> {
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<types::cobs::Stats, Error> {
ctx.diff_stats(rid, base, head).map_err(Error::from)
ctx.diff_stats(rid, base, head)
}
+1 -3
View File
@@ -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<types::cobs::thread::Comment<types::cobs::Never>, 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<types::cobs::thread::Comment<types::cobs::thread::CodeLocation>, Error> {
ctx.create_patch_comment(rid, new, opts)
.map_err(Error::from)
}
-113
View File
@@ -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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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)
}
}
+4 -4
View File
@@ -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::<radicle::Profile, error::Error>(profile),
Err(radicle::profile::Error::NotFound(path)) => Err(error::Error::WithHint {
Ok(profile) => Ok::<radicle::Profile, Error>(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",
}),
+21 -9
View File
@@ -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 <code>rad auth</code> 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 <code>rad auth</code> in your terminal to add your keys to the ssh-agent.\"}");
}
}