Skip to content

Commit 28f41b3

Browse files
committed
Take JsonReply in extract_err_req
This simple type can more easily cross foreign language boundaries. Fix payjoin#605
1 parent c8dd102 commit 28f41b3

File tree

4 files changed

+29
-22
lines changed

4 files changed

+29
-22
lines changed

payjoin-cli/src/app/v2.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,8 @@ async fn handle_recoverable_error(
287287
mut receiver: UncheckedProposal,
288288
ohttp_relay: &payjoin::Url,
289289
) -> anyhow::Error {
290-
let (err_req, err_ctx) = match receiver.extract_err_req(&e, ohttp_relay) {
290+
let to_return = anyhow!("Replied with error: {}", e);
291+
let (err_req, err_ctx) = match receiver.extract_err_req(&e.into(), ohttp_relay) {
291292
Ok(req_ctx) => req_ctx,
292293
Err(e) => return anyhow!("Failed to extract error request: {}", e),
293294
};
@@ -306,7 +307,7 @@ async fn handle_recoverable_error(
306307
return anyhow!("Failed to process error response: {}", e);
307308
}
308309

309-
e.into()
310+
to_return
310311
}
311312

312313
fn try_contributing_inputs(

payjoin/src/receive/error.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,14 @@ impl JsonReply {
104104
}
105105
}
106106

107-
impl From<&ReplyableError> for JsonReply {
108-
fn from(e: &ReplyableError) -> Self {
107+
impl From<ReplyableError> for JsonReply {
108+
fn from(e: ReplyableError) -> Self {
109+
use ReplyableError::*;
109110
match e {
110-
ReplyableError::Payload(e) => e.into(),
111+
Payload(e) => e.into(),
111112
#[cfg(feature = "v1")]
112-
ReplyableError::V1(e) => e.into(),
113-
ReplyableError::Implementation(_) => JsonReply::new(Unavailable, "Receiver error"),
113+
V1(e) => e.into(),
114+
Implementation(_) => JsonReply::new(Unavailable, "Receiver error"),
114115
}
115116
}
116117
}
@@ -196,8 +197,8 @@ pub(crate) enum InternalPayloadError {
196197
FeeTooHigh(bitcoin::FeeRate, bitcoin::FeeRate),
197198
}
198199

199-
impl From<&PayloadError> for JsonReply {
200-
fn from(e: &PayloadError) -> Self {
200+
impl From<PayloadError> for JsonReply {
201+
fn from(e: PayloadError) -> Self {
201202
use InternalPayloadError::*;
202203

203204
match &e.0 {

payjoin/src/receive/v1/exclusive/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ impl From<InternalRequestError> for super::ReplyableError {
3838
fn from(e: InternalRequestError) -> Self { super::ReplyableError::V1(e.into()) }
3939
}
4040

41-
impl From<&RequestError> for JsonReply {
42-
fn from(e: &RequestError) -> Self {
41+
impl From<RequestError> for JsonReply {
42+
fn from(e: RequestError) -> Self {
4343
use InternalRequestError::*;
4444

4545
match &e.0 {

payjoin/src/receive/v2/mod.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -278,15 +278,15 @@ impl UncheckedProposal {
278278
/// a Receiver Error Response
279279
pub fn extract_err_req(
280280
&mut self,
281-
err: &ReplyableError,
281+
err: &JsonReply,
282282
ohttp_relay: impl IntoUrl,
283283
) -> Result<(Request, ohttp::ClientResponse), SessionError> {
284284
let subdir = subdir(&self.context.directory, &id(&self.context.s));
285285
let (body, ohttp_ctx) = ohttp_encapsulate(
286286
&mut self.context.ohttp_keys,
287287
"POST",
288288
subdir.as_str(),
289-
Some(JsonReply::from(err).to_json().to_string().as_bytes()),
289+
Some(err.to_json().to_string().as_bytes()),
290290
)
291291
.map_err(InternalSessionError::OhttpEncapsulation)?;
292292
let req = Request::new_v2(&self.context.full_relay_url(ohttp_relay)?, &body);
@@ -620,21 +620,26 @@ mod test {
620620
context: SHARED_CONTEXT.clone(),
621621
};
622622

623-
let server_error = proposal
624-
.clone()
625-
.check_broadcast_suitability(None, |_| Err("mock error".into()))
626-
.err()
627-
.ok_or("expected error but got success")?;
623+
let server_error = || {
624+
proposal
625+
.clone()
626+
.check_broadcast_suitability(None, |_| Err("mock error".into()))
627+
.expect_err("expected broadcast suitability check to fail")
628+
};
629+
628630
let expected_json = serde_json::json!({
629631
"errorCode": "unavailable",
630632
"message": "Receiver error"
631633
});
632-
let actual_json = JsonReply::from(&server_error).to_json();
634+
635+
let actual_json = JsonReply::from(server_error()).to_json().clone();
633636
assert_eq!(actual_json, expected_json);
634-
let (_req, _ctx) = proposal.clone().extract_err_req(&server_error, &*EXAMPLE_URL)?;
635637

636-
let internal_error = InternalPayloadError::MissingPayment.into();
637-
let (_req, _ctx) = proposal.extract_err_req(&internal_error, &*EXAMPLE_URL)?;
638+
let (_req, _ctx) =
639+
proposal.clone().extract_err_req(&server_error().into(), &*EXAMPLE_URL)?;
640+
641+
let internal_error: ReplyableError = InternalPayloadError::MissingPayment.into();
642+
let (_req, _ctx) = proposal.extract_err_req(&internal_error.into(), &*EXAMPLE_URL)?;
638643
Ok(())
639644
}
640645

0 commit comments

Comments
 (0)