Migrate from failure to anyhow and thiserror (#1042)

* Migrate from failure to anyhow and thiserror

* Replace 'format_err!' to 'anyhow!'
This commit is contained in:
andor0 2020-08-01 18:04:42 +04:00 committed by GitHub
parent 78603e7e31
commit b6411aba7a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 53 additions and 70 deletions

43
server/Cargo.lock generated vendored
View file

@ -406,6 +406,12 @@ dependencies = [
"winapi 0.3.9", "winapi 0.3.9",
] ]
[[package]]
name = "anyhow"
version = "1.0.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b602bfe940d21c130f3895acd65221e8a61270debe89d628b9cb4e3ccb8569b"
[[package]] [[package]]
name = "arc-swap" name = "arc-swap"
version = "0.4.7" version = "0.4.7"
@ -1194,28 +1200,6 @@ dependencies = [
"termcolor", "termcolor",
] ]
[[package]]
name = "failure"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86"
dependencies = [
"backtrace",
"failure_derive",
]
[[package]]
name = "failure_derive"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4"
dependencies = [
"proc-macro2",
"quote",
"syn",
"synstructure",
]
[[package]] [[package]]
name = "fake-simd" name = "fake-simd"
version = "0.1.2" version = "0.1.2"
@ -1733,6 +1717,7 @@ dependencies = [
"actix-rt", "actix-rt",
"actix-web", "actix-web",
"actix-web-actors", "actix-web-actors",
"anyhow",
"async-trait", "async-trait",
"awc", "awc",
"base64 0.12.3", "base64 0.12.3",
@ -1743,7 +1728,6 @@ dependencies = [
"diesel_migrations", "diesel_migrations",
"dotenv", "dotenv",
"env_logger", "env_logger",
"failure",
"futures", "futures",
"http", "http",
"http-signature-normalization-actix", "http-signature-normalization-actix",
@ -1762,6 +1746,7 @@ dependencies = [
"sha2", "sha2",
"strum", "strum",
"strum_macros", "strum_macros",
"thiserror",
"tokio", "tokio",
"url", "url",
"uuid 0.8.1", "uuid 0.8.1",
@ -3116,18 +3101,6 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "synstructure"
version = "0.12.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701"
dependencies = [
"proc-macro2",
"quote",
"syn",
"unicode-xid",
]
[[package]] [[package]]
name = "tempfile" name = "tempfile"
version = "3.1.0" version = "3.1.0"

3
server/Cargo.toml vendored
View file

@ -23,7 +23,6 @@ activitystreams-ext = { git = "https://yerbamate.dev/asonix/activitystreams-ext"
bcrypt = "0.8.0" bcrypt = "0.8.0"
chrono = { version = "0.4.7", features = ["serde"] } chrono = { version = "0.4.7", features = ["serde"] }
serde_json = { version = "1.0.52", features = ["preserve_order"]} serde_json = { version = "1.0.52", features = ["preserve_order"]}
failure = "0.1.8"
serde = { version = "1.0.105", features = ["derive"] } serde = { version = "1.0.105", features = ["derive"] }
actix = "0.10.0-alpha.2" actix = "0.10.0-alpha.2"
actix-web = { version = "3.0.0-alpha.3", features = ["rustls"] } actix-web = { version = "3.0.0-alpha.3", features = ["rustls"] }
@ -52,3 +51,5 @@ uuid = { version = "0.8", features = ["serde", "v4"] }
sha2 = "0.9" sha2 = "0.9"
async-trait = "0.1.36" async-trait = "0.1.36"
captcha = "0.0.7" captcha = "0.0.7"
anyhow = "1.0.32"
thiserror = "1.0.20"

View file

@ -9,6 +9,7 @@ use lemmy_db::{
user_view::*, user_view::*,
Crud, Crud,
}; };
use thiserror::Error;
pub mod claims; pub mod claims;
pub mod comment; pub mod comment;
@ -17,8 +18,8 @@ pub mod post;
pub mod site; pub mod site;
pub mod user; pub mod user;
#[derive(Fail, Debug)] #[derive(Debug, Error)]
#[fail(display = "{{\"error\":\"{}\"}}", message)] #[error("{{\"error\":\"{message}\"}}")]
pub struct APIError { pub struct APIError {
pub message: String, pub message: String,
} }

View file

@ -2,6 +2,7 @@ use crate::{apub::ActorType, LemmyError};
use activitystreams_ext::UnparsedExtension; use activitystreams_ext::UnparsedExtension;
use activitystreams_new::unparsed::UnparsedMutExt; use activitystreams_new::unparsed::UnparsedMutExt;
use actix_web::{client::ClientRequest, HttpRequest}; use actix_web::{client::ClientRequest, HttpRequest};
use anyhow::anyhow;
use http_signature_normalization_actix::{ use http_signature_normalization_actix::{
digest::{DigestClient, SignExt}, digest::{DigestClient, SignExt},
Config, Config,
@ -70,7 +71,7 @@ pub fn verify(request: &HttpRequest, actor: &dyn ActorType) -> Result<(), LemmyE
debug!("verified signature for {}", &request.uri()); debug!("verified signature for {}", &request.uri());
Ok(()) Ok(())
} else { } else {
Err(format_err!("Invalid signature on request: {}", &request.uri()).into()) Err(anyhow!("Invalid signature on request: {}", &request.uri()).into())
} }
} }

View file

@ -17,6 +17,7 @@ use crate::{
}; };
use activitystreams_new::{base::BaseExt, collection::OrderedCollection, object::Note, prelude::*}; use activitystreams_new::{base::BaseExt, collection::OrderedCollection, object::Note, prelude::*};
use actix_web::client::Client; use actix_web::client::Client;
use anyhow::anyhow;
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use diesel::{result::Error::NotFound, PgConnection}; use diesel::{result::Error::NotFound, PgConnection};
use lemmy_db::{ use lemmy_db::{
@ -66,7 +67,7 @@ where
Response: for<'de> Deserialize<'de>, Response: for<'de> Deserialize<'de>,
{ {
if !is_apub_id_valid(&url) { if !is_apub_id_valid(&url) {
return Err(format_err!("Activitypub uri invalid or blocked: {}", url).into()); return Err(anyhow!("Activitypub uri invalid or blocked: {}", url).into());
} }
let timeout = Duration::from_secs(60); let timeout = Duration::from_secs(60);
@ -125,10 +126,10 @@ pub async fn search_by_apub_id(
let split2 = split[0].split('!').collect::<Vec<&str>>(); let split2 = split[0].split('!').collect::<Vec<&str>>();
(format!("/c/{}", split2[1]), split[1]) (format!("/c/{}", split2[1]), split[1])
} else { } else {
return Err(format_err!("Invalid search query: {}", query).into()); return Err(anyhow!("Invalid search query: {}", query).into());
} }
} else { } else {
return Err(format_err!("Invalid search query: {}", query).into()); return Err(anyhow!("Invalid search query: {}", query).into());
}; };
let url = format!("{}://{}{}", get_apub_protocol_string(), instance, name); let url = format!("{}://{}{}", get_apub_protocol_string(), instance, name);

View file

@ -22,6 +22,7 @@ use crate::{
}; };
use activitystreams_new::{activity::*, base::AnyBase, object::Note, prelude::*}; use activitystreams_new::{activity::*, base::AnyBase, object::Note, prelude::*};
use actix_web::{client::Client, HttpResponse}; use actix_web::{client::Client, HttpResponse};
use anyhow::anyhow;
use lemmy_db::{ use lemmy_db::{
comment::{Comment, CommentForm, CommentLike, CommentLikeForm}, comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
comment_view::CommentView, comment_view::CommentView,
@ -63,7 +64,7 @@ async fn receive_undo_delete(
"Note" => receive_undo_delete_comment(undo, &delete, client, pool, chat_server).await, "Note" => receive_undo_delete_comment(undo, &delete, client, pool, chat_server).await,
"Page" => receive_undo_delete_post(undo, &delete, client, pool, chat_server).await, "Page" => receive_undo_delete_post(undo, &delete, client, pool, chat_server).await,
"Group" => receive_undo_delete_community(undo, &delete, client, pool, chat_server).await, "Group" => receive_undo_delete_community(undo, &delete, client, pool, chat_server).await,
d => Err(format_err!("Undo Delete type {} not supported", d).into()), d => Err(anyhow!("Undo Delete type {} not supported", d).into()),
} }
} }
@ -80,7 +81,7 @@ async fn receive_undo_remove(
"Note" => receive_undo_remove_comment(undo, &remove, client, pool, chat_server).await, "Note" => receive_undo_remove_comment(undo, &remove, client, pool, chat_server).await,
"Page" => receive_undo_remove_post(undo, &remove, client, pool, chat_server).await, "Page" => receive_undo_remove_post(undo, &remove, client, pool, chat_server).await,
"Group" => receive_undo_remove_community(undo, &remove, client, pool, chat_server).await, "Group" => receive_undo_remove_community(undo, &remove, client, pool, chat_server).await,
d => Err(format_err!("Undo Delete type {} not supported", d).into()), d => Err(anyhow!("Undo Delete type {} not supported", d).into()),
} }
} }
@ -96,7 +97,7 @@ async fn receive_undo_like(
match type_ { match type_ {
"Note" => receive_undo_like_comment(undo, &like, client, pool, chat_server).await, "Note" => receive_undo_like_comment(undo, &like, client, pool, chat_server).await,
"Page" => receive_undo_like_post(undo, &like, client, pool, chat_server).await, "Page" => receive_undo_like_post(undo, &like, client, pool, chat_server).await,
d => Err(format_err!("Undo Delete type {} not supported", d).into()), d => Err(anyhow!("Undo Delete type {} not supported", d).into()),
} }
} }
@ -109,7 +110,7 @@ async fn receive_undo_dislike(
let dislike = Dislike::from_any_base(undo.object().to_owned().one().unwrap())?.unwrap(); let dislike = Dislike::from_any_base(undo.object().to_owned().one().unwrap())?.unwrap();
let type_ = dislike.object().as_single_kind_str().unwrap(); let type_ = dislike.object().as_single_kind_str().unwrap();
Err(format_err!("Undo Delete type {} not supported", type_).into()) Err(anyhow!("Undo Delete type {} not supported", type_).into())
} }
async fn receive_undo_delete_comment( async fn receive_undo_delete_comment(

View file

@ -14,6 +14,7 @@ use activitystreams_new::{
prelude::*, prelude::*,
}; };
use actix_web::{client::Client, web, HttpRequest, HttpResponse}; use actix_web::{client::Client, web, HttpRequest, HttpResponse};
use anyhow::anyhow;
use lemmy_db::{ use lemmy_db::{
community::{Community, CommunityFollower, CommunityFollowerForm}, community::{Community, CommunityFollower, CommunityFollowerForm},
user::User_, user::User_,
@ -57,7 +58,7 @@ pub async fn community_inbox(
if !community.local { if !community.local {
return Err( return Err(
format_err!( anyhow!(
"Received activity is addressed to remote community {}", "Received activity is addressed to remote community {}",
&community.actor_id &community.actor_id
) )

View file

@ -28,8 +28,8 @@ use activitystreams_new::{
prelude::*, prelude::*,
}; };
use actix_web::{body::Body, client::Client, HttpResponse}; use actix_web::{body::Body, client::Client, HttpResponse};
use anyhow::anyhow;
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use failure::_core::fmt::Debug;
use lemmy_db::{activity::do_insert_activity, user::User_}; use lemmy_db::{activity::do_insert_activity, user::User_};
use lemmy_utils::{convert_datetime, get_apub_protocol_string, settings::Settings, MentionData}; use lemmy_utils::{convert_datetime, get_apub_protocol_string, settings::Settings, MentionData};
use log::debug; use log::debug;
@ -118,10 +118,10 @@ where
tombstone.set_deleted(convert_datetime(updated)); tombstone.set_deleted(convert_datetime(updated));
Ok(tombstone) Ok(tombstone)
} else { } else {
Err(format_err!("Cant convert to tombstone because updated time was None.").into()) Err(anyhow!("Cant convert to tombstone because updated time was None.").into())
} }
} else { } else {
Err(format_err!("Cant convert object to tombstone if it wasnt deleted").into()) Err(anyhow!("Cant convert object to tombstone if it wasnt deleted").into())
} }
} }
@ -339,13 +339,13 @@ pub async fn fetch_webfinger_url(
.links .links
.iter() .iter()
.find(|l| l.type_.eq(&Some("application/activity+json".to_string()))) .find(|l| l.type_.eq(&Some("application/activity+json".to_string())))
.ok_or_else(|| format_err!("No application/activity+json link found."))?; .ok_or_else(|| anyhow!("No application/activity+json link found."))?;
link link
.href .href
.to_owned() .to_owned()
.map(|u| Url::parse(&u)) .map(|u| Url::parse(&u))
.transpose()? .transpose()?
.ok_or_else(|| format_err!("No href found.").into()) .ok_or_else(|| anyhow!("No href found.").into())
} }
pub async fn insert_activity<T>( pub async fn insert_activity<T>(
@ -355,7 +355,7 @@ pub async fn insert_activity<T>(
pool: &DbPool, pool: &DbPool,
) -> Result<(), LemmyError> ) -> Result<(), LemmyError>
where where
T: Serialize + Debug + Send + 'static, T: Serialize + std::fmt::Debug + Send + 'static,
{ {
blocking(pool, move |conn| { blocking(pool, move |conn| {
do_insert_activity(conn, user_id, &data, local) do_insert_activity(conn, user_id, &data, local)

View file

@ -3,8 +3,6 @@
pub extern crate strum_macros; pub extern crate strum_macros;
#[macro_use] #[macro_use]
pub extern crate lazy_static; pub extern crate lazy_static;
#[macro_use]
pub extern crate failure;
pub extern crate actix; pub extern crate actix;
pub extern crate actix_web; pub extern crate actix_web;
pub extern crate base64; pub extern crate base64;
@ -33,6 +31,7 @@ pub mod websocket;
use crate::request::{retry, RecvError}; use crate::request::{retry, RecvError};
use actix_web::{client::Client, dev::ConnectionInfo}; use actix_web::{client::Client, dev::ConnectionInfo};
use anyhow::anyhow;
use lemmy_utils::{get_apub_protocol_string, settings::Settings}; use lemmy_utils::{get_apub_protocol_string, settings::Settings};
use log::error; use log::error;
use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC}; use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
@ -48,12 +47,12 @@ pub type IPAddr = String;
#[derive(Debug)] #[derive(Debug)]
pub struct LemmyError { pub struct LemmyError {
inner: failure::Error, inner: anyhow::Error,
} }
impl<T> From<T> for LemmyError impl<T> From<T> for LemmyError
where where
T: Into<failure::Error>, T: Into<anyhow::Error>,
{ {
fn from(t: T) -> Self { fn from(t: T) -> Self {
LemmyError { inner: t.into() } LemmyError { inner: t.into() }
@ -118,7 +117,7 @@ pub async fn fetch_pictrs(client: &Client, image_url: &str) -> Result<PictrsResp
if response.msg == "ok" { if response.msg == "ok" {
Ok(response) Ok(response)
} else { } else {
Err(format_err!("{}", &response.msg).into()) Err(anyhow!("{}", &response.msg).into())
} }
} }
@ -191,13 +190,13 @@ pub async fn is_image_content_type(client: &Client, test: &str) -> Result<(), Le
if response if response
.headers() .headers()
.get("Content-Type") .get("Content-Type")
.ok_or_else(|| format_err!("No Content-Type header"))? .ok_or_else(|| anyhow!("No Content-Type header"))?
.to_str()? .to_str()?
.starts_with("image/") .starts_with("image/")
{ {
Ok(()) Ok(())
} else { } else {
Err(format_err!("Not an image type.").into()) Err(anyhow!("Not an image type.").into())
} }
} }

View file

@ -1,12 +1,14 @@
use crate::LemmyError; use crate::LemmyError;
use anyhow::anyhow;
use std::future::Future; use std::future::Future;
use thiserror::Error;
#[derive(Clone, Debug, Fail)] #[derive(Clone, Debug, Error)]
#[fail(display = "Error sending request, {}", _0)] #[error("Error sending request, {0}")]
struct SendError(pub String); struct SendError(pub String);
#[derive(Clone, Debug, Fail)] #[derive(Clone, Debug, Error)]
#[fail(display = "Error receiving response, {}", _0)] #[error("Error receiving response, {0}")]
pub struct RecvError(pub String); pub struct RecvError(pub String);
pub async fn retry<F, Fut, T>(f: F) -> Result<T, LemmyError> pub async fn retry<F, Fut, T>(f: F) -> Result<T, LemmyError>
@ -22,7 +24,7 @@ where
F: Fn() -> Fut, F: Fn() -> Fut,
Fut: Future<Output = Result<Result<T, actix_web::client::SendRequestError>, LemmyError>>, Fut: Future<Output = Result<Result<T, actix_web::client::SendRequestError>, LemmyError>>,
{ {
let mut response = Err(format_err!("connect timeout").into()); let mut response = Err(anyhow!("connect timeout").into());
for _ in 0u8..3 { for _ in 0u8..3 {
match (f)().await? { match (f)().await? {

View file

@ -1,5 +1,6 @@
use crate::{api::claims::Claims, blocking, routes::DbPoolParam, LemmyError}; use crate::{api::claims::Claims, blocking, routes::DbPoolParam, LemmyError};
use actix_web::{error::ErrorBadRequest, *}; use actix_web::{error::ErrorBadRequest, *};
use anyhow::anyhow;
use chrono::{DateTime, NaiveDateTime, Utc}; use chrono::{DateTime, NaiveDateTime, Utc};
use diesel::{ use diesel::{
r2d2::{ConnectionManager, Pool}, r2d2::{ConnectionManager, Pool},
@ -88,7 +89,7 @@ async fn get_feed(
"c" => RequestType::Community, "c" => RequestType::Community,
"front" => RequestType::Front, "front" => RequestType::Front,
"inbox" => RequestType::Inbox, "inbox" => RequestType::Inbox,
_ => return Err(ErrorBadRequest(LemmyError::from(format_err!("wrong_type")))), _ => return Err(ErrorBadRequest(LemmyError::from(anyhow!("wrong_type")))),
}; };
let param = path.1.to_owned(); let param = path.1.to_owned();

View file

@ -1,5 +1,6 @@
use crate::{blocking, routes::DbPoolParam, version, LemmyError}; use crate::{blocking, routes::DbPoolParam, version, LemmyError};
use actix_web::{body::Body, error::ErrorBadRequest, *}; use actix_web::{body::Body, error::ErrorBadRequest, *};
use anyhow::anyhow;
use lemmy_db::site_view::SiteView; use lemmy_db::site_view::SiteView;
use lemmy_utils::{get_apub_protocol_string, settings::Settings}; use lemmy_utils::{get_apub_protocol_string, settings::Settings};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -28,7 +29,7 @@ async fn node_info_well_known() -> Result<HttpResponse<Body>, LemmyError> {
async fn node_info(db: DbPoolParam) -> Result<HttpResponse, Error> { async fn node_info(db: DbPoolParam) -> Result<HttpResponse, Error> {
let site_view = blocking(&db, SiteView::read) let site_view = blocking(&db, SiteView::read)
.await? .await?
.map_err(|_| ErrorBadRequest(LemmyError::from(format_err!("not_found"))))?; .map_err(|_| ErrorBadRequest(LemmyError::from(anyhow!("not_found"))))?;
let protocols = if Settings::get().federation.enabled { let protocols = if Settings::get().federation.enabled {
vec!["activitypub".to_string()] vec!["activitypub".to_string()]

View file

@ -1,5 +1,6 @@
use crate::{blocking, routes::DbPoolParam, LemmyError}; use crate::{blocking, routes::DbPoolParam, LemmyError};
use actix_web::{error::ErrorBadRequest, web::Query, *}; use actix_web::{error::ErrorBadRequest, web::Query, *};
use anyhow::anyhow;
use lemmy_db::{community::Community, user::User_}; use lemmy_db::{community::Community, user::User_};
use lemmy_utils::{settings::Settings, WEBFINGER_COMMUNITY_REGEX, WEBFINGER_USER_REGEX}; use lemmy_utils::{settings::Settings, WEBFINGER_COMMUNITY_REGEX, WEBFINGER_USER_REGEX};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -62,17 +63,17 @@ async fn get_webfinger_response(
Community::read_from_name(conn, &community_name) Community::read_from_name(conn, &community_name)
}) })
.await? .await?
.map_err(|_| ErrorBadRequest(LemmyError::from(format_err!("not_found"))))? .map_err(|_| ErrorBadRequest(LemmyError::from(anyhow!("not_found"))))?
.actor_id .actor_id
} else if let Some(user_name) = user_regex_parsed { } else if let Some(user_name) = user_regex_parsed {
let user_name = user_name.as_str().to_owned(); let user_name = user_name.as_str().to_owned();
// Make sure the requested user exists. // Make sure the requested user exists.
blocking(&db, move |conn| User_::read_from_name(conn, &user_name)) blocking(&db, move |conn| User_::read_from_name(conn, &user_name))
.await? .await?
.map_err(|_| ErrorBadRequest(LemmyError::from(format_err!("not_found"))))? .map_err(|_| ErrorBadRequest(LemmyError::from(anyhow!("not_found"))))?
.actor_id .actor_id
} else { } else {
return Err(ErrorBadRequest(LemmyError::from(format_err!("not_found")))); return Err(ErrorBadRequest(LemmyError::from(anyhow!("not_found"))));
}; };
let json = WebFingerResponse { let json = WebFingerResponse {