correctly print anyhow errors

This commit is contained in:
Nikita Galaiko 2023-02-23 16:49:06 +01:00
parent ba838e19d9
commit 56ca8d5be4
No known key found for this signature in database
GPG Key ID: EBAB54E845BA519D
5 changed files with 92 additions and 70 deletions

View File

@ -135,7 +135,7 @@ fn list_commit_deltas(
);
}
Err(e) => {
log::error!("Could not get blob for {}: {}", entry_path.display(), e);
log::error!("Could not get blob for {}: {:#}", entry_path.display(), e);
}
}
git2::TreeWalkResult::Ok

View File

@ -8,7 +8,7 @@ mod storage;
mod users;
mod watchers;
use anyhow::Result;
use anyhow::{Context, Result};
use deltas::Delta;
use log;
use serde::{Deserialize, Serialize};
@ -47,14 +47,14 @@ fn list_sessions(
let repo = repositories::Repository::open(&projects_storage, &users_storage, project_id)
.map_err(|e| {
log::error!("{}", e);
log::error!("{:#}", e);
Error {
message: "Failed to open project".to_string(),
}
})?;
let sessions = repo.sessions().map_err(|e| {
log::error!("{}", e);
log::error!("{:#}", e);
Error {
message: "Failed to list sessions".to_string(),
}
@ -70,7 +70,7 @@ fn get_user(handle: tauri::AppHandle) -> Result<Option<users::User>, Error> {
let users_storage = users::Storage::new(storage);
users_storage.get().map_err(|e| {
log::error!("{}", e);
log::error!("{:#}", e);
Error {
message: "Failed to get user".to_string(),
}
@ -84,7 +84,7 @@ fn set_user(handle: tauri::AppHandle, user: users::User) -> Result<(), Error> {
let users_storage = users::Storage::new(storage);
users_storage.set(&user).map_err(|e| {
log::error!("{}", e);
log::error!("{:#}", e);
Error {
message: "Failed to save user".to_string(),
}
@ -102,7 +102,7 @@ fn delete_user(handle: tauri::AppHandle) -> Result<(), Error> {
let users_storage = users::Storage::new(storage);
users_storage.delete().map_err(|e| {
log::error!("{}", e);
log::error!("{:#}", e);
Error {
message: "Failed to delete user".to_string(),
}
@ -123,7 +123,7 @@ fn update_project(
let projects_storage = projects::Storage::new(storage);
projects_storage.update_project(&project).map_err(|e| {
log::error!("{}", e);
log::error!("{:#}", e);
Error {
message: "Failed to update project".to_string(),
}
@ -144,7 +144,7 @@ fn add_project(handle: tauri::AppHandle, path: &str) -> Result<projects::Project
);
for project in projects_storage.list_projects().map_err(|e| {
log::error!("{}", e);
log::error!("{:#}", e);
Error {
message: "Failed to list projects".to_string(),
}
@ -162,7 +162,7 @@ fn add_project(handle: tauri::AppHandle, path: &str) -> Result<projects::Project
..Default::default()
})
.map_err(|e| {
log::error!("{}", e);
log::error!("{:#}", e);
Error {
message: "Failed to undelete project".to_string(),
}
@ -176,7 +176,7 @@ fn add_project(handle: tauri::AppHandle, path: &str) -> Result<projects::Project
if project.is_ok() {
let project = project.unwrap();
projects_storage.add_project(&project).map_err(|e| {
log::error!("{}", e);
log::error!("{:#}", e);
Error {
message: "Failed to add project".to_string(),
}
@ -186,7 +186,7 @@ fn add_project(handle: tauri::AppHandle, path: &str) -> Result<projects::Project
mpsc::channel();
watchers.watch(tx, &project).map_err(|e| {
log::error!("{}", e);
log::error!("{:#}", e);
Error {
message: "Failed to watch project".to_string(),
}
@ -209,7 +209,7 @@ fn list_projects(handle: tauri::AppHandle) -> Result<Vec<projects::Project>, Err
let projects_storage = projects::Storage::new(storage);
projects_storage.list_projects().map_err(|e| {
log::error!("{}", e);
log::error!("{:#}", e);
Error {
message: "Failed to list projects".to_string(),
}
@ -232,7 +232,7 @@ fn delete_project(handle: tauri::AppHandle, id: &str) -> Result<(), Error> {
match projects_storage.get_project(id) {
Ok(Some(project)) => {
watchers.unwatch(project).map_err(|e| {
log::error!("{}", e);
log::error!("{:#}", e);
Error {
message: "Failed to unwatch project".to_string(),
}
@ -245,7 +245,7 @@ fn delete_project(handle: tauri::AppHandle, id: &str) -> Result<(), Error> {
..Default::default()
})
.map_err(|e| {
log::error!("{}", e);
log::error!("{:#}", e);
Error {
message: "Failed to delete project".to_string(),
}
@ -255,7 +255,7 @@ fn delete_project(handle: tauri::AppHandle, id: &str) -> Result<(), Error> {
}
Ok(None) => Ok(()),
Err(e) => {
log::error!("{}", e);
log::error!("{:#}", e);
Err(Error {
message: "Failed to get project".to_string(),
})
@ -277,14 +277,14 @@ fn list_session_files(
let repo = repositories::Repository::open(&projects_storage, &users_storage, project_id)
.map_err(|e| {
log::error!("{}", e);
log::error!("{:#}", e);
Error {
message: "Failed to open project".to_string(),
}
})?;
let files = repo.files(session_id, paths).map_err(|e| {
log::error!("{}", e);
log::error!("{:#}", e);
Error {
message: "Failed to list files".to_string(),
}
@ -306,14 +306,14 @@ fn list_deltas(
let repo = repositories::Repository::open(&projects_storage, &users_storage, project_id)
.map_err(|e| {
log::error!("{}", e);
log::error!("{:#}", e);
Error {
message: "Failed to open project".to_string(),
}
})?;
let deltas = repo.deltas(session_id).map_err(|e| {
log::error!("{}", e);
log::error!("{:#}", e);
Error {
message: "Failed to list deltas".to_string(),
}
@ -415,14 +415,15 @@ fn main() {
let (tx, rx): (mpsc::Sender<events::Event>, mpsc::Receiver<events::Event>) =
mpsc::channel();
if let Ok(projects) = projects_storage.list_projects() {
for project in projects {
watchers
.watch(tx.clone(), &project)
.map_err(|e| e.to_string())?;
match projects_storage.list_projects() {
Ok(projects) => {
for project in projects {
watchers
.watch(tx.clone(), &project)
.with_context(|| format!("Failed to watch project: {}", project.id))?
}
}
} else {
log::error!("Failed to list projects");
Err(e) => log::error!("Failed to list projects: {:#}", e),
}
watch_events(app.handle(), rx);
@ -505,7 +506,7 @@ fn watch_events(handle: tauri::AppHandle, rx: mpsc::Receiver<events::Event>) {
if let Some(window) = handle.get_window("main") {
log::info!("Emitting event: {}", event.name);
match window.emit(&event.name, event.payload) {
Err(e) => log::error!("Failed to emit event: {}", e),
Err(e) => log::error!("Failed to emit event: {:#}", e),
_ => {}
}
}

View File

@ -570,7 +570,7 @@ fn push_to_remote(
repo: &git2::Repository,
user: &Option<users::User>,
project: &projects::Project,
) -> Result<(), git2::Error> {
) -> Result<()> {
// only push if logged in
let access_token = match user {
Some(user) => user.access_token.clone(),
@ -586,7 +586,14 @@ fn push_to_remote(
log::info!("pushing {} to {}", project.path, remote_url);
// Create an anonymous remote
let mut remote = repo.remote_anonymous(remote_url.as_str()).unwrap();
let mut remote = repo
.remote_anonymous(remote_url.as_str())
.with_context(|| {
format!(
"failed to create anonymous remote for {}",
remote_url.as_str()
)
})?;
// Set the remote's callbacks
let mut callbacks = git2::RemoteCallbacks::new();
@ -616,7 +623,15 @@ fn push_to_remote(
push_options.custom_headers(headers);
// Push to the remote
remote.push(&[project.refname()], Some(&mut push_options))?;
remote
.push(&[project.refname()], Some(&mut push_options))
.with_context(|| {
format!(
"failed to push {} to {}",
project.refname(),
remote_url.as_str()
)
})?;
Ok(())
}
@ -747,7 +762,7 @@ fn add_wd_path(
/// calculates sha256 digest of a large file as lowercase hex string via streaming buffer
/// used to calculate the hash of large files that are not supported by git
fn sha256_digest(path: &Path) -> Result<String, std::io::Error> {
fn sha256_digest(path: &Path) -> Result<String> {
let input = File::open(path)?;
let mut reader = BufReader::new(input);
@ -881,7 +896,7 @@ fn write_gb_commit(
repo: &git2::Repository,
user: &Option<users::User>,
project: &projects::Project,
) -> Result<git2::Oid, git2::Error> {
) -> Result<git2::Oid> {
// find the Oid of the commit that refs/.../current points to, none if it doesn't exist
let refname = project.refname();

View File

@ -44,44 +44,50 @@ impl<'a> DeltaWatchers<'a> {
let repo = git2::Repository::open(project_path)?;
thread::spawn(move || {
while let Ok(event) = rx.recv() {
if let Ok(notify_event) = event {
for file_path in notify_event.paths {
let relative_file_path =
file_path.strip_prefix(repo.workdir().unwrap()).unwrap();
match register_file_change(
&project,
&repo,
&notify_event.kind,
&relative_file_path,
) {
Ok(Some((session, deltas))) => {
match sender.send(events::Event::session(&project, &session)) {
Err(e) => log::error!("filed to send session event: {:?}", e),
Ok(_) => {}
}
match deltas {
Some(deltas) => {
match sender.send(events::Event::detlas(
&project,
&session,
&deltas,
&relative_file_path,
)) {
Err(e) => {
log::error!("failed to send deltas event: {:?}", e)
}
Ok(_) => {}
match event {
Ok(notify_event) => {
for file_path in notify_event.paths {
let relative_file_path =
file_path.strip_prefix(repo.workdir().unwrap()).unwrap();
match register_file_change(
&project,
&repo,
&notify_event.kind,
&relative_file_path,
) {
Ok(Some((session, deltas))) => {
match sender.send(events::Event::session(&project, &session)) {
Err(e) => {
log::error!("filed to send session event: {:#}", e)
}
Ok(_) => {}
}
match deltas {
Some(deltas) => {
match sender.send(events::Event::detlas(
&project,
&session,
&deltas,
&relative_file_path,
)) {
Err(e) => {
log::error!(
"failed to send deltas event: {:#}",
e
)
}
Ok(_) => {}
}
}
None => {}
}
None => {}
}
Ok(None) => {}
Err(e) => log::error!("failed to register file change: {:#}", e),
}
Ok(None) => {}
Err(e) => log::error!("failed to register file change: {:?}", e),
}
}
} else {
log::error!("notify event error: {:?}", event);
Err(e) => log::error!("notify event error: {:#}", e),
}
}
});
@ -238,7 +244,7 @@ fn write_beginning_meta_files(
repo: &git2::Repository,
) -> Result<sessions::Session, Box<dyn std::error::Error>> {
match sessions::Session::current(repo, project)
.map_err(|e| format!("Error while getting current session: {}", e.to_string()))?
.with_context(|| "failed to get current session")?
{
Some(mut session) => {
session

View File

@ -41,7 +41,7 @@ impl GitWatcher {
let project = local_self.projects_storage.get_project(&project_id);
if project.is_err() {
log::error!(
"Error while getting project {} for git watcher: {}",
"Error while getting project {} for git watcher: {:#}",
project_id,
project.err().unwrap()
);
@ -58,7 +58,7 @@ impl GitWatcher {
let user = local_self.users_storage.get();
if user.is_err() {
log::error!(
"Error while getting user for git watcher: {}",
"Error while getting user for git watcher: {:#}",
user.err().unwrap()
);
continue;
@ -68,14 +68,14 @@ impl GitWatcher {
match local_self.check_for_changes(&project, &user) {
Ok(Some(session)) => {
match sender.send(events::Event::session(&project, &session)) {
Err(e) => log::error!("filed to send session event: {:?}", e),
Err(e) => log::error!("filed to send session event: {:#}", e),
Ok(_) => {}
}
}
Ok(None) => {}
Err(error) => {
log::error!(
"Error while checking {} for changes: {}",
"Error while checking {} for changes: {:#}",
project.path,
error
);