refactor: unify error handling with anyhow and add From conversions

Phase 5: Replace all Box<dyn Error> return types with anyhow::Result<T>
throughout the codebase. Replace string-based Err("msg".into()) and
format!().into() patterns with bail!() and anyhow!() macros. Fix
dirs::home_dir().unwrap() in settings.rs to use a fallback path instead
of panicking when HOME is unset. Remove stray use std::error::Error
imports no longer needed.

Phase 6: Add From<&User> for CacheUser in models/user.rs and
From<&Laboratory>/From<&Laboratories> for CacheLaboratory/CacheLabsWrapper
in models/laboratory.rs. Simplify commands/login.rs to use .into()
conversions, removing the redundant to_cache_user() and to_cache_labs()
helper functions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-04-20 14:19:10 +09:00
co-authored by Copilot
parent e8fd359f54
commit 769a5a68e2
30 changed files with 889 additions and 872 deletions
+3 -2
View File
@@ -1,6 +1,7 @@
use crate::connection::MDRSConnection;
use crate::models::user::User as ModelUser;
use serde::Deserialize;
use anyhow::{bail};
/// Full API response shape from GET v3/users/current/
#[derive(Debug, Deserialize)]
@@ -40,7 +41,7 @@ impl MDRSConnection {
pub async fn token_refresh(
&self,
refresh_token: &str,
) -> Result<String, Box<dyn std::error::Error>> {
) -> Result<String, anyhow::Error> {
let body = serde_json::json!({ "refresh": refresh_token });
let resp = self
.client
@@ -49,7 +50,7 @@ impl MDRSConnection {
.send()
.await?;
if !resp.status().is_success() {
return Err(format!("Token refresh failed: {}", resp.status()).into());
bail!("Token refresh failed: {}", resp.status());
}
let r: TokenRefreshResponse = resp.json().await?;
Ok(r.access)