Consistently return Error type

This commit is contained in:
Dirkjan Ochtman 2021-12-22 11:07:19 +01:00 committed by masalachai
parent abab1aca8c
commit 978e43a4c1
3 changed files with 30 additions and 11 deletions

View File

@ -33,7 +33,6 @@
//! } //! }
//! ``` //! ```
use std::error::Error as StdError;
use std::net::SocketAddr; use std::net::SocketAddr;
use crate::common::{Certificate, NoExtension, PrivateKey}; use crate::common::{Certificate, NoExtension, PrivateKey};
@ -59,14 +58,14 @@ impl EppClient {
addr: SocketAddr, addr: SocketAddr,
hostname: &str, hostname: &str,
identity: Option<(Vec<Certificate>, PrivateKey)>, identity: Option<(Vec<Certificate>, PrivateKey)>,
) -> Result<Self, Box<dyn StdError>> { ) -> Result<Self, Error> {
Ok(Self { Ok(Self {
connection: EppConnection::connect(registry, addr, hostname, identity).await?, connection: EppConnection::connect(registry, addr, hostname, identity).await?,
}) })
} }
/// Executes an EPP Hello call and returns the response as an `Greeting` /// Executes an EPP Hello call and returns the response as an `Greeting`
pub async fn hello(&mut self) -> Result<Greeting, Box<dyn StdError>> { pub async fn hello(&mut self) -> Result<Greeting, Error> {
let hello_xml = HelloDocument::default().serialize()?; let hello_xml = HelloDocument::default().serialize()?;
let response = self.connection.transact(&hello_xml).await?; let response = self.connection.transact(&hello_xml).await?;
@ -93,7 +92,7 @@ impl EppClient {
/// Accepts raw EPP XML and returns the raw EPP XML response to it. /// Accepts raw EPP XML and returns the raw EPP XML response to it.
/// Not recommended for direct use but sometimes can be useful for debugging /// Not recommended for direct use but sometimes can be useful for debugging
pub async fn transact_xml(&mut self, xml: &str) -> Result<String, Box<dyn StdError>> { pub async fn transact_xml(&mut self, xml: &str) -> Result<String, Error> {
self.connection.transact(xml).await self.connection.transact(xml).await
} }
@ -107,7 +106,7 @@ impl EppClient {
GreetingDocument::deserialize(&self.connection.greeting).map(|obj| obj.data) GreetingDocument::deserialize(&self.connection.greeting).map(|obj| obj.data)
} }
pub async fn shutdown(mut self) -> Result<(), Box<dyn StdError>> { pub async fn shutdown(mut self) -> Result<(), Error> {
self.connection.shutdown().await self.connection.shutdown().await
} }
} }

View File

@ -1,7 +1,6 @@
//! Manages registry connections and reading/writing to them //! Manages registry connections and reading/writing to them
use std::convert::TryInto; use std::convert::TryInto;
use std::error::Error as StdError;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::sync::Arc; use std::sync::Arc;
use std::{io, str, u32}; use std::{io, str, u32};
@ -28,7 +27,7 @@ impl EppConnection {
addr: SocketAddr, addr: SocketAddr,
hostname: &str, hostname: &str,
identity: Option<(Vec<Certificate>, PrivateKey)>, identity: Option<(Vec<Certificate>, PrivateKey)>,
) -> Result<EppConnection, Box<dyn StdError>> { ) -> Result<EppConnection, Error> {
let mut stream = epp_connect(addr, hostname, identity).await?; let mut stream = epp_connect(addr, hostname, identity).await?;
let mut buf = vec![0u8; 4096]; let mut buf = vec![0u8; 4096];
@ -45,7 +44,7 @@ impl EppConnection {
} }
/// Constructs an EPP XML request in the required form and sends it to the server /// Constructs an EPP XML request in the required form and sends it to the server
async fn send_epp_request(&mut self, content: &str) -> Result<(), Box<dyn StdError>> { async fn send_epp_request(&mut self, content: &str) -> Result<(), Error> {
let len = content.len(); let len = content.len();
let buf_size = len + 4; let buf_size = len + 4;
@ -63,7 +62,7 @@ impl EppConnection {
} }
/// Receives response from the socket and converts it into an EPP XML string /// Receives response from the socket and converts it into an EPP XML string
async fn get_epp_response(&mut self) -> Result<String, Box<dyn StdError>> { async fn get_epp_response(&mut self) -> Result<String, Error> {
let mut buf = [0u8; 4]; let mut buf = [0u8; 4];
self.stream.read_exact(&mut buf).await?; self.stream.read_exact(&mut buf).await?;
@ -98,7 +97,7 @@ impl EppConnection {
/// Sends an EPP XML request to the registry and return the response /// Sends an EPP XML request to the registry and return the response
/// receieved to the request /// receieved to the request
pub(crate) async fn transact(&mut self, content: &str) -> Result<String, Box<dyn StdError>> { pub(crate) async fn transact(&mut self, content: &str) -> Result<String, Error> {
debug!("{}: request: {}", self.registry, content); debug!("{}: request: {}", self.registry, content);
self.send_epp_request(content).await?; self.send_epp_request(content).await?;
@ -109,7 +108,7 @@ impl EppConnection {
} }
/// Closes the socket and shuts the connection /// Closes the socket and shuts the connection
pub(crate) async fn shutdown(&mut self) -> Result<(), Box<dyn StdError>> { pub(crate) async fn shutdown(&mut self) -> Result<(), Error> {
info!("{}: Closing connection", self.registry); info!("{}: Closing connection", self.registry);
self.stream.shutdown().await?; self.stream.shutdown().await?;

View File

@ -3,6 +3,9 @@
use std::error::Error as StdError; use std::error::Error as StdError;
use std::fmt::{self, Display}; use std::fmt::{self, Display};
use std::io; use std::io;
use std::num::TryFromIntError;
use std::str::Utf8Error;
use std::string::FromUtf8Error;
use crate::response::ResponseStatus; use crate::response::ResponseStatus;
@ -46,3 +49,21 @@ impl From<io::ErrorKind> for Error {
Self::Io(io::Error::from(e)) Self::Io(io::Error::from(e))
} }
} }
impl From<TryFromIntError> for Error {
fn from(e: TryFromIntError) -> Self {
Self::Other(e.into())
}
}
impl From<FromUtf8Error> for Error {
fn from(e: FromUtf8Error) -> Self {
Self::Other(e.into())
}
}
impl From<Utf8Error> for Error {
fn from(e: Utf8Error) -> Self {
Self::Other(e.into())
}
}