Change to return Err when an awaited response is an error response

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/16/head
trivernis 3 years ago
parent d8cf7427da
commit cfc158dcb8
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

2
Cargo.lock generated

@ -500,7 +500,7 @@ dependencies = [
[[package]]
name = "rmp-ipc"
version = "0.7.2"
version = "0.8.0"
dependencies = [
"criterion",
"lazy_static",

@ -1,6 +1,6 @@
[package]
name = "rmp-ipc"
version = "0.7.2"
version = "0.8.0"
authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018"
readme = "README.md"

@ -1,3 +1,4 @@
use crate::error_event::ErrorEventData;
use thiserror::Error;
use tokio::sync::oneshot;
@ -25,6 +26,9 @@ pub enum Error {
#[error("Send Error")]
SendError,
#[error("Error response: {0}")]
ErrorEvent(#[from] ErrorEventData),
}
impl From<String> for Error {

@ -1,4 +1,6 @@
use serde::{Deserialize, Serialize};
use std::error::Error;
use std::fmt::{Display, Formatter};
pub static ERROR_EVENT_NAME: &str = "error";
@ -11,3 +13,11 @@ pub struct ErrorEventData {
pub code: u16,
pub message: String,
}
impl Error for ErrorEventData {}
impl Display for ErrorEventData {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "IPC Code {}: '{}'", self.code, self.message)
}
}

@ -1,4 +1,5 @@
use crate::error::Result;
use crate::error_event::{ErrorEventData, ERROR_EVENT_NAME};
use crate::events::event::Event;
use crate::events::payload::EventSendPayload;
use crate::ipc::context::Context;
@ -119,6 +120,10 @@ impl EmitMetadata {
#[tracing::instrument(skip(self, ctx), fields(self.message_id))]
pub async fn await_reply(&self, ctx: &Context) -> Result<Event> {
let reply = ctx.await_reply(self.message_id).await?;
Ok(reply)
if reply.name() == ERROR_EVENT_NAME {
Err(reply.data::<ErrorEventData>()?.into())
} else {
Ok(reply)
}
}
}

@ -205,7 +205,6 @@ async fn test_error_responses() {
.await
.unwrap()
.await_reply(&ctx)
.await
.unwrap();
assert_eq!(reply.name(), "error");
.await;
assert!(reply.is_err());
}

Loading…
Cancel
Save