Use consistent naming for error types

This commit is contained in:
Dirkjan Ochtman 2021-12-22 10:55:48 +01:00 committed by masalachai
parent 467ac03df7
commit 6d063804d3
5 changed files with 36 additions and 34 deletions

View File

@ -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<Certificate>, PrivateKey)>,
) -> Result<Self, Box<dyn Error>> {
) -> Result<Self, Box<dyn StdError>> {
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<Greeting, Box<dyn Error>> {
pub async fn hello(&mut self) -> Result<Greeting, Box<dyn StdError>> {
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<RequestData<'a, C, E>> + 'a,
id: &str,
) -> Result<Response<C::Response, E::Response>, error::Error>
) -> Result<Response<C::Response, E::Response>, Error>
where
C: Transaction<E> + 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<String, Box<dyn Error>> {
pub async fn transact_xml(&mut self, xml: &str) -> Result<String, Box<dyn StdError>> {
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<Greeting, error::Error> {
pub fn greeting(&self) -> Result<Greeting, Error> {
GreetingDocument::deserialize(&self.connection.greeting).map(|obj| obj.data)
}
pub async fn shutdown(mut self) -> Result<(), Box<dyn Error>> {
pub async fn shutdown(mut self) -> Result<(), Box<dyn StdError>> {
self.connection.shutdown().await
}
}

View File

@ -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<Certificate>, PrivateKey)>,
) -> Result<EppConnection, Box<dyn Error>> {
) -> Result<EppConnection, Box<dyn StdError>> {
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<dyn Error>> {
async fn send_epp_request(&mut self, content: &str) -> Result<(), Box<dyn StdError>> {
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<String, Box<dyn Error>> {
async fn get_epp_response(&mut self) -> Result<String, Box<dyn StdError>> {
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<String, Box<dyn Error>> {
pub(crate) async fn transact(&mut self, content: &str) -> Result<String, Box<dyn StdError>> {
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<dyn Error>> {
pub(crate) async fn shutdown(&mut self) -> Result<(), Box<dyn StdError>> {
info!("{}: Closing connection", self.registry);
self.stream.shutdown().await?;
@ -123,7 +123,7 @@ async fn epp_connect(
addr: SocketAddr,
hostname: &str,
identity: Option<(Vec<Certificate>, PrivateKey)>,
) -> Result<TlsStream<TcpStream>, error::Error> {
) -> Result<TlsStream<TcpStream>, 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),
)
})?;

View File

@ -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<std::boxed::Box<dyn std::error::Error>> for Error {
fn from(e: std::boxed::Box<dyn std::error::Error>) -> Self {
impl From<Box<dyn StdError>> for Error {
fn from(e: Box<dyn StdError>) -> Self {
Self::Other(format!("{:?}", e))
}
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Self::Io(e)
}
}
impl From<std::io::ErrorKind> for Error {
fn from(e: std::io::ErrorKind) -> Self {
Self::Io(std::io::Error::from(e))
impl From<io::ErrorKind> for Error {
fn from(e: io::ErrorKind) -> Self {
Self::Io(io::Error::from(e))
}
}

View File

@ -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;

View File

@ -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<Ext: Extension>: Command + Sized {
&self,
extension: Option<&Ext>,
client_tr_id: &str,
) -> Result<String, crate::error::Error> {
) -> Result<String, Error> {
<CommandDocument<Self, Ext> as EppXml>::serialize(&CommandDocument::new(CommandWrapper {
command: Self::COMMAND,
data: self,
@ -29,7 +30,7 @@ pub trait Transaction<Ext: Extension>: Command + Sized {
fn deserialize_response(
epp_xml: &str,
) -> Result<Response<Self::Response, Ext::Response>, crate::error::Error> {
) -> Result<Response<Self::Response, Ext::Response>, Error> {
let rsp =
<ResponseDocument<Self::Response, Ext::Response> as EppXml>::deserialize(epp_xml)?;
match rsp.data.result.code {