diff --git a/meteomatics/src/client.rs b/meteomatics/src/client.rs index ae566b3..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( @@ -106,7 +106,7 @@ impl APIClient { status, )), }, - Err(_) => Err(ConnectorError::ReqwestError), + Err(e) => Err(ConnectorError::ReqwestError(e.to_string())), } } @@ -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( @@ -182,7 +182,7 @@ impl APIClient { status, )), }, - Err(_) => Err(ConnectorError::ReqwestError), + Err(e) => Err(ConnectorError::ReqwestError(e.to_string())), } } @@ -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( @@ -264,7 +267,7 @@ impl APIClient { status, )), }, - Err(_) => Err(ConnectorError::ReqwestError), + Err(e) => Err(ConnectorError::ReqwestError(e.to_string())), } } @@ -297,7 +300,7 @@ impl APIClient { status, )), }, - Err(_) => Err(ConnectorError::ReqwestError), + Err(e) => Err(ConnectorError::ReqwestError(e.to_string())), } } @@ -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) } } @@ -390,7 +394,7 @@ impl APIClient { status, )), }, - Err(_) => Err(ConnectorError::ReqwestError), + Err(e) => Err(ConnectorError::ReqwestError(e.to_string())), } } @@ -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) } } @@ -482,7 +487,7 @@ impl APIClient { status, )), }, - Err(_) => Err(ConnectorError::ReqwestError), + Err(e) => Err(ConnectorError::ReqwestError(e.to_string())), } } @@ -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( @@ -564,7 +569,7 @@ impl APIClient { status, )), }, - Err(_) => Err(ConnectorError::ReqwestError), + Err(e) => Err(ConnectorError::ReqwestError(e.to_string())), } } @@ -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( @@ -649,7 +654,7 @@ impl APIClient { status, )), }, - Err(_) => Err(ConnectorError::ReqwestError), + Err(e) => Err(ConnectorError::ReqwestError(e.to_string())), } } @@ -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( @@ -736,7 +741,7 @@ impl APIClient { status, )), }, - Err(_) => Err(ConnectorError::ReqwestError), + Err(e) => Err(ConnectorError::ReqwestError(e.to_string())), } } @@ -829,7 +834,7 @@ impl APIClient { status, )), }, - Err(_) => Err(ConnectorError::ReqwestError), + Err(e) => Err(ConnectorError::ReqwestError(e.to_string())), } } @@ -917,7 +922,7 @@ impl APIClient { status, )), }, - Err(_) => Err(ConnectorError::ReqwestError), + Err(e) => Err(ConnectorError::ReqwestError(e.to_string())), } } @@ -1000,7 +1005,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..7f87bcf 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}`")] @@ -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,24 +32,12 @@ 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 } } -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)?;