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
use crate::response::ResponseStatus;
use std::error::Error as StdError;
use std::fmt::Display;
use crate::response::ResponseStatus;
/// Error enum holding the possible error types
#[derive(Debug)]
pub enum Error {
Io(std::io::Error),
Command(ResponseStatus),
Deserialize(String),
Xml(Box<dyn StdError>),
Other(String),
}

View File

@ -18,7 +18,7 @@ pub trait Transaction<Ext: Extension>: Command + Sized {
&self,
extension: Option<&Ext>,
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 {
command: Self::COMMAND,
data: self,

View File

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