handle errors

This commit is contained in:
Nikita Galaiko 2023-07-05 12:14:14 +02:00
parent e89bd9bdf6
commit ed9247f74c
3 changed files with 20 additions and 14 deletions

View File

@ -388,8 +388,11 @@ impl Repository {
return Err(anyhow!("project does not exist"));
}
let project = project.unwrap();
let project_repository = project_repository::Repository::open(&project)?;
let session = self.create_current_session(&project_repository)?;
let project_repository = project_repository::Repository::open(&project)
.context("failed to open project repository")?;
let session = self
.create_current_session(&project_repository)
.context("failed to create current session")?;
Ok(session)
}
}
@ -568,14 +571,14 @@ impl Repository {
// calculate the commit as the merge-base between HEAD in project_repository and this target commit
let head_oid = repo
.head()
.expect("Failed to get HEAD reference")
.context("Failed to get HEAD reference")?
.peel_to_commit()
.expect("Failed to peel HEAD reference to commit")
.context("Failed to peel HEAD reference to commit")?
.id();
commit_oid = repo
.merge_base(head_oid, commit_oid)
.expect("Failed to calculate merge base");
.context("Failed to calculate merge base")?;
println!("merge base: {:?}", commit_oid);
}

View File

@ -446,9 +446,12 @@ async fn git_remote_branches(
project_id: &str,
) -> Result<Vec<String>, Error> {
let app = handle.state::<app::App>();
let branches = app
.git_remote_branches(project_id)
.with_context(|| format!("failed to get git branches for project {}", project_id))?;
let branches = app.git_remote_branches(project_id).with_context(|| {
format!(
"failed to get remote git branches for project {}",
project_id
)
})?;
Ok(branches)
}

View File

@ -593,10 +593,10 @@ pub fn list_virtual_branches(
let mut branches: Vec<VirtualBranch> = Vec::new();
let current_session = gb_repository
.get_or_create_current_session()
.expect("failed to get or create currnt session");
.context("failed to get or create currnt session")?;
let current_session_reader = sessions::Reader::open(gb_repository, &current_session)
.expect("failed to open current session reader");
.context("failed to open current session reader")?;
let target_reader = target::Reader::new(&current_session_reader);
let default_target = match target_reader.read_default() {
@ -1392,9 +1392,9 @@ pub fn get_status_by_branch(
fn get_default_target(gb_repository: &gb_repository::Repository) -> Result<target::Target> {
let current_session = gb_repository
.get_or_create_current_session()
.expect("failed to get or create currnt session");
.context("failed to get or create currnt session")?;
let current_session_reader = sessions::Reader::open(gb_repository, &current_session)
.expect("failed to open current session reader");
.context("failed to open current session reader")?;
let target_reader = target::Reader::new(&current_session_reader);
let default_target = match target_reader.read_default() {
@ -1621,7 +1621,7 @@ fn write_tree(
.to_object(git_repository)
.unwrap()
.peel_to_blob()
.expect("failed to get blob");
.context("failed to get blob")?;
// get the contents
let blob_contents = blob.content();
@ -1686,7 +1686,7 @@ pub fn commit(
) -> Result<()> {
// get the files to commit
let statuses = get_status_by_branch(gb_repository, project_repository)
.expect("failed to get status by branch");
.context("failed to get status by branch")?;
for (mut branch, files) in statuses {
if branch.id == branch_id {