Skip to content

Commit

Permalink
chore: add builder errors
Browse files Browse the repository at this point in the history
  • Loading branch information
itsyaasir committed Nov 13, 2023
1 parent 386f13d commit e74579c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
16 changes: 11 additions & 5 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

Expand All @@ -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),
}

Expand All @@ -80,8 +80,14 @@ impl From<String> for BuilderError {
}
}

impl From<derive_builder::UninitializedFieldError> for BuilderError {
impl From<derive_builder::UninitializedFieldError> for MpesaError {
fn from(e: derive_builder::UninitializedFieldError) -> Self {
Self::UninitializedField(e.field_name())
Self::BuilderError(BuilderError::UninitializedField(e.field_name()))
}
}

impl From<url::ParseError> for MpesaError {
fn from(e: url::ParseError) -> Self {
Self::BuilderError(BuilderError::ValidationError(e.to_string()))
}
}
4 changes: 2 additions & 2 deletions src/services/express_request.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Env>,
Expand Down
4 changes: 2 additions & 2 deletions src/services/transaction_reversal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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<Env>,
Expand Down
21 changes: 8 additions & 13 deletions tests/mpesa-rust/transaction_reversal_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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")
Expand All @@ -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]
Expand Down

0 comments on commit e74579c

Please sign in to comment.