Skip to main content

cossh/auth/agent/
error.rs

1//! Errors returned by unlock-agent operations.
2
3use crate::auth::vault::VaultError;
4use std::fmt;
5use std::io;
6
7#[derive(Debug)]
8/// Agent client/server error variants.
9pub enum AgentError {
10    Vault(VaultError),
11    Io(io::Error),
12    Locked,
13    EntryNotFound,
14    InvalidMasterPassword,
15    InvalidOrExpiredAskpassToken,
16    VaultNotInitialized,
17    Protocol(String),
18}
19
20impl fmt::Display for AgentError {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        match self {
23            Self::Vault(err) => write!(f, "{err}"),
24            Self::Io(err) => write!(f, "{err}"),
25            Self::Locked => write!(f, "password vault is locked"),
26            Self::EntryNotFound => write!(f, "password vault entry was not found"),
27            Self::InvalidMasterPassword => write!(f, "invalid master password"),
28            Self::InvalidOrExpiredAskpassToken => write!(f, "invalid or expired askpass token"),
29            Self::VaultNotInitialized => write!(f, "password vault is not initialized"),
30            Self::Protocol(message) => write!(f, "{message}"),
31        }
32    }
33}
34
35impl std::error::Error for AgentError {}
36
37impl From<io::Error> for AgentError {
38    fn from(value: io::Error) -> Self {
39        Self::Io(value)
40    }
41}
42
43impl From<VaultError> for AgentError {
44    fn from(value: VaultError) -> Self {
45        Self::Vault(value)
46    }
47}
48
49pub(crate) fn map_remote_error(code: &str, message: String) -> AgentError {
50    match code {
51        "locked" => AgentError::Locked,
52        "entry_not_found" => AgentError::EntryNotFound,
53        "invalid_master_password" => AgentError::InvalidMasterPassword,
54        "invalid_or_expired_askpass_token" => AgentError::InvalidOrExpiredAskpassToken,
55        "vault_not_initialized" => AgentError::VaultNotInitialized,
56        "invalid_entry_name" | "vault_error" | "askpass_token_error" => AgentError::Protocol(message),
57        _ => AgentError::Protocol(message),
58    }
59}