Skip to content

Commit 75d0643

Browse files
committed
add missing impl Errors
use `thiserror` to do this as it's already used in the crate. this updates it from v1 to v2 and also uses it in `no_std` environments as that's now supported by `thiserror`. this was missing and thus it wasn't possible to use e.g. `HostErr` in a `#[from]` with `thiserror`.
1 parent 6cba073 commit 75d0643

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

source/postcard-rpc/src/server/impls/test_channels.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::{
1818
Topic,
1919
};
2020
use core::fmt::Arguments;
21+
use thiserror::Error;
2122
use tokio::{select, sync::mpsc};
2223

2324
//////////////////////////////////////////////////////////////////////////////
@@ -212,9 +213,10 @@ impl WireTx for ChannelWireTx {
212213
}
213214

214215
/// A wire tx error
215-
#[derive(Debug)]
216+
#[derive(Debug, Error)]
216217
pub enum ChannelWireTxError {
217218
/// The receiver closed the channel
219+
#[error("channel closed")]
218220
ChannelClosed,
219221
}
220222

@@ -279,11 +281,13 @@ impl WireRx for ChannelWireRx {
279281
}
280282

281283
/// A wire rx error
282-
#[derive(Debug)]
284+
#[derive(Debug, Error)]
283285
pub enum ChannelWireRxError {
284286
/// The sender closed the channel
287+
#[error("channel closed")]
285288
ChannelClosed,
286289
/// The sender sent a too-large message
290+
#[error("message too large")]
287291
MessageTooLarge,
288292
}
289293

source/postcard-rpc/src/server/mod.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ pub mod impls;
2929

3030
use core::{fmt::Arguments, ops::DerefMut};
3131

32-
use postcard_schema::Schema;
33-
use serde::Serialize;
34-
3532
use crate::{
3633
header::{VarHeader, VarKey, VarKeyKind, VarSeq},
3734
DeviceMap, Key, TopicDirection,
3835
};
36+
use postcard_schema::Schema;
37+
use serde::Serialize;
38+
use thiserror::Error;
3939

4040
//////////////////////////////////////////////////////////////////////////////
4141
// TX
@@ -77,21 +77,24 @@ pub trait WireTx {
7777
}
7878

7979
/// The base [`WireTx`] Error Kind
80-
#[derive(Debug, Clone, Copy)]
80+
#[derive(Debug, Clone, Copy, Error)]
8181
#[non_exhaustive]
8282
pub enum WireTxErrorKind {
8383
/// The connection has been closed, and is unlikely to succeed until
8484
/// the connection is re-established. This will cause the Server run
8585
/// loop to terminate.
86+
#[error("connection closed")]
8687
ConnectionClosed,
8788
/// Other unspecified errors
89+
#[error("other")]
8890
Other,
8991
/// Timeout (WireTx impl specific) reached
92+
#[error("timeout reached")]
9093
Timeout,
9194
}
9295

9396
/// A conversion trait to convert a user error into a base Kind type
94-
pub trait AsWireTxErrorKind {
97+
pub trait AsWireTxErrorKind: core::error::Error {
9598
/// Convert the error type into a base type
9699
fn as_kind(&self) -> WireTxErrorKind;
97100
}
@@ -127,21 +130,24 @@ pub trait WireRx {
127130
}
128131

129132
/// The base [`WireRx`] Error Kind
130-
#[derive(Debug, Clone, Copy)]
133+
#[derive(Debug, Clone, Copy, Error)]
131134
#[non_exhaustive]
132135
pub enum WireRxErrorKind {
133136
/// The connection has been closed, and is unlikely to succeed until
134137
/// the connection is re-established. This will cause the Server run
135138
/// loop to terminate.
139+
#[error("connection closed")]
136140
ConnectionClosed,
137141
/// The received message was too large for the server to handle
142+
#[error("the received message was too large for the server to handle")]
138143
ReceivedMessageTooLarge,
139144
/// Other message kinds
145+
#[error("other")]
140146
Other,
141147
}
142148

143149
/// A conversion trait to convert a user error into a base Kind type
144-
pub trait AsWireRxErrorKind {
150+
pub trait AsWireRxErrorKind: core::error::Error {
145151
/// Convert the error type into a base type
146152
fn as_kind(&self) -> WireRxErrorKind;
147153
}
@@ -384,15 +390,18 @@ where
384390
}
385391

386392
/// A type representing the different errors [`Server::run()`] may return
393+
#[derive(Debug, Error)]
387394
pub enum ServerError<Tx, Rx>
388395
where
389396
Tx: WireTx,
390397
Rx: WireRx,
391398
{
392399
/// A fatal error occurred with the [`WireTx::send()`] implementation
393-
TxFatal(Tx::Error),
400+
#[error("A fatal error occurred while transmitting")]
401+
TxFatal(#[source] Tx::Error),
394402
/// A fatal error occurred with the [`WireRx::receive()`] implementation
395-
RxFatal(Rx::Error),
403+
#[error("A fatal error occurred while receiving")]
404+
RxFatal(#[source] Rx::Error),
396405
}
397406

398407
impl<Tx, Rx, Buf, D> Server<Tx, Rx, Buf, D>

0 commit comments

Comments
 (0)