From 70c76e41cb3debcd1f84494fc70ec30f91eaad94 Mon Sep 17 00:00:00 2001 From: Dominic Leutenegger Date: Fri, 13 Oct 2023 14:29:44 +0200 Subject: [PATCH 1/4] Add error codes to topup --- crow/src/lib.rs | 171 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 169 insertions(+), 2 deletions(-) diff --git a/crow/src/lib.rs b/crow/src/lib.rs index 55a6fc3..d19b355 100644 --- a/crow/src/lib.rs +++ b/crow/src/lib.rs @@ -21,6 +21,31 @@ pub enum TopupStatus { SETTLED, } +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum TemporaryFailureCode { + NoRoute, + InvoiceExpired, + Unexpected, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum PermanentFailureCode { + ThresholdExceeded, + OrderInactive, + CompaniesUnsupported, + CountryUnsupported, + OtherRiskDetected, + CustomerRequested, + AccountNotMatching, + PayoutExpired, +} + +#[derive(PartialEq, Eq, Debug, Clone)] +pub enum TopupError { + TemporaryFailure { code: TemporaryFailureCode }, + PermanentFailure { code: PermanentFailureCode }, +} + pub struct TopupInfo { pub id: String, pub status: TopupStatus, @@ -33,6 +58,7 @@ pub struct TopupInfo { pub expires_at: SystemTime, pub lnurlw: String, + pub error: Option, } pub struct OfferManager { @@ -146,6 +172,12 @@ fn to_topup_info(topup: ListUncompletedTopupsTopup) -> graphql::Result to_topup_error(topup.additional_info), + topup_status_enum::REFUNDED => to_topup_error(topup.additional_info), + _ => None, + }; + Ok(TopupInfo { id: topup.id, status, @@ -158,6 +190,71 @@ fn to_topup_info(topup: ListUncompletedTopupsTopup) -> graphql::Result) -> Option { + error.map(|e| { + match e { + TopupError::TemporaryFailure { code } => match code { + TemporaryFailureCode::NoRoute => "no_route", + TemporaryFailureCode::InvoiceExpired => "invoice_expired", + TemporaryFailureCode::Unexpected => "error", + }, + TopupError::PermanentFailure { code } => match code { + PermanentFailureCode::ThresholdExceeded => "threshold_exceeded", + PermanentFailureCode::OrderInactive => "order_inactive", + PermanentFailureCode::CompaniesUnsupported => "companies_unsupported", + PermanentFailureCode::CountryUnsupported => "country_unsupported", + PermanentFailureCode::OtherRiskDetected => "other_risk_detected", + PermanentFailureCode::CustomerRequested => "customer_requested", + PermanentFailureCode::AccountNotMatching => "account_not_matching", + PermanentFailureCode::PayoutExpired => "payout_expired", + }, + } + .to_string() + }) +} + +pub fn to_topup_error(code: Option) -> Option { + code.map(|c| match &*c { + "no_route" => TopupError::TemporaryFailure { + code: TemporaryFailureCode::NoRoute, + }, + "invoice_expired" => TopupError::TemporaryFailure { + code: TemporaryFailureCode::InvoiceExpired, + }, + "error" => TopupError::TemporaryFailure { + code: TemporaryFailureCode::Unexpected, + }, + "threshold_exceeded" => TopupError::PermanentFailure { + code: PermanentFailureCode::ThresholdExceeded, + }, + "order_inactive" => TopupError::PermanentFailure { + code: PermanentFailureCode::OrderInactive, + }, + "companies_unsupported" => TopupError::PermanentFailure { + code: PermanentFailureCode::CompaniesUnsupported, + }, + "country_unsupported" => TopupError::PermanentFailure { + code: PermanentFailureCode::CountryUnsupported, + }, + "other_risk_detected" => TopupError::PermanentFailure { + code: PermanentFailureCode::OtherRiskDetected, + }, + "customer_requested" => TopupError::PermanentFailure { + code: PermanentFailureCode::CustomerRequested, + }, + "account_not_matching" => TopupError::PermanentFailure { + code: PermanentFailureCode::AccountNotMatching, + }, + "payout_expired" => TopupError::PermanentFailure { + code: PermanentFailureCode::PayoutExpired, + }, + _ => TopupError::TemporaryFailure { + code: TemporaryFailureCode::Unexpected, + }, }) } @@ -165,7 +262,9 @@ fn to_topup_info(topup: ListUncompletedTopupsTopup) -> graphql::Result { + panic!("Expected topup to have error code, was None") + } + Some(e) => match e { + TopupError::TemporaryFailure { code } => { + assert_eq!(code, TemporaryFailureCode::NoRoute) + } + TopupError::PermanentFailure { .. } => { + panic!("Expected topup to have temporary failure, was permanent") + } + }, + } + + let topup = ListUncompletedTopupsTopup { + additional_info: Some("customer_requested".to_string()), + amount_sat: 42578, + amount_user_currency, + created_at: "2023-07-21T16:39:21.271+00:00".to_string(), + exchange_fee_rate: 0.014999999664723873, + exchange_fee_user_currency: 0.11999999731779099, + exchange_rate: 18507.0, + expires_at: "2023-09-21T16:39:21.919+00:00".to_string(), + id: "1707e09e-ebe1-4004-abd7-7a64604501b3".to_string(), + lightning_fee_user_currency: 0.0, + lnurl: LNURL.to_string(), + node_pub_key: "0233786a3f5c79d25508ed973e7a37506ddab49d41a07fcb3d341ab638000d69cf" + .to_string(), + status: topup_status_enum::REFUNDED, + user_currency: "eur".to_string(), + }; + + let topup_info = to_topup_info(topup).unwrap(); + + match topup_info.error { + None => { + panic!("Expected topup to have error code, was None") + } + Some(e) => match e { + TopupError::TemporaryFailure { .. } => { + panic!("Expected topup to have permanent failure, was temporary") + } + TopupError::PermanentFailure { code } => { + assert_eq!(code, PermanentFailureCode::CustomerRequested) + } + }, + } } } From 1c2d6d32770c17899eaa29b4060644b38a3af56a Mon Sep 17 00:00:00 2001 From: Dominic Date: Fri, 13 Oct 2023 17:15:40 +0200 Subject: [PATCH 2/4] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Daniel Granhão <32176319+danielgranhao@users.noreply.github.com> --- crow/src/lib.rs | 38 ++++++++++++-------------------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/crow/src/lib.rs b/crow/src/lib.rs index d19b355..798f8a3 100644 --- a/crow/src/lib.rs +++ b/crow/src/lib.rs @@ -336,19 +336,12 @@ mod tests { let topup_info = to_topup_info(topup).unwrap(); - match topup_info.error { - None => { - panic!("Expected topup to have error code, was None") - } - Some(e) => match e { - TopupError::TemporaryFailure { code } => { - assert_eq!(code, TemporaryFailureCode::NoRoute) - } - TopupError::PermanentFailure { .. } => { - panic!("Expected topup to have temporary failure, was permanent") - } - }, - } + assert!(matches!( + topup_info.error, + Some(TopupError::TemporaryFailure { + code: TemporaryFailureCode::NoRoute + }) + )); let topup = ListUncompletedTopupsTopup { additional_info: Some("customer_requested".to_string()), @@ -370,18 +363,11 @@ mod tests { let topup_info = to_topup_info(topup).unwrap(); - match topup_info.error { - None => { - panic!("Expected topup to have error code, was None") - } - Some(e) => match e { - TopupError::TemporaryFailure { .. } => { - panic!("Expected topup to have permanent failure, was temporary") - } - TopupError::PermanentFailure { code } => { - assert_eq!(code, PermanentFailureCode::CustomerRequested) - } - }, - } + assert!(matches!( + topup_info.error, + Some(TopupError::PermanentFailure { + code: PermanentFailureCode::CustomerRequested + }) + )); } } From c8d85a6f1ea0aae77124ba62b651578865b2eebf Mon Sep 17 00:00:00 2001 From: Dominic Leutenegger Date: Fri, 13 Oct 2023 17:16:26 +0200 Subject: [PATCH 3/4] Remove `from_topup_error` --- crow/src/lib.rs | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/crow/src/lib.rs b/crow/src/lib.rs index 798f8a3..1d3d830 100644 --- a/crow/src/lib.rs +++ b/crow/src/lib.rs @@ -194,29 +194,6 @@ fn to_topup_info(topup: ListUncompletedTopupsTopup) -> graphql::Result) -> Option { - error.map(|e| { - match e { - TopupError::TemporaryFailure { code } => match code { - TemporaryFailureCode::NoRoute => "no_route", - TemporaryFailureCode::InvoiceExpired => "invoice_expired", - TemporaryFailureCode::Unexpected => "error", - }, - TopupError::PermanentFailure { code } => match code { - PermanentFailureCode::ThresholdExceeded => "threshold_exceeded", - PermanentFailureCode::OrderInactive => "order_inactive", - PermanentFailureCode::CompaniesUnsupported => "companies_unsupported", - PermanentFailureCode::CountryUnsupported => "country_unsupported", - PermanentFailureCode::OtherRiskDetected => "other_risk_detected", - PermanentFailureCode::CustomerRequested => "customer_requested", - PermanentFailureCode::AccountNotMatching => "account_not_matching", - PermanentFailureCode::PayoutExpired => "payout_expired", - }, - } - .to_string() - }) -} - pub fn to_topup_error(code: Option) -> Option { code.map(|c| match &*c { "no_route" => TopupError::TemporaryFailure { From 672c429e3e88f0c04b7f2d9200bc03f679ff0c0d Mon Sep 17 00:00:00 2001 From: Dominic Leutenegger Date: Fri, 13 Oct 2023 17:21:56 +0200 Subject: [PATCH 4/4] Add unkown error code --- crow/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crow/src/lib.rs b/crow/src/lib.rs index 1d3d830..a57b8cf 100644 --- a/crow/src/lib.rs +++ b/crow/src/lib.rs @@ -26,6 +26,7 @@ pub enum TemporaryFailureCode { NoRoute, InvoiceExpired, Unexpected, + Unknown { msg: String }, } #[derive(Clone, Debug, PartialEq, Eq)] @@ -229,8 +230,8 @@ pub fn to_topup_error(code: Option) -> Option { "payout_expired" => TopupError::PermanentFailure { code: PermanentFailureCode::PayoutExpired, }, - _ => TopupError::TemporaryFailure { - code: TemporaryFailureCode::Unexpected, + e => TopupError::TemporaryFailure { + code: TemporaryFailureCode::Unknown { msg: e.to_string() }, }, }) }