Preserve quick-xml errors

This commit is contained in:
Dirkjan Ochtman 2021-12-07 11:32:44 +01:00 committed by masalachai
parent 78628cfc02
commit 467ac03df7
3 changed files with 14 additions and 23 deletions

View File

@ -1,14 +1,16 @@
//! Error types to wrap internal errors and make EPP errors easier to read //! Error types to wrap internal errors and make EPP errors easier to read
use crate::response::ResponseStatus; use std::error::Error as StdError;
use std::fmt::Display; use std::fmt::Display;
use crate::response::ResponseStatus;
/// Error enum holding the possible error types /// Error enum holding the possible error types
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
Io(std::io::Error), Io(std::io::Error),
Command(ResponseStatus), Command(ResponseStatus),
Deserialize(String), Xml(Box<dyn StdError>),
Other(String), Other(String),
} }

View File

@ -18,7 +18,7 @@ pub trait Transaction<Ext: Extension>: Command + Sized {
&self, &self,
extension: Option<&Ext>, extension: Option<&Ext>,
client_tr_id: &str, client_tr_id: &str,
) -> Result<String, Box<dyn std::error::Error>> { ) -> Result<String, crate::error::Error> {
<CommandDocument<Self, Ext> as EppXml>::serialize(&CommandDocument::new(CommandWrapper { <CommandDocument<Self, Ext> as EppXml>::serialize(&CommandDocument::new(CommandWrapper {
command: Self::COMMAND, command: Self::COMMAND,
data: self, data: self,

View File

@ -1,41 +1,30 @@
//! Types to use in serialization to and deserialization from EPP XML //! Types to use in serialization to and deserialization from EPP XML
use quick_xml::de::from_str;
use quick_xml::se;
use serde::{de::DeserializeOwned, Serialize}; use serde::{de::DeserializeOwned, Serialize};
use std::error::Error;
use crate::error; use crate::error::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"?>"#;
/// Trait to be implemented by serializers. Currently the only included serializer is `quick-xml` /// Trait to be implemented by serializers. Currently the only included serializer is `quick-xml`
pub trait EppXml: Sized { pub trait EppXml: Sized {
/// Serializes the EppObject instance to an EPP XML document /// Serializes the EppObject instance to an EPP XML document
fn serialize(&self) -> Result<String, Box<dyn Error>> fn serialize(&self) -> Result<String, Error>
where where
Self: Serialize, Self: Serialize,
{ {
let epp_xml = format!("{}\r\n{}", EPP_XML_HEADER, se::to_string(self)?); Ok(format!(
"{}\r\n{}",
Ok(epp_xml) EPP_XML_HEADER,
quick_xml::se::to_string(self).map_err(|e| Error::Xml(e.into()))?
))
} }
/// Deserializes an EPP XML document to an EppObject instance /// Deserializes an EPP XML document to an EppObject instance
fn deserialize(epp_xml: &str) -> Result<Self, error::Error> fn deserialize(epp_xml: &str) -> Result<Self, Error>
where where
Self: DeserializeOwned + Sized, Self: DeserializeOwned + Sized,
{ {
let object: Self = match from_str(epp_xml) { quick_xml::de::from_str::<Self>(epp_xml).map_err(|e| Error::Xml(e.into()))
Ok(v) => v,
Err(e) => {
return Err(error::Error::Deserialize(format!(
"epp-client Deserialization Error: {}",
e
)))
}
};
// object.xml = Some(epp_xml.to_string());
Ok(object)
} }
} }