keyforkd: allow sending server-side error to clients
This commit is contained in:
parent
ada6cf150b
commit
ee258ac115
|
@ -1,7 +1,7 @@
|
|||
use std::{collections::HashMap, os::unix::net::UnixStream, path::PathBuf};
|
||||
|
||||
use keyfork_frame::{try_decode_from, try_encode_to, DecodeError, EncodeError};
|
||||
use keyforkd_models::{Request, Response /* Error as KeyforkdError */};
|
||||
use keyforkd_models::{Request, Response, Error as KeyforkdError};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
@ -25,6 +25,9 @@ pub enum Error {
|
|||
|
||||
#[error("Could not perform frame transformation: {0}")]
|
||||
FrameDec(#[from] DecodeError),
|
||||
|
||||
#[error("Error in Keyforkd: {0}")]
|
||||
Keyforkd(#[from] KeyforkdError)
|
||||
}
|
||||
|
||||
pub type Result<T, E = Error> = std::result::Result<T, E>;
|
||||
|
@ -51,23 +54,32 @@ pub fn get_socket() -> Result<UnixStream, Error> {
|
|||
UnixStream::connect(&socket_path).map_err(|e| Error::Connect(e, socket_path))
|
||||
}
|
||||
|
||||
/// A client to interact with Keyforkd.
|
||||
///
|
||||
/// Upon creation, a socket is opened, and is kept open for the duration of the object's lifetime.
|
||||
/// Currently, Keyforkd does not support the reuse of sockets. Attempting to reuse the socket after
|
||||
/// previously using it will likely result in an error.
|
||||
#[derive(Debug)]
|
||||
pub struct Client {
|
||||
socket: UnixStream,
|
||||
}
|
||||
|
||||
impl Client {
|
||||
/// Create a new client from a given already-connected [`UnixStream`].
|
||||
pub fn new(socket: UnixStream) -> Self {
|
||||
Self { socket }
|
||||
}
|
||||
|
||||
/// Create a new client using well-known socket locations.
|
||||
pub fn discover_socket() -> Result<Self> {
|
||||
get_socket().map(|socket| Self { socket })
|
||||
}
|
||||
|
||||
/// Serialize and send a [`Request`] to the server, awaiting a [`Result<Response>`].
|
||||
pub fn request(&mut self, req: &Request) -> Result<Response> {
|
||||
try_encode_to(&bincode::serialize(&req)?, &mut self.socket)?;
|
||||
let resp = try_decode_from(&mut self.socket)?;
|
||||
bincode::deserialize(&resp).map_err(From::from)
|
||||
let resp: Result<Response, KeyforkdError> = bincode::deserialize(&resp)?;
|
||||
resp.map_err(From::from)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,7 +59,7 @@ where
|
|||
S: Service<Request>,
|
||||
Request: DeserializeOwned,
|
||||
<S as Service<Request>>::Response: Serialize,
|
||||
<S as Service<Request>>::Error: std::error::Error,
|
||||
<S as Service<Request>>::Error: std::error::Error + Serialize,
|
||||
<S as Service<Request>>::Future: Send + 'static,
|
||||
{
|
||||
type Response = Vec<u8>;
|
||||
|
@ -86,9 +86,11 @@ where
|
|||
let response = self.service.call(request);
|
||||
|
||||
Box::pin(async move {
|
||||
let response = response
|
||||
.await
|
||||
.map_err(|e| BincodeServiceError::Call(e.to_string()))?;
|
||||
let response = response.await;
|
||||
#[cfg(feature = "tracing")]
|
||||
if let Err(e) = &response {
|
||||
tracing::error!("Error performing derivation: {e}");
|
||||
}
|
||||
serialize(&response).map_err(|e| BincodeServiceError::Convert(e.to_string()))
|
||||
})
|
||||
}
|
||||
|
@ -116,7 +118,7 @@ mod tests {
|
|||
|
||||
struct App;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
#[derive(Debug, thiserror::Error, Serialize)]
|
||||
enum Infallible {}
|
||||
|
||||
impl Service<Test> for App {
|
||||
|
@ -141,7 +143,8 @@ mod tests {
|
|||
|
||||
#[tokio::test]
|
||||
async fn can_serde_responses() {
|
||||
let content = serialize(&Test::new()).unwrap();
|
||||
let test = Test::new();
|
||||
let content = serialize(&test).unwrap();
|
||||
let mut service = ServiceBuilder::new()
|
||||
.layer(BincodeLayer::<Test>::default())
|
||||
.service(App);
|
||||
|
@ -152,6 +155,6 @@ mod tests {
|
|||
.call(content.clone())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(result, content);
|
||||
assert_eq!(result, serialize(&Result::<Test, Infallible>::Ok(test)).unwrap());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue