More error cleanup
This commit is contained in:
parent
b72e5950bf
commit
9b3ced3165
|
@ -22,24 +22,20 @@ fn other_eio<E: std::fmt::Display>(e: E) -> std::io::Error {
|
||||||
std::io::Error::new(std::io::ErrorKind::Other, e.to_string())
|
std::io::Error::new(std::io::ErrorKind::Other, e.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn translate_sink_err(e: tungstenite::Error) -> packets::EncodeError {
|
|
||||||
packets::EncodeError::Write(other_eio(e))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn encode_message(codec: &value::Codec<V, Syndicate>, p: packets::S2C) ->
|
fn encode_message(codec: &value::Codec<V, Syndicate>, p: packets::S2C) ->
|
||||||
Result<Message, packets::EncodeError>
|
Result<Message, std::io::Error>
|
||||||
{
|
{
|
||||||
use serde::ser::Serialize;
|
use serde::ser::Serialize;
|
||||||
use preserves::ser::Serializer;
|
use preserves::ser::Serializer;
|
||||||
let mut bs = Vec::with_capacity(128);
|
let mut bs = Vec::with_capacity(128);
|
||||||
let mut ser: Serializer<_, V, Syndicate> =
|
let mut ser: Serializer<_, V, Syndicate> =
|
||||||
Serializer::new(&mut bs, codec.encode_placeholders.as_ref());
|
Serializer::new(&mut bs, codec.encode_placeholders.as_ref());
|
||||||
p.serialize(&mut ser).map_err(|e| std::io::Error::from(e))?;
|
p.serialize(&mut ser)?;
|
||||||
Ok(Message::Binary(bs))
|
Ok(Message::Binary(bs))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn message_encoder(codec: &value::Codec<V, Syndicate>)
|
fn message_encoder(codec: &value::Codec<V, Syndicate>)
|
||||||
-> impl Fn(packets::S2C) -> futures::future::Ready<Result<Message, packets::EncodeError>> + '_
|
-> impl Fn(packets::S2C) -> futures::future::Ready<Result<Message, std::io::Error>> + '_
|
||||||
{
|
{
|
||||||
return move |p| futures::future::ready(encode_message(codec, p));
|
return move |p| futures::future::ready(encode_message(codec, p));
|
||||||
}
|
}
|
||||||
|
@ -97,7 +93,7 @@ async fn run_connection(connid: ConnId,
|
||||||
let (o, i) = s.split();
|
let (o, i) = s.split();
|
||||||
let codec = packets::standard_preserves_codec();
|
let codec = packets::standard_preserves_codec();
|
||||||
let i = i.map(message_decoder(&codec));
|
let i = i.map(message_decoder(&codec));
|
||||||
let o = o.sink_map_err(translate_sink_err).with(message_encoder(&codec));
|
let o = o.sink_map_err(other_eio).with(message_encoder(&codec));
|
||||||
let mut p = Peer::new(connid, i, o);
|
let mut p = Peer::new(connid, i, o);
|
||||||
p.run(spaces, &config).await?
|
p.run(spaces, &config).await?
|
||||||
},
|
},
|
||||||
|
|
|
@ -138,8 +138,7 @@ impl Dataspace {
|
||||||
packets::Action::Assert(ref epname, ref assertion) => {
|
packets::Action::Assert(ref epname, ref assertion) => {
|
||||||
let ac = self.peers.get_mut(&id).unwrap();
|
let ac = self.peers.get_mut(&id).unwrap();
|
||||||
if ac.endpoints.contains_key(&epname) {
|
if ac.endpoints.contains_key(&epname) {
|
||||||
return Err(("Duplicate endpoint name".to_string(),
|
return Err(("Duplicate endpoint name".to_string(), value::to_value(a)));
|
||||||
value::to_value(a).unwrap()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let ar =
|
let ar =
|
||||||
|
@ -171,8 +170,7 @@ impl Dataspace {
|
||||||
let ac = self.peers.get_mut(&id).unwrap();
|
let ac = self.peers.get_mut(&id).unwrap();
|
||||||
match ac.endpoints.remove(epname) {
|
match ac.endpoints.remove(epname) {
|
||||||
None => {
|
None => {
|
||||||
return Err(("Nonexistent endpoint name".to_string(),
|
return Err(("Nonexistent endpoint name".to_string(), value::to_value(a)));
|
||||||
value::to_value(a).unwrap()));
|
|
||||||
}
|
}
|
||||||
Some(ep) => {
|
Some(ep) => {
|
||||||
self.remove_endpoint(&mut outbound_turns, id, epname, ep);
|
self.remove_endpoint(&mut outbound_turns, id, epname, ep);
|
||||||
|
|
|
@ -47,7 +47,7 @@ pub enum S2C {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum DecodeError {
|
pub enum DecodeError {
|
||||||
Read(io::Error),
|
Read(io::Error),
|
||||||
Parse(value::error::Error<Syndicate>, V),
|
Parse(value::de::error::Error<Syndicate>, V),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<io::Error> for DecodeError {
|
impl From<io::Error> for DecodeError {
|
||||||
|
@ -67,45 +67,6 @@ impl std::error::Error for DecodeError {
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum EncodeError {
|
|
||||||
Write(io::Error),
|
|
||||||
Unparse(value::error::Error<Syndicate>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<io::Error> for EncodeError {
|
|
||||||
fn from(v: io::Error) -> Self {
|
|
||||||
EncodeError::Write(v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<value::error::Error<Syndicate>> for EncodeError {
|
|
||||||
fn from(v: value::error::Error<Syndicate>) -> Self {
|
|
||||||
EncodeError::Unparse(v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<EncodeError> for io::Error {
|
|
||||||
fn from(v: EncodeError) -> Self {
|
|
||||||
match v {
|
|
||||||
EncodeError::Write(e) => e,
|
|
||||||
EncodeError::Unparse(e) =>
|
|
||||||
Self::new(io::ErrorKind::InvalidData, format!("{:?}", e)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Display for EncodeError {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
||||||
write!(f, "{:?}", self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::error::Error for EncodeError {
|
|
||||||
}
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
pub struct Codec<InT, OutT> {
|
pub struct Codec<InT, OutT> {
|
||||||
codec: value::Codec<V, Syndicate>,
|
codec: value::Codec<V, Syndicate>,
|
||||||
ph_in: PhantomData<InT>,
|
ph_in: PhantomData<InT>,
|
||||||
|
@ -161,7 +122,7 @@ impl<InT: serde::de::DeserializeOwned, OutT> tokio_util::codec::Decoder for Code
|
||||||
|
|
||||||
impl<InT, OutT: serde::Serialize> tokio_util::codec::Encoder<OutT> for Codec<InT, OutT>
|
impl<InT, OutT: serde::Serialize> tokio_util::codec::Encoder<OutT> for Codec<InT, OutT>
|
||||||
{
|
{
|
||||||
type Error = EncodeError;
|
type Error = io::Error;
|
||||||
fn encode(&mut self, item: OutT, bs: &mut BytesMut) -> Result<(), Self::Error> {
|
fn encode(&mut self, item: OutT, bs: &mut BytesMut) -> Result<(), Self::Error> {
|
||||||
let mut w = bs.writer();
|
let mut w = bs.writer();
|
||||||
let mut ser: Serializer<_, V, Syndicate> = Serializer::new(&mut w, self.codec.encode_placeholders.as_ref());
|
let mut ser: Serializer<_, V, Syndicate> = Serializer::new(&mut w, self.codec.encode_placeholders.as_ref());
|
||||||
|
|
|
@ -20,7 +20,7 @@ pub type ResultC2S = Result<packets::C2S, packets::DecodeError>;
|
||||||
|
|
||||||
pub struct Peer<I, O>
|
pub struct Peer<I, O>
|
||||||
where I: Stream<Item = ResultC2S> + Send,
|
where I: Stream<Item = ResultC2S> + Send,
|
||||||
O: Sink<packets::S2C, Error = packets::EncodeError>,
|
O: Sink<packets::S2C, Error = std::io::Error>,
|
||||||
{
|
{
|
||||||
id: ConnId,
|
id: ConnId,
|
||||||
tx: UnboundedSender<packets::S2C>,
|
tx: UnboundedSender<packets::S2C>,
|
||||||
|
@ -36,7 +36,7 @@ fn err(s: &str, ctx: V) -> packets::S2C {
|
||||||
|
|
||||||
impl<I, O> Peer<I, O>
|
impl<I, O> Peer<I, O>
|
||||||
where I: Stream<Item = ResultC2S> + Send,
|
where I: Stream<Item = ResultC2S> + Send,
|
||||||
O: Sink<packets::S2C, Error = packets::EncodeError>,
|
O: Sink<packets::S2C, Error = std::io::Error>,
|
||||||
{
|
{
|
||||||
pub fn new(id: ConnId, i: I, o: O) -> Self {
|
pub fn new(id: ConnId, i: I, o: O) -> Self {
|
||||||
let (tx, rx) = unbounded_channel();
|
let (tx, rx) = unbounded_channel();
|
||||||
|
@ -116,7 +116,7 @@ where I: Stream<Item = ResultC2S> + Send,
|
||||||
packets::C2S::Pong() =>
|
packets::C2S::Pong() =>
|
||||||
(),
|
(),
|
||||||
packets::C2S::Connect(_) => {
|
packets::C2S::Connect(_) => {
|
||||||
to_send.push(err("Unexpected Connect", value::to_value(p).unwrap()));
|
to_send.push(err("Unexpected Connect", value::to_value(p)));
|
||||||
running = false;
|
running = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -184,7 +184,7 @@ where I: Stream<Item = ResultC2S> + Send,
|
||||||
|
|
||||||
impl<I, O> Drop for Peer<I, O>
|
impl<I, O> Drop for Peer<I, O>
|
||||||
where I: Stream<Item = ResultC2S> + Send,
|
where I: Stream<Item = ResultC2S> + Send,
|
||||||
O: Sink<packets::S2C, Error = packets::EncodeError>,
|
O: Sink<packets::S2C, Error = std::io::Error>,
|
||||||
{
|
{
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if let Some(ref s) = self.space {
|
if let Some(ref s) = self.space {
|
||||||
|
|
Loading…
Reference in New Issue