Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relax the error type in handle #21

Merged
merged 4 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ This example starts listening on a Unix socket `ssh-agent.sock` and processes re
use tokio::net::UnixListener;
use ssh_agent_lib::agent::{Session, Agent};
use ssh_agent_lib::error::AgentError;
use ssh_agent_lib::proto::message::Message;
#[derive(Default)]
struct MyAgent;
#[ssh_agent_lib::async_trait]
impl Session for MyAgent {
async fn handle(&mut self, message: Message) -> Result<Message, AgentError> {
async fn handle(&mut self, message: Message) -> Result<Message, Box<dyn std::error::Error>> {
match message {
Message::SignRequest(request) => {
// get the signature by signing `request.data`
Expand Down
8 changes: 2 additions & 6 deletions examples/key_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use log::info;
use tokio::net::UnixListener;

use ssh_agent_lib::agent::{Agent, Session};
use ssh_agent_lib::error::AgentError;
use ssh_agent_lib::proto::message::{self, Message, SignRequest};
use ssh_agent_lib::proto::private_key::{PrivateKey, RsaPrivateKey};
use ssh_agent_lib::proto::public_key::PublicKey;
Expand Down Expand Up @@ -148,11 +147,8 @@ impl KeyStorage {

#[async_trait]
impl Session for KeyStorage {
async fn handle(&mut self, message: Message) -> Result<Message, AgentError> {
self.handle_message(message).or_else(|error| {
println!("Error handling message - {:?}", error);
Ok(Message::Failure)
})
async fn handle(&mut self, message: Message) -> Result<Message, Box<dyn std::error::Error>> {
self.handle_message(message)
}
}

Expand Down
17 changes: 10 additions & 7 deletions src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl ListeningSocket for TcpListener {

#[async_trait]
pub trait Session: 'static + Sync + Send + Sized {
async fn handle(&mut self, message: Message) -> Result<Message, AgentError>;
async fn handle(&mut self, message: Message) -> Result<Message, Box<dyn std::error::Error>>;

async fn handle_socket<S>(
&mut self,
Expand All @@ -88,10 +88,13 @@ pub trait Session: 'static + Sync + Send + Sized {
{
loop {
if let Some(incoming_message) = adapter.try_next().await? {
let response = self.handle(incoming_message).await.map_err(|e| {
error!("Error handling message; error = {:?}", e);
AgentError::User
})?;
let response = match self.handle(incoming_message).await {
Ok(message) => message,
Err(e) => {
error!("Error handling message: {:?}", e);
Message::Failure
wiktor-k marked this conversation as resolved.
Show resolved Hide resolved
}
};

adapter.send(response).await?;
} else {
Expand All @@ -118,12 +121,12 @@ pub trait Agent: 'static + Sync + Send + Sized {
tokio::spawn(async move {
let adapter = Framed::new(socket, MessageCodec);
if let Err(e) = session.handle_socket::<S>(adapter).await {
error!("Agent protocol error; error = {:?}", e);
error!("Agent protocol error: {:?}", e);
}
});
}
Err(e) => {
error!("Failed to accept socket; error = {:?}", e);
error!("Failed to accept socket: {:?}", e);
return Err(AgentError::IO(e));
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::io;

#[derive(Debug)]
pub enum AgentError {
User,
Proto(ProtoError),
IO(io::Error),
}
Expand All @@ -23,7 +22,6 @@ impl From<io::Error> for AgentError {
impl std::fmt::Display for AgentError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AgentError::User => write!(f, "Agent: User error"),
AgentError::Proto(proto) => write!(f, "Agent: Protocol error: {}", proto),
AgentError::IO(error) => write!(f, "Agent: I/O error: {}", error),
}
Expand Down
Loading