Skip to content

Commit 40e1035

Browse files
authored
Rename FFI error types to avoid naming conflicts (payjoin#830)
The first commit renames receive::Error to ReceiverError to avoid conflicts in languages where Error is a reserved keyword. The second commit renames TerminalError to TerminalErr as a workaround to a quirk in uniffi-dart (see commit message for more detail)
2 parents d802c9d + dd70c9f commit 40e1035

File tree

3 files changed

+41
-25
lines changed

3 files changed

+41
-25
lines changed

payjoin-ffi/src/receive/error.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::uri::error::IntoUrlError;
99
#[derive(Debug, thiserror::Error)]
1010
#[non_exhaustive]
1111
#[cfg_attr(feature = "uniffi", derive(uniffi::Error))]
12-
pub enum Error {
12+
pub enum ReceiverError {
1313
/// Errors that can be replied to the sender
1414
#[error("Replyable error: {0}")]
1515
ReplyToSender(Arc<ReplyableError>),
@@ -24,12 +24,14 @@ pub enum Error {
2424
Unexpected,
2525
}
2626

27-
impl From<receive::Error> for Error {
27+
impl From<receive::Error> for ReceiverError {
2828
fn from(value: receive::Error) -> Self {
29+
use ReceiverError::*;
30+
2931
match value {
30-
receive::Error::ReplyToSender(e) => Error::ReplyToSender(Arc::new(ReplyableError(e))),
31-
receive::Error::V2(e) => Error::V2(Arc::new(SessionError(e))),
32-
_ => Error::Unexpected,
32+
receive::Error::ReplyToSender(e) => ReplyToSender(Arc::new(ReplyableError(e))),
33+
receive::Error::V2(e) => V2(Arc::new(SessionError(e))),
34+
_ => Unexpected,
3335
}
3436
}
3537
}
@@ -41,7 +43,7 @@ impl From<receive::Error> for Error {
4143
pub enum ReceiverPersistedError {
4244
/// rust-payjoin receiver error
4345
#[error(transparent)]
44-
Receiver(Error),
46+
Receiver(ReceiverError),
4547
/// Storage error that could occur at application storage layer
4648
#[error(transparent)]
4749
Storage(Arc<ImplementationError>),
@@ -69,21 +71,21 @@ macro_rules! impl_persisted_error_from {
6971
if let Some(api_err) = err.api_error() {
7072
return ReceiverPersistedError::Receiver($receiver_arm(api_err));
7173
}
72-
ReceiverPersistedError::Receiver(Error::Unexpected)
74+
ReceiverPersistedError::Receiver(ReceiverError::Unexpected)
7375
}
7476
}
7577
};
7678
}
7779

7880
impl_persisted_error_from!(receive::ReplyableError, |api_err: receive::ReplyableError| {
79-
Error::ReplyToSender(Arc::new(api_err.into()))
81+
ReceiverError::ReplyToSender(Arc::new(api_err.into()))
8082
});
8183

8284
impl_persisted_error_from!(receive::Error, |api_err: receive::Error| api_err.into());
8385

84-
impl_persisted_error_from!(payjoin::IntoUrlError, |api_err: payjoin::IntoUrlError| Error::IntoUrl(
85-
Arc::new(api_err.into())
86-
));
86+
impl_persisted_error_from!(payjoin::IntoUrlError, |api_err: payjoin::IntoUrlError| {
87+
ReceiverError::IntoUrl(Arc::new(api_err.into()))
88+
});
8789

8890
/// The replyable error type for the payjoin receiver, representing failures need to be
8991
/// returned to the sender.

payjoin-ffi/src/receive/mod.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::{Arc, RwLock};
33
use std::time::Duration;
44

55
pub use error::{
6-
Error, InputContributionError, JsonReply, OutputSubstitutionError, PsbtInputError,
6+
InputContributionError, JsonReply, OutputSubstitutionError, PsbtInputError, ReceiverError,
77
ReplyableError, SelectionError, SessionError,
88
};
99
use payjoin::bitcoin::psbt::Psbt;
@@ -157,7 +157,10 @@ pub struct InitializedTransition(
157157
);
158158

159159
impl InitializedTransition {
160-
pub fn save<P>(&self, persister: &P) -> Result<InitializedTransitionOutcome, ReceiverPersistedError>
160+
pub fn save<P>(
161+
&self,
162+
persister: &P,
163+
) -> Result<InitializedTransitionOutcome, ReceiverPersistedError>
161164
where
162165
P: SessionPersister<SessionEvent = payjoin::receive::v2::SessionEvent>,
163166
{
@@ -209,7 +212,10 @@ impl
209212
}
210213

211214
impl Initialized {
212-
pub fn extract_req(&self, ohttp_relay: String) -> Result<(Request, ClientResponse), Error> {
215+
pub fn extract_req(
216+
&self,
217+
ohttp_relay: String,
218+
) -> Result<(Request, ClientResponse), ReceiverError> {
213219
self.0
214220
.clone()
215221
.extract_req(ohttp_relay)
@@ -568,7 +574,9 @@ impl WantsOutputsTransition {
568574
.take()
569575
.ok_or_else(|| ImplementationError::from("Already saved or moved".to_string()))?;
570576

571-
let res = value.save(persister).map_err(|e| ReceiverPersistedError::Storage(Arc::new(ImplementationError::from(e.to_string()))))?;
577+
let res = value.save(persister).map_err(|e| {
578+
ReceiverPersistedError::Storage(Arc::new(ImplementationError::from(e.to_string())))
579+
})?;
572580
Ok(res.into())
573581
}
574582
}
@@ -639,7 +647,9 @@ impl WantsInputsTransition {
639647
.take()
640648
.ok_or_else(|| ImplementationError::from("Already saved or moved".to_string()))?;
641649

642-
let res = value.save(persister).map_err(|e| ReceiverPersistedError::Storage(Arc::new(ImplementationError::from(e.to_string()))))?;
650+
let res = value.save(persister).map_err(|e| {
651+
ReceiverPersistedError::Storage(Arc::new(ImplementationError::from(e.to_string())))
652+
})?;
643653
Ok(res.into())
644654
}
645655
}
@@ -833,7 +843,10 @@ impl PayjoinProposal {
833843
}
834844

835845
/// Extract an OHTTP Encapsulated HTTP POST request for the Proposal PSBT
836-
pub fn extract_req(&self, ohttp_relay: String) -> Result<(Request, ClientResponse), Error> {
846+
pub fn extract_req(
847+
&self,
848+
ohttp_relay: String,
849+
) -> Result<(Request, ClientResponse), ReceiverError> {
837850
self.0
838851
.clone()
839852
.extract_req(ohttp_relay)

payjoin-ffi/src/receive/uni.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::bitcoin_ffi::{Address, OutPoint, Script, TxOut};
55
use crate::error::ForeignError;
66
use crate::receive::error::{ReceiverPersistedError, ReceiverReplayError};
77
pub use crate::receive::{
8-
Error, ImplementationError, InputContributionError, JsonReply, OutputSubstitutionError,
8+
ImplementationError, InputContributionError, JsonReply, OutputSubstitutionError, ReceiverError,
99
ReplyableError, SelectionError, SerdeJsonError, SessionError,
1010
};
1111
use crate::{ClientResponse, OhttpKeys, OutputSubstitution, Request};
@@ -93,13 +93,13 @@ impl From<SessionHistory> for super::SessionHistory {
9393
}
9494

9595
#[derive(uniffi::Object)]
96-
pub struct TerminalError {
96+
pub struct TerminalErr {
9797
error: String,
9898
reply: Option<JsonReply>,
9999
}
100100

101101
#[uniffi::export]
102-
impl TerminalError {
102+
impl TerminalErr {
103103
pub fn error(&self) -> String { self.error.clone() }
104104

105105
pub fn reply(&self) -> Option<Arc<JsonReply>> {
@@ -120,9 +120,9 @@ impl SessionHistory {
120120
}
121121

122122
/// Terminal error from the session if present
123-
pub fn terminal_error(&self) -> Option<Arc<TerminalError>> {
123+
pub fn terminal_error(&self) -> Option<Arc<TerminalErr>> {
124124
self.0 .0.terminal_error().map(|(error, reply)| {
125-
Arc::new(TerminalError { error, reply: reply.map(|reply| reply.into()) })
125+
Arc::new(TerminalErr { error, reply: reply.map(|reply| reply.into()) })
126126
})
127127
}
128128

@@ -166,7 +166,8 @@ pub fn replay_receiver_event_log(
166166
persister: Arc<dyn JsonReceiverSessionPersister>,
167167
) -> Result<ReplayResult, ReceiverReplayError> {
168168
let adapter = CallbackPersisterAdapter::new(persister);
169-
let (state, session_history) = super::replay_event_log(&adapter).map_err(ReceiverReplayError::from)?;
169+
let (state, session_history) =
170+
super::replay_event_log(&adapter).map_err(ReceiverReplayError::from)?;
170171
Ok(ReplayResult { state: state.into(), session_history: session_history.into() })
171172
}
172173
#[derive(uniffi::Object)]
@@ -275,7 +276,7 @@ impl Initialized {
275276
/// This identifies a session at the payjoin directory server.
276277
pub fn pj_uri(&self) -> crate::PjUri { self.0.pj_uri() }
277278

278-
pub fn extract_req(&self, ohttp_relay: String) -> Result<RequestResponse, Error> {
279+
pub fn extract_req(&self, ohttp_relay: String) -> Result<RequestResponse, ReceiverError> {
279280
self.0
280281
.extract_req(ohttp_relay)
281282
.map(|(request, ctx)| RequestResponse { request, client_response: Arc::new(ctx) })
@@ -719,7 +720,7 @@ impl PayjoinProposal {
719720
pub fn psbt(&self) -> String { self.0.psbt() }
720721

721722
/// Extract an OHTTP Encapsulated HTTP POST request for the Proposal PSBT
722-
pub fn extract_req(&self, ohttp_relay: String) -> Result<RequestResponse, Error> {
723+
pub fn extract_req(&self, ohttp_relay: String) -> Result<RequestResponse, ReceiverError> {
723724
let (req, res) = self.0.extract_req(ohttp_relay)?;
724725
Ok(RequestResponse { request: req, client_response: Arc::new(res) })
725726
}

0 commit comments

Comments
 (0)