Skip to content

Commit 07df5e4

Browse files
committed
Expose standard Rust traits on FFI error types
This fixes the string representation of the FFI errors to use the Rust Display string instead of an opaque `Instance of *Error` message.
1 parent 592f914 commit 07df5e4

6 files changed

Lines changed: 24 additions & 0 deletions

File tree

payjoin-ffi/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::error;
44
///
55
/// e.g. database errors, network failures, wallet errors
66
#[derive(Debug, thiserror::Error, uniffi::Object)]
7+
#[uniffi::export(Debug, Display)]
78
#[error(transparent)]
89
pub struct ImplementationError(#[from] payjoin::ImplementationError);
910

@@ -18,6 +19,7 @@ impl From<ImplementationError> for payjoin::ImplementationError {
1819
}
1920

2021
#[derive(Debug, thiserror::Error, uniffi::Object)]
22+
#[uniffi::export(Debug, Display)]
2123
#[error("Error de/serializing JSON object: {0}")]
2224
pub struct SerdeJsonError(#[from] serde_json::Error);
2325

payjoin-ffi/src/ohttp.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub use error::OhttpError;
22

33
pub mod error {
44
#[derive(Debug, thiserror::Error, uniffi::Object)]
5+
#[uniffi::export(Debug, Display)]
56
#[error(transparent)]
67
pub struct OhttpError(#[from] ohttp::Error);
78
}

payjoin-ffi/src/receive/error.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ impl From<payjoin::bitcoin::address::ParseError> for ReceiverBuilderError {
115115

116116
/// Error parsing a Bitcoin address.
117117
#[derive(Debug, thiserror::Error, uniffi::Object)]
118+
#[uniffi::export(Debug, Display)]
118119
#[error("Invalid Bitcoin address: {msg}")]
119120
pub struct AddressParseError {
120121
msg: String,
@@ -136,6 +137,7 @@ impl From<payjoin::bitcoin::address::ParseError> for AddressParseError {
136137
/// 4. Provide errors according to BIP-78 JSON error specifications for return
137138
/// after conversion into [`JsonReply`]
138139
#[derive(Debug, thiserror::Error, uniffi::Object)]
140+
#[uniffi::export(Debug, Display)]
139141
#[error(transparent)]
140142
pub struct ProtocolError(#[from] receive::ProtocolError);
141143

@@ -149,6 +151,7 @@ pub struct ProtocolError(#[from] receive::ProtocolError);
149151
/// }
150152
/// ```
151153
#[derive(Debug, Clone, PartialEq, Eq, uniffi::Object)]
154+
#[uniffi::export(Debug, Eq)]
152155
pub struct JsonReply(receive::JsonReply);
153156

154157
impl From<JsonReply> for receive::JsonReply {
@@ -165,11 +168,13 @@ impl From<ProtocolError> for JsonReply {
165168

166169
/// Error that may occur during a v2 session typestate change
167170
#[derive(Debug, thiserror::Error, uniffi::Object)]
171+
#[uniffi::export(Debug, Display)]
168172
#[error(transparent)]
169173
pub struct SessionError(#[from] receive::v2::SessionError);
170174

171175
/// Protocol error raised during output substitution.
172176
#[derive(Debug, thiserror::Error, uniffi::Object)]
177+
#[uniffi::export(Debug, Display)]
173178
#[error(transparent)]
174179
pub struct OutputSubstitutionProtocolError(#[from] receive::OutputSubstitutionError);
175180

@@ -194,16 +199,19 @@ impl From<FfiValidationError> for OutputSubstitutionError {
194199

195200
/// Error that may occur when coin selection fails.
196201
#[derive(Debug, thiserror::Error, uniffi::Object)]
202+
#[uniffi::export(Debug, Display)]
197203
#[error(transparent)]
198204
pub struct SelectionError(#[from] receive::SelectionError);
199205

200206
/// Error that may occur when input contribution fails.
201207
#[derive(Debug, thiserror::Error, uniffi::Object)]
208+
#[uniffi::export(Debug, Display)]
202209
#[error(transparent)]
203210
pub struct InputContributionError(#[from] receive::InputContributionError);
204211

205212
/// Error validating a PSBT Input
206213
#[derive(Debug, thiserror::Error, uniffi::Object)]
214+
#[uniffi::export(Debug, Display)]
207215
#[error(transparent)]
208216
pub struct PsbtInputError(#[from] receive::PsbtInputError);
209217

@@ -233,6 +241,7 @@ impl From<FfiValidationError> for InputPairError {
233241

234242
/// Error that may occur when a receiver event log is replayed
235243
#[derive(Debug, thiserror::Error, uniffi::Object)]
244+
#[uniffi::export(Debug, Display)]
236245
#[error(transparent)]
237246
pub struct ReceiverReplayError(
238247
#[from] payjoin::error::ReplayError<receive::v2::ReceiveSession, receive::v2::SessionEvent>,

payjoin-ffi/src/send/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::error::{FfiValidationError, ImplementationError};
99
///
1010
/// This error is unrecoverable.
1111
#[derive(Debug, PartialEq, Eq, thiserror::Error, uniffi::Object)]
12+
#[uniffi::export(Debug, Display, Eq)]
1213
#[error("Error initializing the sender: {msg}")]
1314
pub struct BuildSenderError {
1415
msg: String,
@@ -55,16 +56,19 @@ impl From<FfiValidationError> for SenderInputError {
5556
/// `unwrap()`ing it is thus considered OK in Rust but you may achieve nicer message by displaying
5657
/// it.
5758
#[derive(Debug, thiserror::Error, uniffi::Object)]
59+
#[uniffi::export(Debug, Display)]
5860
#[error(transparent)]
5961
pub struct CreateRequestError(#[from] send::v2::CreateRequestError);
6062

6163
/// Error returned for v2-specific payload encapsulation errors.
6264
#[derive(Debug, thiserror::Error, uniffi::Object)]
65+
#[uniffi::export(Debug, Display)]
6366
#[error(transparent)]
6467
pub struct EncapsulationError(#[from] send::v2::EncapsulationError);
6568

6669
/// Error that may occur when the response from receiver is malformed.
6770
#[derive(Debug, thiserror::Error, uniffi::Object)]
71+
#[uniffi::export(Debug, Display)]
6872
#[error(transparent)]
6973
pub struct ValidationError(#[from] send::ValidationError);
7074

@@ -106,11 +110,13 @@ impl From<send::ResponseError> for ResponseError {
106110

107111
/// A well-known error that can be safely displayed to end users.
108112
#[derive(Debug, thiserror::Error, uniffi::Object)]
113+
#[uniffi::export(Debug, Display)]
109114
#[error(transparent)]
110115
pub struct WellKnownError(#[from] send::WellKnownError);
111116

112117
/// Error that may occur when the sender session event log is replayed
113118
#[derive(Debug, thiserror::Error, uniffi::Object)]
119+
#[uniffi::export(Debug, Display)]
114120
#[error(transparent)]
115121
pub struct SenderReplayError(
116122
#[from] payjoin::error::ReplayError<send::v2::SendSession, send::v2::SessionEvent>,

payjoin-ffi/src/test_utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ impl FfiError {
7878
}
7979

8080
#[derive(Debug, thiserror::Error, uniffi::Object)]
81+
#[uniffi::export(Debug, Display)]
8182
#[error(transparent)]
8283
pub struct BoxSendSyncError(#[from] payjoin_test_utils::BoxSendSyncError);
8384

payjoin-ffi/src/uri/error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#[derive(Debug, PartialEq, Eq, thiserror::Error, uniffi::Object)]
2+
#[uniffi::export(Debug, Display, Eq)]
23
#[error("Error parsing the payjoin URI: {msg}")]
34
pub struct PjParseError {
45
msg: String,
@@ -9,6 +10,7 @@ impl PjParseError {
910
}
1011

1112
#[derive(Debug, PartialEq, Eq, thiserror::Error, uniffi::Object)]
13+
#[uniffi::export(Debug, Display, Eq)]
1214
#[error("URI doesn't support payjoin: {msg}")]
1315
pub struct PjNotSupported {
1416
msg: String,
@@ -21,14 +23,17 @@ impl PjNotSupported {
2123
}
2224

2325
#[derive(Debug, thiserror::Error, uniffi::Object)]
26+
#[uniffi::export(Debug, Display)]
2427
#[error(transparent)]
2528
pub struct UrlParseError(#[from] url::ParseError);
2629

2730
#[derive(Debug, thiserror::Error, uniffi::Object)]
31+
#[uniffi::export(Debug, Display)]
2832
#[error(transparent)]
2933
pub struct IntoUrlError(#[from] payjoin::IntoUrlError);
3034

3135
#[derive(Debug, thiserror::Error, uniffi::Object)]
36+
#[uniffi::export(Debug, Display)]
3237
#[error("{msg}")]
3338
pub struct FeeRateError {
3439
msg: String,

0 commit comments

Comments
 (0)