premilinary error handling and code re-arrangement

This commit is contained in:
Ritesh Chitlangi 2021-07-22 23:08:01 +08:00
parent fca4e4b231
commit 3ea096d62f
9 changed files with 51 additions and 40 deletions

View File

@ -1,11 +1,11 @@
use epp_client::{epp::request::generate_client_tr_id, connection::client::EppClient, connection, epp::xml::EppXml}; use epp_client::{epp::request::generate_client_tr_id, connection::client::EppClient, connection, epp::xml::EppXml};
use epp_client::epp::response::EppGreeting;
use epp_client::epp::request::domain::check::EppDomainCheck; use epp_client::epp::request::domain::check::EppDomainCheck;
use epp_client::epp::response::domain::check::EppDomainCheckResponse; use epp_client::epp::response::domain::check::EppDomainCheckResponse;
use epp_client::epp::request::contact::check::EppContactCheck; use epp_client::epp::request::contact::check::EppContactCheck;
use epp_client::epp::response::contact::check::EppContactCheckResponse; use epp_client::epp::response::contact::check::EppContactCheckResponse;
use epp_client::epp::object::data::{PostalInfo, Address, Phone}; use epp_client::epp::object::data::{PostalInfo, Address, Phone};
use epp_client::epp::request::contact::create::EppContactCreate; use epp_client::epp::request::contact::create::EppContactCreate;
use epp_client::epp::response::contact::create::EppContactCreateResponse;
async fn check_domains(client: &mut EppClient) { async fn check_domains(client: &mut EppClient) {
let domains = vec!["eppdev.com", "hexonet.net"]; let domains = vec!["eppdev.com", "hexonet.net"];
@ -21,7 +21,7 @@ async fn check_contacts(client: &mut EppClient) {
client.transact::<_, EppContactCheckResponse>(&contact_check).await.unwrap(); client.transact::<_, EppContactCheckResponse>(&contact_check).await.unwrap();
} }
async fn create_contact() { async fn create_contact(client: &mut EppClient) {
let street = vec!["58", "Orchid Road"]; let street = vec!["58", "Orchid Road"];
let address = Address::new(street, "Paris", "Paris", "392374", "FR"); let address = Address::new(street, "Paris", "Paris", "392374", "FR");
let postal_info = PostalInfo::new("int", "John Doe", "Acme Widgets", address); let postal_info = PostalInfo::new("int", "John Doe", "Acme Widgets", address);
@ -33,9 +33,9 @@ async fn create_contact() {
let mut contact_create = EppContactCreate::new("eppdev-contact-1", "contact@eppdev.net", postal_info, voice, "eppdev-387323", generate_client_tr_id("eppdev").unwrap().as_str()); let mut contact_create = EppContactCreate::new("eppdev-contact-1", "contact@eppdev.net", postal_info, voice, "eppdev-387323", generate_client_tr_id("eppdev").unwrap().as_str());
contact_create.set_fax(fax); contact_create.set_fax(fax);
println!("xml: {}", contact_create.serialize().unwrap()); // println!("xml: {}", contact_create.serialize().unwrap());
//client.transact::<EppContactCheck, EppContactCheckResponse>(&contact_check).await.unwrap(); client.transact::<_, EppContactCreateResponse>(&contact_create).await.unwrap();
} }
async fn hello(client: &mut EppClient) { async fn hello(client: &mut EppClient) {
@ -48,9 +48,7 @@ async fn hello(client: &mut EppClient) {
async fn main() { async fn main() {
let mut client = match EppClient::new("hexonet").await { let mut client = match EppClient::new("hexonet").await {
Ok(client) => { Ok(client) => {
let greeting = client.xml_greeting(); println!("{:?}", client.greeting());
let greeting_object = EppGreeting::deserialize(&greeting).unwrap();
println!("{:?}", greeting_object);
client client
}, },
Err(e) => panic!("Error: {}", e) Err(e) => panic!("Error: {}", e)
@ -58,9 +56,9 @@ async fn main() {
// hello(&mut client).await; // hello(&mut client).await;
check_domains(&mut client).await; // check_domains(&mut client).await;
// check_contacts(&mut client).await; // check_contacts(&mut client).await;
// create_contact().await; create_contact(&mut client).await;
} }

View File

@ -66,6 +66,8 @@ impl EppClient {
let response = self.connection.transact(&hello_xml).await?; let response = self.connection.transact(&hello_xml).await?;
println!("hello response: {}", response);
Ok(EppGreeting::deserialize(&response)?) Ok(EppGreeting::deserialize(&response)?)
} }
@ -86,8 +88,7 @@ impl EppClient {
Ok(response) Ok(response)
} else { } else {
let epp_error = EppCommandResponseError::deserialize(&response)?; let epp_error = EppCommandResponseError::deserialize(&response)?;
let epp_error = error::Error::EppCommandError(error::EppCommandError::new(epp_error)); Err(error::Error::EppCommandError(epp_error))
Err(epp_error)
} }
} }

View File

@ -1,7 +1,7 @@
pub mod data; pub mod data;
use std::fmt::Display;
use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer}; use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer};
use std::fmt::Display;
use crate::epp::xml::{EPP_XMLNS, EPP_XMLNS_XSI, EPP_XSI_SCHEMA_LOCATION}; use crate::epp::xml::{EPP_XMLNS, EPP_XMLNS_XSI, EPP_XSI_SCHEMA_LOCATION};

View File

@ -5,6 +5,7 @@ use std::{error::Error, fmt::Debug};
use crate::epp::object::{ElementName, EppObject}; use crate::epp::object::{ElementName, EppObject};
use crate::epp::xml::{EppXml, EPP_XML_HEADER}; use crate::epp::xml::{EppXml, EPP_XML_HEADER};
use crate::error;
impl<T: Serialize + DeserializeOwned + ElementName + Debug> EppXml for EppObject<T> { impl<T: Serialize + DeserializeOwned + ElementName + Debug> EppXml for EppObject<T> {
type Output = EppObject<T>; type Output = EppObject<T>;
@ -15,10 +16,14 @@ impl<T: Serialize + DeserializeOwned + ElementName + Debug> EppXml for EppObject
Ok(epp_xml) Ok(epp_xml)
} }
fn deserialize(epp_xml: &str) -> Result<Self::Output, Box<dyn Error>> { fn deserialize(epp_xml: &str) -> Result<Self::Output, error::Error> {
let mut object: Self::Output = match from_str(epp_xml) { let mut object: Self::Output = match from_str(epp_xml) {
Ok(v) => v, Ok(v) => v,
Err(e) => return Err(format!("epp-client Deserialization Error: {}", e).into()), Err(e) => {
return Err(error::Error::EppDeserializationError(
format!("epp-client Deserialization Error: {}", e).to_string(),
))
}
}; };
// object.xml = Some(epp_xml.to_string()); // object.xml = Some(epp_xml.to_string());
Ok(object) Ok(object)

View File

@ -15,7 +15,7 @@ pub struct DomainList {
} }
#[derive(Serialize, Deserialize, Debug, ElementName)] #[derive(Serialize, Deserialize, Debug, ElementName)]
#[element_name(name = "checkr")] #[element_name(name = "check")]
pub struct DomainCheck { pub struct DomainCheck {
#[serde(rename = "check")] #[serde(rename = "check")]
list: DomainList, list: DomainList,

View File

@ -1 +1,2 @@
pub mod check; pub mod check;
pub mod create;

View File

@ -0,0 +1,19 @@
use serde::{Deserialize, Serialize};
use crate::epp::object::{EppObject, StringValue};
use crate::epp::response::CommandResponse;
pub type EppContactCreateResponse = EppObject<CommandResponse<ContactCreateResult>>;
#[derive(Serialize, Deserialize, Debug)]
pub struct ContactCreateData {
pub id: StringValue,
#[serde(rename = "crDate")]
pub created_at: StringValue,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ContactCreateResult {
#[serde(rename = "creData")]
pub check_data: ContactCreateData,
}

View File

@ -1,6 +1,6 @@
use std::{error::Error, fmt::Debug}; use std::{error::Error, fmt::Debug};
// use crate::epp::object::EppObject; use crate::error;
pub const EPP_XML_HEADER: &str = r#"<?xml version="1.0" encoding="UTF-8" standalone="no"?>"#; pub const EPP_XML_HEADER: &str = r#"<?xml version="1.0" encoding="UTF-8" standalone="no"?>"#;
pub const EPP_XMLNS: &str = "urn:ietf:params:xml:ns:epp-1.0"; pub const EPP_XMLNS: &str = "urn:ietf:params:xml:ns:epp-1.0";
@ -17,5 +17,5 @@ pub trait EppXml {
type Output: Debug; type Output: Debug;
fn serialize(&self) -> Result<String, Box<dyn Error>>; fn serialize(&self) -> Result<String, Box<dyn Error>>;
fn deserialize(epp_xml: &str) -> Result<Self::Output, Box<dyn Error>>; fn deserialize(epp_xml: &str) -> Result<Self::Output, error::Error>;
} }

View File

@ -4,7 +4,8 @@ use std::fmt::Display;
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
EppConnectionError(std::io::Error), EppConnectionError(std::io::Error),
EppCommandError(EppCommandError), EppCommandError(EppCommandResponseError),
EppDeserializationError(String),
Other(String), Other(String),
} }
@ -15,35 +16,21 @@ pub struct EppCommandError {
impl std::error::Error for Error {} impl std::error::Error for Error {}
impl std::error::Error for EppCommandError {}
impl Display for EppCommandError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"epp-client EppCommandError: {}",
self.epp_error.data.result.message
)
}
}
impl EppCommandError {
pub fn new(epp_error: EppCommandResponseError) -> EppCommandError {
EppCommandError {
epp_error: epp_error,
}
}
}
impl Display for Error { impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "epp-client Exception: {:?}", self) match self {
Error::EppCommandError(e) => {
write!(f, "epp-client EppCommandError: {}", e.data.result.message)
}
Error::Other(e) => write!(f, "epp-client Exception: {}", e),
_ => write!(f, "epp-client Exception: {:?}", self),
}
} }
} }
impl From<std::boxed::Box<dyn std::error::Error>> for Error { impl From<std::boxed::Box<dyn std::error::Error>> for Error {
fn from(e: std::boxed::Box<dyn std::error::Error>) -> Self { fn from(e: std::boxed::Box<dyn std::error::Error>) -> Self {
Self::Other(format!("{}", e).to_string()) Self::Other(format!("{:?}", e).to_string())
} }
} }