From ed6692b1c697219ad9083e9f5c57a4915b014154 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Thu, 6 Jul 2023 13:36:23 +0100 Subject: [PATCH] refactor(crates-io): rename `ResponseError` to `Error` --- crates/crates-io/lib.rs | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/crates/crates-io/lib.rs b/crates/crates-io/lib.rs index af1f491a0..43965c218 100644 --- a/crates/crates-io/lib.rs +++ b/crates/crates-io/lib.rs @@ -126,7 +126,7 @@ struct Crates { } #[derive(Debug)] -pub enum ResponseError { +pub enum Error { Curl(curl::Error), Api { code: u32, @@ -141,29 +141,29 @@ pub enum ResponseError { Other(anyhow::Error), } -impl std::error::Error for ResponseError { +impl std::error::Error for Error { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { match self { - ResponseError::Curl(..) => None, - ResponseError::Api { .. } => None, - ResponseError::Code { .. } => None, - ResponseError::Other(e) => Some(e.as_ref()), + Self::Curl(..) => None, + Self::Api { .. } => None, + Self::Code { .. } => None, + Self::Other(e) => Some(e.as_ref()), } } } -impl fmt::Display for ResponseError { +impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - ResponseError::Curl(e) => write!(f, "{}", e), - ResponseError::Api { code, errors, .. } => { + Self::Curl(e) => write!(f, "{}", e), + Self::Api { code, errors, .. } => { f.write_str("the remote server responded with an error")?; if *code != 200 { write!(f, " (status {} {})", code, reason(*code))?; }; write!(f, ": {}", errors.join(", ")) } - ResponseError::Code { + Self::Code { code, headers, body, @@ -178,14 +178,14 @@ impl fmt::Display for ResponseError { headers.join("\n\t"), body ), - ResponseError::Other(..) => write!(f, "invalid response from server"), + Self::Other(..) => write!(f, "invalid response from server"), } } } -impl From for ResponseError { +impl From for Error { fn from(error: curl::Error) -> Self { - ResponseError::Curl(error) + Self::Curl(error) } } @@ -301,7 +301,7 @@ impl Registry { let body = self .handle(&mut |buf| body.read(buf).unwrap_or(0)) .map_err(|e| match e { - ResponseError::Code { code, .. } + Error::Code { code, .. } if code == 503 && started.elapsed().as_secs() >= 29 && self.host_is_crates_io() => @@ -414,7 +414,7 @@ impl Registry { fn handle( &mut self, read: &mut dyn FnMut(&mut [u8]) -> usize, - ) -> std::result::Result { + ) -> std::result::Result { let mut headers = Vec::new(); let mut body = Vec::new(); { @@ -441,7 +441,7 @@ impl Registry { let body = match String::from_utf8(body) { Ok(body) => body, Err(..) => { - return Err(ResponseError::Other(format_err!( + return Err(Error::Other(format_err!( "response body was not valid utf-8" ))) } @@ -452,12 +452,12 @@ impl Registry { match (self.handle.response_code()?, errors) { (0, None) | (200, None) => Ok(body), - (code, Some(errors)) => Err(ResponseError::Api { + (code, Some(errors)) => Err(Error::Api { code, headers, errors, }), - (code, None) => Err(ResponseError::Code { + (code, None) => Err(Error::Code { code, headers, body,