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 std::{collections::HashMap, os::unix::net::UnixStream, path::PathBuf};
|
||||||
|
|
||||||
use keyfork_frame::{try_decode_from, try_encode_to, DecodeError, EncodeError};
|
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)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
@ -25,6 +25,9 @@ pub enum Error {
|
||||||
|
|
||||||
#[error("Could not perform frame transformation: {0}")]
|
#[error("Could not perform frame transformation: {0}")]
|
||||||
FrameDec(#[from] DecodeError),
|
FrameDec(#[from] DecodeError),
|
||||||
|
|
||||||
|
#[error("Error in Keyforkd: {0}")]
|
||||||
|
Keyforkd(#[from] KeyforkdError)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Result<T, E = Error> = std::result::Result<T, E>;
|
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))
|
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)]
|
#[derive(Debug)]
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
socket: UnixStream,
|
socket: UnixStream,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Client {
|
impl Client {
|
||||||
|
/// Create a new client from a given already-connected [`UnixStream`].
|
||||||
pub fn new(socket: UnixStream) -> Self {
|
pub fn new(socket: UnixStream) -> Self {
|
||||||
Self { socket }
|
Self { socket }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create a new client using well-known socket locations.
|
||||||
pub fn discover_socket() -> Result<Self> {
|
pub fn discover_socket() -> Result<Self> {
|
||||||
get_socket().map(|socket| Self { socket })
|
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> {
|
pub fn request(&mut self, req: &Request) -> Result<Response> {
|
||||||
try_encode_to(&bincode::serialize(&req)?, &mut self.socket)?;
|
try_encode_to(&bincode::serialize(&req)?, &mut self.socket)?;
|
||||||
let resp = try_decode_from(&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>,
|
S: Service<Request>,
|
||||||
Request: DeserializeOwned,
|
Request: DeserializeOwned,
|
||||||
<S as Service<Request>>::Response: Serialize,
|
<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,
|
<S as Service<Request>>::Future: Send + 'static,
|
||||||
{
|
{
|
||||||
type Response = Vec<u8>;
|
type Response = Vec<u8>;
|
||||||
|
@ -86,9 +86,11 @@ where
|
||||||
let response = self.service.call(request);
|
let response = self.service.call(request);
|
||||||
|
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
let response = response
|
let response = response.await;
|
||||||
.await
|
#[cfg(feature = "tracing")]
|
||||||
.map_err(|e| BincodeServiceError::Call(e.to_string()))?;
|
if let Err(e) = &response {
|
||||||
|
tracing::error!("Error performing derivation: {e}");
|
||||||
|
}
|
||||||
serialize(&response).map_err(|e| BincodeServiceError::Convert(e.to_string()))
|
serialize(&response).map_err(|e| BincodeServiceError::Convert(e.to_string()))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -116,7 +118,7 @@ mod tests {
|
||||||
|
|
||||||
struct App;
|
struct App;
|
||||||
|
|
||||||
#[derive(Debug, thiserror::Error)]
|
#[derive(Debug, thiserror::Error, Serialize)]
|
||||||
enum Infallible {}
|
enum Infallible {}
|
||||||
|
|
||||||
impl Service<Test> for App {
|
impl Service<Test> for App {
|
||||||
|
@ -141,7 +143,8 @@ mod tests {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn can_serde_responses() {
|
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()
|
let mut service = ServiceBuilder::new()
|
||||||
.layer(BincodeLayer::<Test>::default())
|
.layer(BincodeLayer::<Test>::default())
|
||||||
.service(App);
|
.service(App);
|
||||||
|
@ -152,6 +155,6 @@ mod tests {
|
||||||
.call(content.clone())
|
.call(content.clone())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(result, content);
|
assert_eq!(result, serialize(&Result::<Test, Infallible>::Ok(test)).unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue