diff --git a/CHANGELOG.md b/CHANGELOG.md index 2313514..d5e76da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] - SimpleLogger init error handling +### Library +- The returned error in the Result is from now on anyhow::Error and not String. + ## [0.2.0] - 2022-01-23 ### Added #### CLI diff --git a/Cargo.toml b/Cargo.toml index e22584e..e3d8c69 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ chrono = "0.4.19" base64 = "0.13.0" notify-rust = "4.5.5" clap_complete = "3.0.5" +anyhow = "1.0.53" [dependencies.clap] version = "3.0.7" diff --git a/src/api/delete_image.rs b/src/api/delete_image.rs index bf3b45d..cfa523a 100644 --- a/src/api/delete_image.rs +++ b/src/api/delete_image.rs @@ -2,26 +2,24 @@ use crate::api::configuration::{api_url, ImgurHandle}; use super::send_api_request; -use log::error; +use anyhow::Error as anyhow_err; use reqwest::Method; -use std::process::exit; +use std::io::{Error, ErrorKind}; -pub async fn delete_image(c: ImgurHandle, delete_hash: String) -> Result { +pub async fn delete_image(c: ImgurHandle, delete_hash: String) -> Result { let uri = api_url!(format!("image/{delete_hash}")); - let res = send_api_request(&c, Method::DELETE, uri, None) - .await - .unwrap_or_else(|e| { - error!("send api request: {e}"); - exit(1) - }); + let res = send_api_request(&c, Method::DELETE, uri, None).await?; let status = res.status(); if status.is_client_error() || status.is_server_error() { - let body = res.text().await.map_err(|err| err.to_string())?; - Err(format!( - "server returned non-successful status code = {status}. body = {body}" - )) + let body = res.text().await.map_err(anyhow_err::new)?; + let err = Error::new( + ErrorKind::Other, + format!("server returned non-successful status code = {status}, body = {body}"), + ); + + Err(anyhow_err::from(err)) } else { Ok("If the delete hash was correct the image was deleted!".to_string()) } diff --git a/src/api/get_image.rs b/src/api/get_image.rs index b8a3240..da3d3c3 100644 --- a/src/api/get_image.rs +++ b/src/api/get_image.rs @@ -3,27 +3,25 @@ use crate::api::ImageInfo; use super::send_api_request; -use log::error; +use anyhow::Error as anyhow_err; use reqwest::Method; -use std::process::exit; +use std::io::{Error, ErrorKind}; -pub async fn get_image(c: ImgurHandle, image: &str) -> Result { +pub async fn get_image(c: ImgurHandle, image: &str) -> Result { let uri = api_url!(format!("image/{image}")); - let res = send_api_request(&c, Method::GET, uri, None) - .await - .unwrap_or_else(|e| { - error!("send api request: {e}"); - exit(1) - }); + let res = send_api_request(&c, Method::GET, uri, None).await?; let status = res.status(); if status.is_client_error() || status.is_server_error() { - Err(format!( - "server returned non-successful status code = {status}." - )) + let err = Error::new( + ErrorKind::Other, + format!("server returned non-successful status code = {status}"), + ); + + Err(anyhow_err::from(err)) } else { - let content: ImageInfo = res.json().await.map_err(|e| e.to_string())?; + let content: ImageInfo = res.json().await.map_err(anyhow_err::new)?; Ok(content) } } diff --git a/src/api/rate_limit.rs b/src/api/rate_limit.rs index e9f48f6..101cdf6 100644 --- a/src/api/rate_limit.rs +++ b/src/api/rate_limit.rs @@ -2,10 +2,10 @@ use crate::api::configuration::{api_url, ImgurHandle}; use super::send_api_request; -use log::error; +use anyhow::Error as anyhow_err; use reqwest::Method; use serde_derive::{Deserialize, Serialize}; -use std::process::exit; +use std::io::{Error, ErrorKind}; #[derive(Debug, Serialize, Deserialize)] pub struct RateLimitInfo { @@ -28,24 +28,22 @@ pub struct RateLimitData { pub client_remaining: i32, } -pub async fn rate_limit(c: ImgurHandle) -> Result { +pub async fn rate_limit(c: ImgurHandle) -> Result { let uri = api_url!("credits"); - let res = send_api_request(&c, Method::GET, uri, None) - .await - .unwrap_or_else(|e| { - error!("send api request: {e}"); - exit(1) - }); + let res = send_api_request(&c, Method::GET, uri, None).await?; let status = res.status(); if status.is_client_error() || status.is_server_error() { - let body = res.text().await.map_err(|e| e.to_string())?; - Err(format!( - "server returned non-successful status code = {status}. body = {body}" - )) + let body = res.text().await.map_err(anyhow_err::new)?; + let err = Error::new( + ErrorKind::Other, + format!("server returned non-successful status code = {status}, body = {body}"), + ); + + Err(anyhow_err::from(err)) } else { - let content: RateLimitInfo = res.json().await.map_err(|e| e.to_string())?; + let content: RateLimitInfo = res.json().await.map_err(anyhow_err::new)?; Ok(content) } } diff --git a/src/api/send_request.rs b/src/api/send_request.rs index 39353d5..250bc89 100644 --- a/src/api/send_request.rs +++ b/src/api/send_request.rs @@ -1,5 +1,6 @@ use super::ImgurHandle; +use anyhow::Error; use reqwest::Method; use std::collections::HashMap; @@ -8,7 +9,7 @@ pub async fn send_api_request( method: Method, uri: String, form: Option>, -) -> Result { +) -> Result { let client = &config.client; let mut req = client.request(method, uri.as_str()); @@ -28,5 +29,5 @@ pub async fn send_api_request( let req = req.build()?; - client.execute(req).await + client.execute(req).await.map_err(Error::from) } diff --git a/src/api/upload_image.rs b/src/api/upload_image.rs index cb537a4..7f5269a 100644 --- a/src/api/upload_image.rs +++ b/src/api/upload_image.rs @@ -5,34 +5,33 @@ use crate::api::{ use super::send_api_request; -use log::error; +use anyhow::Error as anyhow_err; use reqwest::Method; -use std::{collections::HashMap, process::exit}; +use std::{ + collections::HashMap, + io::{Error, ErrorKind}, +}; -pub async fn upload_image(c: ImgurHandle, image: &str) -> Result { +pub async fn upload_image(c: ImgurHandle, image: &str) -> Result { let mut form = HashMap::new(); form.insert("image", image.to_string()); let uri = api_url!("image"); - let res = send_api_request(&c, Method::POST, uri, Some(form)) - .await - .unwrap_or_else(|e| { - error!("send api request: {e}"); - exit(1) - }); + let res = send_api_request(&c, Method::POST, uri, Some(form)).await?; let status = res.status(); if status.is_client_error() || status.is_server_error() { - let body = res.text().await.map_err(|e| e.to_string())?; + let body = res.text().await.map_err(anyhow_err::new)?; + let err = Error::new( + ErrorKind::Other, + format!("server returned non-successful status code = {status}, body = {body}"), + ); - Err(format!( - "server returned non-successful status code = {status}. body = {body}" - )) + Err(anyhow_err::from(err)) } else { - let content: ImageInfo = res.json().await.map_err(|e| e.to_string())?; - + let content: ImageInfo = res.json().await.map_err(anyhow_err::new)?; Ok(content) } } diff --git a/src/config/toml.rs b/src/config/toml.rs index f1986eb..d10d269 100644 --- a/src/config/toml.rs +++ b/src/config/toml.rs @@ -1,5 +1,6 @@ use super::Config; +use anyhow::Error; use dirs::config_dir; use log::{error, warn}; use std::{ @@ -10,6 +11,8 @@ use std::{ }; use toml::from_str; +const CONFIG_DIR: &str = "/imgurs/config.toml"; + pub fn parse() -> Config { toml().unwrap_or_else(|e| { warn!("Parse toml config: {e}! Creating config file..."); @@ -17,7 +20,7 @@ pub fn parse() -> Config { let default_config = include_str!(concat!("../../config.toml")); let sys_config_dir = config_dir().unwrap(); - let config_dir = format!("{}/imgurs/config.toml", sys_config_dir.to_string_lossy()); + let config_dir = format!("{}{CONFIG_DIR}", sys_config_dir.to_string_lossy()); let config_path = Path::new(&config_dir); let prefix = config_path.parent().unwrap(); @@ -31,19 +34,19 @@ pub fn parse() -> Config { toml().unwrap_or_else(|e| { error!("parse toml config: {e}"); - exit(2); + exit(3); }) }) } -fn toml() -> Result { +fn toml() -> Result { let config_dir = config_dir().unwrap(); - let file_dir: String = String::from(config_dir.to_string_lossy()) + "/imgurs/config.toml"; + let file_dir: String = String::from(config_dir.to_string_lossy()) + CONFIG_DIR; - let toml_str = read_to_string(file_dir).map_err(|err| err.to_string())?; + let toml_str = read_to_string(file_dir).map_err(|e| e)?; - let decode = from_str(&toml_str).map_err(|err| err.to_string())?; + let decode = from_str(&toml_str).map_err(|e| e)?; Ok(decode) }