mirror of https://github.com/MedzikUser/imgurs
library: return anyhow::Error instead String
This commit is contained in:
parent
62c0352345
commit
5a620ea1c2
|
@ -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
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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<String, String> {
|
||||
pub async fn delete_image(c: ImgurHandle, delete_hash: String) -> Result<String, anyhow_err> {
|
||||
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())
|
||||
}
|
||||
|
|
|
@ -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<ImageInfo, String> {
|
||||
pub async fn get_image(c: ImgurHandle, image: &str) -> Result<ImageInfo, anyhow_err> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<RateLimitInfo, String> {
|
||||
pub async fn rate_limit(c: ImgurHandle) -> Result<RateLimitInfo, anyhow_err> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<HashMap<&str, String>>,
|
||||
) -> Result<reqwest::Response, reqwest::Error> {
|
||||
) -> Result<reqwest::Response, Error> {
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -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<ImageInfo, String> {
|
||||
pub async fn upload_image(c: ImgurHandle, image: &str) -> Result<ImageInfo, anyhow_err> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Config, String> {
|
||||
fn toml() -> Result<Config, Error> {
|
||||
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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue