Merge pull request #1440 from gitbutlerapp/validation-code

add validation error code
This commit is contained in:
Nikita Galaiko 2023-10-23 11:35:40 +02:00 committed by GitHub
commit e54f29b0f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 74 additions and 54 deletions

View File

@ -74,5 +74,7 @@ allow = [
"clippy::inconsistent_struct_constructor",
"clippy::match_wildcard_for_single_variants",
"clippy::unnested_or_patterns",
"clippy::similar_names"
"clippy::similar_names",
# TODO: fixme
"clippy::struct_field_names"
]

View File

@ -35,7 +35,7 @@ pub async fn list_sessions(
) -> Result<Vec<sessions::Session>, Error> {
let app = handle.state::<app::App>();
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
let sessions = app.list_sessions(&project_id, earliest_timestamp_ms)?;
@ -53,10 +53,10 @@ pub async fn list_session_files(
let app = handle.state::<app::App>();
let session_id: SessionId = session_id.parse().map_err(|_| Error::UserError {
message: "Malformed session id".to_string(),
code: Code::Sessions,
code: Code::Validation,
})?;
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
let files = app.list_session_files(&project_id, &session_id, &paths)?;
@ -74,10 +74,10 @@ pub async fn list_deltas(
let app = handle.state::<app::App>();
let session_id = session_id.parse().map_err(|_| Error::UserError {
message: "Malformed session id".to_string(),
code: Code::Sessions,
code: Code::Validation,
})?;
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
let deltas = app.list_session_deltas(&project_id, &session_id, &paths)?;
@ -93,7 +93,7 @@ pub async fn git_wd_diff(
) -> Result<HashMap<path::PathBuf, String>, Error> {
let app = handle.state::<app::App>();
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
let diff = app.git_wd_diff(&project_id, context_lines)?;
@ -108,7 +108,7 @@ pub async fn git_remote_branches(
) -> Result<Vec<git::RemoteBranchName>, Error> {
let app = handle.state::<app::App>();
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
let branches = app.git_remote_branches(&project_id)?;
@ -123,7 +123,7 @@ pub async fn git_remote_branches_data(
) -> Result<Vec<virtual_branches::RemoteBranch>, Error> {
let app = handle.state::<app::App>();
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
let branches = app.git_remote_branches_data(&project_id)?;
@ -139,7 +139,7 @@ pub async fn git_remote_branches_data(
pub async fn git_head(handle: tauri::AppHandle, project_id: &str) -> Result<String, Error> {
let app = handle.state::<app::App>();
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
let head = app.git_head(&project_id)?;
@ -169,7 +169,7 @@ pub async fn upsert_bookmark(
.context("failed to get time")?
.as_millis();
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
let bookmark = bookmarks::Bookmark {
@ -195,7 +195,7 @@ pub async fn list_bookmarks(
) -> Result<Vec<bookmarks::Bookmark>, Error> {
let app = handle.state::<app::App>();
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
let bookmarks = app.list_bookmarks(&project_id, range)?;
@ -207,7 +207,7 @@ pub async fn list_bookmarks(
pub async fn fetch_from_target(handle: tauri::AppHandle, project_id: &str) -> Result<(), Error> {
let app = handle.state::<app::App>();
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
app.fetch_from_target(&project_id)?;
@ -223,7 +223,7 @@ pub async fn mark_resolved(
) -> Result<(), Error> {
let app = handle.state::<app::App>();
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
app.mark_resolved(&project_id, path)?;

View File

@ -5,8 +5,7 @@ use serde::{ser::SerializeMap, Serialize};
#[derive(Debug)]
pub enum Code {
Unknown,
Sessions,
Branches,
Validation,
Projects,
ProjectGitAuth,
ProjectGitRemote,
@ -18,8 +17,7 @@ impl fmt::Display for Code {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Code::Unknown => write!(f, "errors.unknown"),
Code::Branches => write!(f, "errors.branches"),
Code::Sessions => write!(f, "errors.sessions"),
Code::Validation => write!(f, "errors.validation"),
Code::Projects => write!(f, "errors.projects"),
Code::ProjectGitAuth => write!(f, "errors.projects.git.auth"),
Code::ProjectGitRemote => write!(f, "errors.projects.git.remote"),

View File

@ -92,7 +92,7 @@ impl From<controller::GetError> for Error {
#[instrument(skip(handle))]
pub async fn get_project(handle: tauri::AppHandle, id: &str) -> Result<projects::Project, Error> {
let id = id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".into(),
})?;
handle.state::<Controller>().get(&id).map_err(Into::into)
@ -130,7 +130,7 @@ impl From<controller::DeleteError> for Error {
#[instrument(skip(handle))]
pub async fn delete_project(handle: tauri::AppHandle, id: &str) -> Result<(), Error> {
let id = id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".into(),
})?;
handle.state::<Controller>().delete(&id).map_err(Into::into)

View File

@ -9,7 +9,7 @@ use crate::{
};
use super::{
branch::{BranchId, Ownership},
branch::BranchId,
controller::{self, Controller},
RemoteBranchFile,
};
@ -40,16 +40,23 @@ pub async fn commit_virtual_branch(
project_id: &str,
branch: &str,
message: &str,
ownership: Option<Ownership>,
ownership: Option<&str>,
) -> Result<git::Oid, Error> {
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
let branch_id = branch.parse().map_err(|_| Error::UserError {
code: Code::Branches,
code: Code::Validation,
message: "Malformed branch id".to_string(),
})?;
let ownership = ownership
.map(str::parse)
.transpose()
.map_err(|_| Error::UserError {
code: Code::Validation,
message: "Malformed ownership".to_string(),
})?;
handle
.state::<Controller>()
.create_commit(&project_id, &branch_id, message, ownership.as_ref())
@ -64,7 +71,7 @@ pub async fn list_virtual_branches(
project_id: &str,
) -> Result<Vec<super::VirtualBranch>, Error> {
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
let branches = handle
@ -85,7 +92,7 @@ pub async fn create_virtual_branch(
branch: super::branch::BranchCreateRequest,
) -> Result<BranchId, Error> {
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
handle
@ -103,7 +110,7 @@ pub async fn create_virtual_branch_from_branch(
branch: git::BranchName,
) -> Result<BranchId, Error> {
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
handle
@ -121,11 +128,11 @@ pub async fn merge_virtual_branch_upstream(
branch: &str,
) -> Result<(), Error> {
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
let branch_id = branch.parse().map_err(|_| Error::UserError {
code: Code::Branches,
code: Code::Validation,
message: "Malformed branch id".to_string(),
})?;
handle
@ -142,7 +149,7 @@ pub async fn get_base_branch_data(
project_id: &str,
) -> Result<Option<super::BaseBranch>, Error> {
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
if let Some(base_branch) = handle
@ -165,7 +172,7 @@ pub async fn set_base_branch(
branch: &str,
) -> Result<super::BaseBranch, Error> {
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
let branch_name = format!("refs/remotes/{}", branch)
@ -185,7 +192,7 @@ pub async fn set_base_branch(
#[instrument(skip(handle))]
pub async fn update_base_branch(handle: AppHandle, project_id: &str) -> Result<(), Error> {
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".into(),
})?;
handle
@ -203,7 +210,7 @@ pub async fn update_virtual_branch(
branch: super::branch::BranchUpdateRequest,
) -> Result<(), Error> {
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
handle
@ -221,11 +228,11 @@ pub async fn delete_virtual_branch(
branch_id: &str,
) -> Result<(), Error> {
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
let branch_id = branch_id.parse().map_err(|_| Error::UserError {
code: Code::Branches,
code: Code::Validation,
message: "Malformed branch id".to_string(),
})?;
handle
@ -239,11 +246,11 @@ pub async fn delete_virtual_branch(
#[instrument(skip(handle))]
pub async fn apply_branch(handle: AppHandle, project_id: &str, branch: &str) -> Result<(), Error> {
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
let branch_id = branch.parse().map_err(|_| Error::UserError {
code: Code::Branches,
code: Code::Validation,
message: "Malformed branch id".to_string(),
})?;
handle
@ -261,11 +268,11 @@ pub async fn unapply_branch(
branch: &str,
) -> Result<(), Error> {
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
let branch_id = branch.parse().map_err(|_| Error::UserError {
code: Code::Branches,
code: Code::Validation,
message: "Malformed branch id".to_string(),
})?;
handle
@ -280,12 +287,16 @@ pub async fn unapply_branch(
pub async fn unapply_ownership(
handle: AppHandle,
project_id: &str,
ownership: Ownership,
ownership: &str,
) -> Result<(), Error> {
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
let ownership = ownership.parse().map_err(|_| Error::UserError {
code: Code::Validation,
message: "Malformed ownership".to_string(),
})?;
handle
.state::<Controller>()
.unapply_ownership(&project_id, &ownership)
@ -302,11 +313,11 @@ pub async fn push_virtual_branch(
with_force: bool,
) -> Result<(), Error> {
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
let branch_id = branch_id.parse().map_err(|_| Error::UserError {
code: Code::Branches,
code: Code::Validation,
message: "Malformed branch id".to_string(),
})?;
handle
@ -324,11 +335,11 @@ pub async fn can_apply_virtual_branch(
branch_id: &str,
) -> Result<bool, Error> {
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
let branch_id = branch_id.parse().map_err(|_| Error::UserError {
code: Code::Branches,
code: Code::Validation,
message: "Malformed branch id".to_string(),
})?;
handle
@ -345,11 +356,11 @@ pub async fn can_apply_remote_branch(
branch: &str,
) -> Result<bool, Error> {
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
let branch = branch.parse().map_err(|_| Error::UserError {
code: Code::Branches,
code: Code::Validation,
message: "Malformed branch name".to_string(),
})?;
handle
@ -363,12 +374,16 @@ pub async fn can_apply_remote_branch(
pub async fn list_remote_commit_files(
handle: AppHandle,
project_id: &str,
commit_oid: git::Oid,
commit_oid: &str,
) -> Result<Vec<RemoteBranchFile>, Error> {
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
let commit_oid = commit_oid.parse().map_err(|_| Error::UserError {
code: Code::Validation,
message: "Malformed commit oid".to_string(),
})?;
handle
.state::<Controller>()
.list_remote_commit_files(&project_id, commit_oid)
@ -381,16 +396,20 @@ pub async fn reset_virtual_branch(
handle: AppHandle,
project_id: &str,
branch_id: &str,
target_commit_oid: git::Oid,
target_commit_oid: &str,
) -> Result<(), Error> {
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
let branch_id = branch_id.parse().map_err(|_| Error::UserError {
code: Code::Branches,
code: Code::Validation,
message: "Malformed branch id".to_string(),
})?;
let target_commit_oid = target_commit_oid.parse().map_err(|_| Error::UserError {
code: Code::Validation,
message: "Malformed commit oid".to_string(),
})?;
handle
.state::<Controller>()
.reset_virtual_branch(&project_id, &branch_id, target_commit_oid)

View File

@ -26,7 +26,7 @@ pub async fn get_project_archive_path(
project_id: &str,
) -> Result<path::PathBuf, Error> {
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".into(),
})?;
handle
@ -54,7 +54,7 @@ pub async fn get_project_data_archive_path(
project_id: &str,
) -> Result<path::PathBuf, Error> {
let project_id = project_id.parse().map_err(|_| Error::UserError {
code: Code::Projects,
code: Code::Validation,
message: "Malformed project id".into(),
})?;
handle

View File

@ -4,6 +4,7 @@ import { listen as listenTauri } from '@tauri-apps/api/event';
export enum Code {
Unknown = 'errors.unknown',
Validation = 'errors.validation',
Projects = 'errors.projects',
ProjectsGitAuth = 'errors.projects.git.auth',
ProjectsGitRemote = 'errors.projects.git.remote',