fix: send error back (#556)

This commit is contained in:
Matthias Seitz 2021-11-05 02:21:23 +01:00 committed by GitHub
parent bd3a704200
commit 6bf45a0b5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 4 deletions

View File

@ -82,6 +82,16 @@ impl<R> ResponseData<R> {
}
}
impl ResponseData<serde_json::Value> {
/// Encode the error to json value if it is an error
pub fn into_value(self) -> serde_json::Result<serde_json::Value> {
match self {
ResponseData::Success { result } => Ok(result),
ResponseData::Error { error } => serde_json::to_value(error),
}
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -181,7 +181,7 @@ where
},
// Handle socket messages
msg = self.socket_reader.next() => match msg {
Some(Ok(msg)) => self.handle_socket(read_buffer, msg).await?,
Some(Ok(msg)) => self.handle_socket(read_buffer, msg)?,
Some(Err(err)) => {
error!("IPC read error: {:?}", err);
return Err(err.into());
@ -207,7 +207,7 @@ where
}
if let Err(err) = self.socket_writer.write(request.as_bytes()).await {
error!("WS connection error: {:?}", err);
error!("IPC connection error: {:?}", err);
self.pending.remove(&id);
}
}
@ -229,7 +229,7 @@ where
Ok(())
}
async fn handle_socket(
fn handle_socket(
&mut self,
read_buffer: &mut Vec<u8>,
bytes: bytes::Bytes,
@ -292,7 +292,7 @@ where
let id = output.id;
// Converts output into result, to send data if valid response.
let value = output.data.into_result()?;
let value = output.data.into_value()?;
let response_tx = self.pending.remove(&id).ok_or_else(|| {
IpcError::ChannelError("No response channel exists for the response ID".to_string())