diff --git a/src/errors.rs b/src/errors.rs index a4832de35..94aff5160 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -45,7 +45,7 @@ pub enum MpesaError { EncryptionError(#[from] openssl::error::ErrorStack), #[error("{0}")] Message(&'static str), - #[error("An error has occurred while building the request")] + #[error("An error has occurred while building the request: {0}")] BuilderError(BuilderError), } @@ -68,9 +68,9 @@ impl fmt::Display for ApiError { #[derive(Debug, Error)] pub enum BuilderError { - #[error("Field {0} is required")] + #[error("Field [{0}] is required")] UninitializedField(&'static str), - #[error("Field {0} is invalid")] + #[error("Field [{0}] is invalid")] ValidationError(String), } @@ -80,8 +80,14 @@ impl From for BuilderError { } } -impl From for BuilderError { +impl From for MpesaError { fn from(e: derive_builder::UninitializedFieldError) -> Self { - Self::UninitializedField(e.field_name()) + Self::BuilderError(BuilderError::UninitializedField(e.field_name())) + } +} + +impl From for MpesaError { + fn from(e: url::ParseError) -> Self { + Self::BuilderError(BuilderError::ValidationError(e.to_string())) } } diff --git a/src/services/express_request.rs b/src/services/express_request.rs index 13d7f586c..2a9a784ca 100644 --- a/src/services/express_request.rs +++ b/src/services/express_request.rs @@ -1,7 +1,7 @@ use crate::client::{Mpesa, MpesaResult}; use crate::constants::CommandId; use crate::environment::ApiEnvironment; -use crate::errors::{BuilderError, MpesaError}; +use crate::errors::MpesaError; use chrono::prelude::Local; use derive_builder::Builder; use openssl::base64; @@ -37,7 +37,7 @@ pub struct MpesaExpressRequestResponse { } #[derive(Builder, Debug, Clone)] -#[builder(build_fn(error = "BuilderError"))] +#[builder(build_fn(error = "MpesaError"))] pub struct MpesaExpress<'mpesa, Env: ApiEnvironment> { #[builder(pattern = "immutable")] client: &'mpesa Mpesa, diff --git a/src/services/transaction_reversal.rs b/src/services/transaction_reversal.rs index 38c2e81df..f740a194e 100644 --- a/src/services/transaction_reversal.rs +++ b/src/services/transaction_reversal.rs @@ -7,8 +7,8 @@ use crate::ApiEnvironment; use crate::CommandId; use crate::IdentifierTypes; use crate::Mpesa; +use crate::MpesaError; use crate::MpesaResult; -use crate::{BuilderError, MpesaError}; #[derive(Debug, Serialize)] #[serde(rename_all = "PascalCase")] @@ -46,7 +46,7 @@ pub struct TransactionReversalResponse { } #[derive(Builder, Debug)] -#[builder(build_fn(error = "BuilderError"))] +#[builder(build_fn(error = "MpesaError"))] pub struct TransactionReversal<'mpesa, Env: ApiEnvironment> { #[builder(pattern = "immutable")] client: &'mpesa Mpesa, diff --git a/tests/mpesa-rust/transaction_reversal_test.rs b/tests/mpesa-rust/transaction_reversal_test.rs index 23b29e543..40e76937d 100644 --- a/tests/mpesa-rust/transaction_reversal_test.rs +++ b/tests/mpesa-rust/transaction_reversal_test.rs @@ -71,7 +71,10 @@ async fn transaction_reversal_fails_if_no_transaction_id_is_provided() { .build() .unwrap_err(); - assert_eq!(err.to_string(), "Field transaction_id is required"); + assert_eq!( + err.to_string(), + "An error has occurred while building the request: Field [transaction_id] is required" + ); } #[tokio::test] @@ -88,7 +91,7 @@ async fn transaction_reversal_fails_if_no_amount_is_provided() { .expect(0) .mount(&server) .await; - if let Err(e) = client + let err = client .transaction_reversal() .initiator("testapi496") .try_result_url("https://testdomain.com/ok") @@ -98,17 +101,9 @@ async fn transaction_reversal_fails_if_no_amount_is_provided() { .transaction_id("OEI2AK4Q16") .receiver_party("600111") .build() - .unwrap() - .send() - .await - { - let MpesaError::Message(msg) = e else { - panic!("Expected MpesaError::Message, but found {}", e); - }; - assert_eq!(msg, "amount is required") - } else { - panic!("Expected error"); - } + .unwrap_err(); + + assert_eq!(err.to_string(), "Field amount is required"); } #[tokio::test]