From 56285fac58fa98a5bfc3b4aa3fd64e9c7bc33cc5 Mon Sep 17 00:00:00 2001 From: Bastian Buman Date: Wed, 8 Jun 2022 11:54:17 +0200 Subject: [PATCH 1/2] Improve the ReqwestError The error now returns the reason it was raised. --- meteomatics/src/client.rs | 24 ++++++++++++------------ meteomatics/src/errors.rs | 10 ++-------- meteomatics/src/util.rs | 9 ++++++--- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/meteomatics/src/client.rs b/meteomatics/src/client.rs index ae566b3..9fb8907 100644 --- a/meteomatics/src/client.rs +++ b/meteomatics/src/client.rs @@ -106,7 +106,7 @@ impl APIClient { status, )), }, - Err(_) => Err(ConnectorError::ReqwestError), + Err(e) => Err(ConnectorError::ReqwestError(e.to_string())), } } @@ -182,7 +182,7 @@ impl APIClient { status, )), }, - Err(_) => Err(ConnectorError::ReqwestError), + Err(e) => Err(ConnectorError::ReqwestError(e.to_string())), } } @@ -264,7 +264,7 @@ impl APIClient { status, )), }, - Err(_) => Err(ConnectorError::ReqwestError), + Err(e) => Err(ConnectorError::ReqwestError(e.to_string())), } } @@ -297,7 +297,7 @@ impl APIClient { status, )), }, - Err(_) => Err(ConnectorError::ReqwestError), + Err(e) => Err(ConnectorError::ReqwestError(e.to_string())), } } @@ -390,7 +390,7 @@ impl APIClient { status, )), }, - Err(_) => Err(ConnectorError::ReqwestError), + Err(e) => Err(ConnectorError::ReqwestError(e.to_string())), } } @@ -482,7 +482,7 @@ impl APIClient { status, )), }, - Err(_) => Err(ConnectorError::ReqwestError), + Err(e) => Err(ConnectorError::ReqwestError(e.to_string())), } } @@ -564,7 +564,7 @@ impl APIClient { status, )), }, - Err(_) => Err(ConnectorError::ReqwestError), + Err(e) => Err(ConnectorError::ReqwestError(e.to_string())), } } @@ -649,7 +649,7 @@ impl APIClient { status, )), }, - Err(_) => Err(ConnectorError::ReqwestError), + Err(e) => Err(ConnectorError::ReqwestError(e.to_string())), } } @@ -736,7 +736,7 @@ impl APIClient { status, )), }, - Err(_) => Err(ConnectorError::ReqwestError), + Err(e) => Err(ConnectorError::ReqwestError(e.to_string())), } } @@ -829,7 +829,7 @@ impl APIClient { status, )), }, - Err(_) => Err(ConnectorError::ReqwestError), + Err(e) => Err(ConnectorError::ReqwestError(e.to_string())), } } @@ -917,7 +917,7 @@ impl APIClient { status, )), }, - Err(_) => Err(ConnectorError::ReqwestError), + Err(e) => Err(ConnectorError::ReqwestError(e.to_string())), } } @@ -1000,7 +1000,7 @@ impl APIClient { .basic_auth(&self.username, Some(String::from(&self.password))) .send() .await - .map_err(|_| ConnectorError::ReqwestError) + .map_err(|e| ConnectorError::ReqwestError(e.to_string())) } } diff --git a/meteomatics/src/errors.rs b/meteomatics/src/errors.rs index 8df94f7..551dac5 100644 --- a/meteomatics/src/errors.rs +++ b/meteomatics/src/errors.rs @@ -3,8 +3,8 @@ use thiserror::Error; #[derive(Error, Debug)] pub enum ConnectorError { /// ReqwestError. - #[error("ReqwestError error")] - ReqwestError, + #[error("ReqwestError error: {0}")] + ReqwestError(String), /// HTTP response error. #[error("HTTP error: `{0}`, `{1}`, {2}`")] @@ -44,12 +44,6 @@ impl From for ConnectorError { } } -impl From for ConnectorError { - fn from(_: reqwest::Error) -> Self { - ConnectorError::ReqwestError - } -} - impl From for ConnectorError { fn from(_: std::io::Error) -> Self { ConnectorError::FileIOError diff --git a/meteomatics/src/util.rs b/meteomatics/src/util.rs index 2d0d338..08f5805 100644 --- a/meteomatics/src/util.rs +++ b/meteomatics/src/util.rs @@ -111,8 +111,11 @@ pub struct Limit{ // Deserializes the response for the user_stats_json query. pub async fn extract_user_statistics(response: Response) -> std::result::Result { - let json: UStatsResponse = response.json::().await?; - Ok(json) + match response.json::() + .await { + Ok(json) => Ok(json), + Err(e) => Err(ConnectorError::ReqwestError(e.to_string())), + } } /// Writes the HTTP response to a file. @@ -123,7 +126,7 @@ pub async fn extract_user_statistics(response: Response) -> std::result::Result< /// * `file_name` - The name for the file to be written (complete with path). /// pub async fn write_file(response: Response, file_name: &String) -> std::result::Result<(), ConnectorError> { - let body = response.bytes().await?; + let body = response.bytes().await.map_err(|e| ConnectorError::ReqwestError(e.to_string())).unwrap(); let mut content = std::io::Cursor::new(body); let mut file = File::create(file_name)?; From 4f79fa4b835681a15f7314e38ba579b1670b1575 Mon Sep 17 00:00:00 2001 From: Bastian Buman Date: Tue, 28 Jun 2022 14:33:50 +0200 Subject: [PATCH 2/2] Better PolarsError --- meteomatics/src/client.rs | 55 +++++++++++++++++++++------------------ meteomatics/src/errors.rs | 10 ++----- 2 files changed, 32 insertions(+), 33 deletions(-) diff --git a/meteomatics/src/client.rs b/meteomatics/src/client.rs index 9fb8907..156bfa7 100644 --- a/meteomatics/src/client.rs +++ b/meteomatics/src/client.rs @@ -96,8 +96,8 @@ impl APIClient { match result { Ok(response) => match response.status() { StatusCode::OK => { - let df = parse_response_to_df( - response).await?; + let df = parse_response_to_df(response).await + .map_err(|e| ConnectorError::PolarsError(e.to_string()))?; Ok(df) } status => Err(ConnectorError::HttpError( @@ -172,8 +172,8 @@ impl APIClient { match result { Ok(response) => match response.status() { StatusCode::OK => { - let df = parse_response_to_df( - response).await?; + let df = parse_response_to_df(response).await + .map_err(|e| ConnectorError::PolarsError(e.to_string()))?; Ok(df) } status => Err(ConnectorError::HttpError( @@ -251,11 +251,14 @@ impl APIClient { match result { Ok(response) => match response.status() { StatusCode::OK => { - let mut df = parse_response_to_df( - response).await?; - df.rename("stroke_time:sql", "validdate")?; - df.rename("stroke_lat:d", "lat")?; - df.rename("stroke_lon:d", "lon")?; + let mut df = parse_response_to_df(response).await + .map_err(|e| ConnectorError::PolarsError(e.to_string()))?; + df.rename("stroke_time:sql", "validdate") + .map_err(|e| ConnectorError::PolarsError(e.to_string()))?; + df.rename("stroke_lat:d", "lat") + .map_err(|e| ConnectorError::PolarsError(e.to_string()))?; + df.rename("stroke_lon:d", "lon") + .map_err(|e| ConnectorError::PolarsError(e.to_string()))?; Ok(df) } status => Err(ConnectorError::HttpError( @@ -374,13 +377,14 @@ impl APIClient { Ok(response) => match response.status() { StatusCode::OK => { if needs_latlon { - let df = parse_response_to_df( - response).await?; - let df = df_add_latlon(df, coordinates.get(0).unwrap()).await?; + let df = parse_response_to_df(response).await + .map_err(|e| ConnectorError::PolarsError(e.to_string()))?; + let df = df_add_latlon(df, coordinates.get(0).unwrap()).await + .map_err(|e| ConnectorError::PolarsError(e.to_string()))?; Ok(df) } else { - let df = parse_response_to_df( - response).await?; + let df = parse_response_to_df(response).await + .map_err(|e| ConnectorError::PolarsError(e.to_string()))?; Ok(df) } } @@ -466,13 +470,14 @@ impl APIClient { Ok(response) => match response.status() { StatusCode::OK => { if needs_latlon { - let df = parse_response_to_df( - response).await?; - let df = df_add_postal(df, postals.get(0).unwrap()).await?; + let df = parse_response_to_df(response).await + .map_err(|e| ConnectorError::PolarsError(e.to_string()))?; + let df = df_add_postal(df, postals.get(0).unwrap()).await + .map_err(|e| ConnectorError::PolarsError(e.to_string()))?; Ok(df) } else { - let df = parse_response_to_df( - response).await?; + let df = parse_response_to_df(response).await + .map_err(|e| ConnectorError::PolarsError(e.to_string()))?; Ok(df) } } @@ -554,8 +559,8 @@ impl APIClient { match result { Ok(response) => match response.status() { StatusCode::OK => { - let df = parse_grid_response_to_df( - response).await?; + let df = parse_grid_response_to_df(response).await + .map_err(|e| ConnectorError::PolarsError(e.to_string()))?; Ok(df) } status => Err(ConnectorError::HttpError( @@ -639,8 +644,8 @@ impl APIClient { match result { Ok(response) => match response.status() { StatusCode::OK => { - let df = parse_response_to_df( - response).await?; + let df = parse_response_to_df(response).await + .map_err(|e| ConnectorError::PolarsError(e.to_string()))?; Ok(df) } status => Err(ConnectorError::HttpError( @@ -726,8 +731,8 @@ impl APIClient { match result { Ok(response) => match response.status() { StatusCode::OK => { - let df = parse_response_to_df( - response).await?; + let df = parse_response_to_df(response).await + .map_err(|e| ConnectorError::PolarsError(e.to_string()))?; Ok(df) } status => Err(ConnectorError::HttpError( diff --git a/meteomatics/src/errors.rs b/meteomatics/src/errors.rs index 551dac5..7f87bcf 100644 --- a/meteomatics/src/errors.rs +++ b/meteomatics/src/errors.rs @@ -15,8 +15,8 @@ pub enum ConnectorError { LibraryError(String), /// Polars error. - #[error("Polars error")] - PolarsError, + #[error("Polars error: `{0}`")] + PolarsError(String), /// Generic error. #[error(transparent)] @@ -32,12 +32,6 @@ pub enum ConnectorError { } -impl From for ConnectorError { - fn from(_: polars::error::PolarsError) -> Self { - ConnectorError::PolarsError - } -} - impl From for ConnectorError { fn from(_: url::ParseError) -> Self { ConnectorError::ParseError