From 6d063804d3bf9047e25609519706518100fb950c Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Wed, 22 Dec 2021 10:55:48 +0100 Subject: [PATCH] Use consistent naming for error types --- src/client.rs | 16 ++++++++-------- src/connection.rs | 22 +++++++++++----------- src/error.rs | 21 +++++++++++---------- src/lib.rs | 6 +++--- src/request.rs | 5 +++-- 5 files changed, 36 insertions(+), 34 deletions(-) diff --git a/src/client.rs b/src/client.rs index 93ef4fa..d95bcc6 100644 --- a/src/client.rs +++ b/src/client.rs @@ -33,12 +33,12 @@ //! } //! ``` -use std::error::Error; +use std::error::Error as StdError; use std::net::SocketAddr; use crate::common::{Certificate, NoExtension, PrivateKey}; use crate::connection::EppConnection; -use crate::error; +use crate::error::Error; use crate::hello::{Greeting, GreetingDocument, HelloDocument}; use crate::request::{Command, Extension, Transaction}; use crate::response::Response; @@ -59,14 +59,14 @@ impl EppClient { addr: SocketAddr, hostname: &str, identity: Option<(Vec, PrivateKey)>, - ) -> Result> { + ) -> Result> { Ok(Self { connection: EppConnection::connect(registry, addr, hostname, identity).await?, }) } /// Executes an EPP Hello call and returns the response as an `Greeting` - pub async fn hello(&mut self) -> Result> { + pub async fn hello(&mut self) -> Result> { let hello_xml = HelloDocument::default().serialize()?; let response = self.connection.transact(&hello_xml).await?; @@ -78,7 +78,7 @@ impl EppClient { &mut self, data: impl Into> + 'a, id: &str, - ) -> Result, error::Error> + ) -> Result, Error> where C: Transaction + Command, E: Extension, @@ -93,7 +93,7 @@ impl EppClient { /// 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 - pub async fn transact_xml(&mut self, xml: &str) -> Result> { + pub async fn transact_xml(&mut self, xml: &str) -> Result> { self.connection.transact(xml).await } @@ -103,11 +103,11 @@ impl EppClient { } /// Returns the greeting received on establishment of the connection as an `Greeting` - pub fn greeting(&self) -> Result { + pub fn greeting(&self) -> Result { GreetingDocument::deserialize(&self.connection.greeting).map(|obj| obj.data) } - pub async fn shutdown(mut self) -> Result<(), Box> { + pub async fn shutdown(mut self) -> Result<(), Box> { self.connection.shutdown().await } } diff --git a/src/connection.rs b/src/connection.rs index 0d995d5..f72f591 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -1,9 +1,9 @@ //! Manages registry connections and reading/writing to them use std::convert::TryInto; +use std::error::Error as StdError; use std::net::SocketAddr; use std::sync::Arc; -use std::{error::Error, io as stdio}; use std::{io, str, u32}; use rustls::{OwnedTrustAnchor, RootCertStore}; @@ -12,7 +12,7 @@ use tokio_rustls::{client::TlsStream, rustls::ClientConfig, TlsConnector}; use tracing::{debug, info}; use crate::common::{Certificate, PrivateKey}; -use crate::error; +use crate::error::Error; /// EPP Connection struct with some metadata for the connection pub(crate) struct EppConnection { @@ -28,7 +28,7 @@ impl EppConnection { addr: SocketAddr, hostname: &str, identity: Option<(Vec, PrivateKey)>, - ) -> Result> { + ) -> Result> { let mut stream = epp_connect(addr, hostname, identity).await?; let mut buf = vec![0u8; 4096]; @@ -45,7 +45,7 @@ impl EppConnection { } /// 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> { + async fn send_epp_request(&mut self, content: &str) -> Result<(), Box> { let len = content.len(); let buf_size = len + 4; @@ -63,7 +63,7 @@ impl EppConnection { } /// Receives response from the socket and converts it into an EPP XML string - async fn get_epp_response(&mut self) -> Result> { + async fn get_epp_response(&mut self) -> Result> { let mut buf = [0u8; 4]; self.stream.read_exact(&mut buf).await?; @@ -98,7 +98,7 @@ impl EppConnection { /// Sends an EPP XML request to the registry and return the response /// receieved to the request - pub(crate) async fn transact(&mut self, content: &str) -> Result> { + pub(crate) async fn transact(&mut self, content: &str) -> Result> { debug!("{}: request: {}", self.registry, content); self.send_epp_request(content).await?; @@ -109,7 +109,7 @@ impl EppConnection { } /// Closes the socket and shuts the connection - pub(crate) async fn shutdown(&mut self) -> Result<(), Box> { + pub(crate) async fn shutdown(&mut self) -> Result<(), Box> { info!("{}: Closing connection", self.registry); self.stream.shutdown().await?; @@ -123,7 +123,7 @@ async fn epp_connect( addr: SocketAddr, hostname: &str, identity: Option<(Vec, PrivateKey)>, -) -> Result, error::Error> { +) -> Result, Error> { info!("Connecting to server: {:?}", addr,); let mut roots = RootCertStore::empty(); @@ -147,7 +147,7 @@ async fn epp_connect( .collect(); builder .with_single_cert(certs, rustls::PrivateKey(key.0)) - .map_err(|e| error::Error::Other(e.to_string()))? + .map_err(|e| Error::Other(e.to_string()))? } None => builder.with_no_client_auth(), }; @@ -156,8 +156,8 @@ async fn epp_connect( let stream = TcpStream::connect(&addr).await?; let domain = hostname.try_into().map_err(|_| { - stdio::Error::new( - stdio::ErrorKind::InvalidInput, + io::Error::new( + io::ErrorKind::InvalidInput, format!("Invalid domain: {}", hostname), ) })?; diff --git a/src/error.rs b/src/error.rs index 2014476..2f9a983 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,7 +1,8 @@ //! Error types to wrap internal errors and make EPP errors easier to read use std::error::Error as StdError; -use std::fmt::Display; +use std::fmt::{self, Display}; +use std::io; use crate::response::ResponseStatus; @@ -14,10 +15,10 @@ pub enum Error { Other(String), } -impl std::error::Error for Error {} +impl StdError for Error {} impl Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Error::Command(e) => { write!(f, "epp-client EppCommandError: {}", e.result.message) @@ -28,20 +29,20 @@ impl Display for Error { } } -impl From> for Error { - fn from(e: std::boxed::Box) -> Self { +impl From> for Error { + fn from(e: Box) -> Self { Self::Other(format!("{:?}", e)) } } -impl From for Error { - fn from(e: std::io::Error) -> Self { +impl From for Error { + fn from(e: io::Error) -> Self { Self::Io(e) } } -impl From for Error { - fn from(e: std::io::ErrorKind) -> Self { - Self::Io(std::io::Error::from(e)) +impl From for Error { + fn from(e: io::ErrorKind) -> Self { + Self::Io(io::Error::from(e)) } } diff --git a/src/lib.rs b/src/lib.rs index de8f007..169b670 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -90,8 +90,9 @@ pub mod client; pub mod common; pub mod connection; +pub mod contact; pub mod domain; -pub mod error; +mod error; pub mod hello; pub mod login; pub mod logout; @@ -99,8 +100,6 @@ pub mod request; pub mod response; pub mod xml; -pub mod contact; - pub mod extensions { pub mod consolidate; pub mod namestore; @@ -123,6 +122,7 @@ pub mod message { } pub use client::EppClient; +pub use error::Error; #[cfg(test)] pub mod tests; diff --git a/src/request.rs b/src/request.rs index 00128ae..77b5fa3 100644 --- a/src/request.rs +++ b/src/request.rs @@ -7,6 +7,7 @@ use crate::{ common::{StringValue, EPP_XMLNS}, response::{Response, ResponseDocument, ResponseStatus}, xml::EppXml, + Error, }; pub const EPP_VERSION: &str = "1.0"; @@ -18,7 +19,7 @@ pub trait Transaction: Command + Sized { &self, extension: Option<&Ext>, client_tr_id: &str, - ) -> Result { + ) -> Result { as EppXml>::serialize(&CommandDocument::new(CommandWrapper { command: Self::COMMAND, data: self, @@ -29,7 +30,7 @@ pub trait Transaction: Command + Sized { fn deserialize_response( epp_xml: &str, - ) -> Result, crate::error::Error> { + ) -> Result, Error> { let rsp = as EppXml>::deserialize(epp_xml)?; match rsp.data.result.code {