Skip to content

Commit 4ec7cbc

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 c0a567b commit 4ec7cbc

6 files changed

Lines changed: 50 additions & 54 deletions

File tree

example/workbook-host/Cargo.lock

Lines changed: 5 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/postcard-rpc-test/Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/postcard-rpc/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ version = "0.3.69"
6464
optional = true
6565

6666
[dependencies.thiserror]
67-
version = "1.0"
68-
optional = true
67+
version = "2"
68+
default-features = false
6969

7070
[dependencies.web-sys]
7171

@@ -195,7 +195,7 @@ use-std = [
195195
"dep:tokio",
196196
"postcard/use-std",
197197
"postcard-schema/use-std",
198-
"dep:thiserror",
198+
"thiserror/std",
199199
"dep:tracing",
200200
"dep:trait-variant",
201201
"dep:ssmarshal",

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use std::{
1313
Arc, RwLock,
1414
},
1515
};
16-
1716
use thiserror::Error;
1817

1918
use maitake_sync::{
@@ -52,28 +51,26 @@ pub(crate) mod util;
5251
pub mod test_channels;
5352

5453
/// Host Error Kind
55-
#[derive(Debug, PartialEq)]
54+
#[derive(Debug, PartialEq, Error)]
5655
pub enum HostErr<WireErr> {
5756
/// An error of the user-specified wire error type
57+
#[error("a wire error occurred")]
5858
Wire(WireErr),
5959
/// We got a response that didn't match the expected value or the
6060
/// user specified wire error type
6161
///
6262
/// This is also (misused) to report when duplicate sequence numbers
6363
/// in-flight at the same time are detected.
64+
#[error("the response received didn't match the expected value or the wire error type")]
6465
BadResponse,
6566
/// Deserialization of the message failed
66-
Postcard(postcard::Error),
67+
#[error("message deserialization failed")]
68+
Postcard(#[from] postcard::Error),
6769
/// The interface has been closed, and no further messages are possible
70+
#[error("the interface has been closed, and no further messages are possible")]
6871
Closed,
6972
}
7073

71-
impl<T> From<postcard::Error> for HostErr<T> {
72-
fn from(value: postcard::Error) -> Self {
73-
Self::Postcard(value)
74-
}
75-
}
76-
7774
impl<T> From<WaitError> for HostErr<T> {
7875
fn from(_: WaitError) -> Self {
7976
Self::Closed
@@ -230,17 +227,23 @@ where
230227
}
231228

232229
/// Errors related to retrieving the schema
233-
#[derive(Debug)]
230+
#[derive(Debug, Error)]
234231
pub enum SchemaError<WireErr> {
235232
/// Some kind of communication error occurred
236-
Comms(HostErr<WireErr>),
233+
#[error("A communication error occurred")]
234+
Comms(#[from] HostErr<WireErr>),
237235
/// An error occurred internally. Please open an issue.
236+
#[error("An error occurred internally. Please open an issue.")]
238237
TaskError,
239238
/// Invalid report data was received, including endpoints or
240239
/// tasks that referred to unknown types. Please open an issue
240+
#[error("Invalid report data was received. Please open an issue.")]
241241
InvalidReportData,
242242
/// Data was lost while transmitting. If a retry does not solve
243243
/// this, please open an issue.
244+
#[error(
245+
"Data was lost while transmitting. If a retry does not solve this, please open an issue."
246+
)]
244247
LostData,
245248
}
246249

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)