error handling

This commit is contained in:
Felix Ableitner 2023-07-04 12:21:36 +02:00
parent d2eb1e608f
commit 2b56ac8a0d
7 changed files with 29 additions and 25 deletions

View file

@ -171,10 +171,10 @@ where
.build()
});
let dead_instances = CACHE
.get_with((), async {
Arc::new(Instance::dead_instances(data.pool()).await.unwrap())
.try_get_with((), async {
Ok::<_, diesel::result::Error>(Arc::new(Instance::dead_instances(data.pool()).await?))
})
.await;
.await?;
inbox.retain(|i| {
let domain = i.domain().expect("has domain").to_string();

View file

@ -11,7 +11,7 @@ use lemmy_db_schema::{
traits::Crud,
utils::DbPool,
};
use lemmy_utils::error::LemmyError;
use lemmy_utils::error::{LemmyError, LemmyResult};
use moka::future::Cache;
use once_cell::sync::Lazy;
use serde::Serialize;
@ -42,7 +42,9 @@ pub struct VerifyUrlData(pub DbPool);
#[async_trait]
impl UrlVerifier for VerifyUrlData {
async fn verify(&self, url: &Url) -> Result<(), &'static str> {
let local_site_data = local_site_data_cached(&self.0).await;
let local_site_data = local_site_data_cached(&self.0)
.await
.expect("read local site data");
check_apub_id_valid(url, &local_site_data)?;
Ok(())
}
@ -96,27 +98,29 @@ pub(crate) struct LocalSiteData {
blocked_instances: Vec<Instance>,
}
pub(crate) async fn local_site_data_cached(pool: &DbPool) -> Arc<LocalSiteData> {
pub(crate) async fn local_site_data_cached(pool: &DbPool) -> LemmyResult<Arc<LocalSiteData>> {
static CACHE: Lazy<Cache<(), Arc<LocalSiteData>>> = Lazy::new(|| {
Cache::builder()
.max_capacity(1)
.time_to_live(DB_QUERY_CACHE_DURATION)
.build()
});
CACHE
.get_with((), async {
// LocalSite may be missing
let local_site = LocalSite::read(pool).await.ok();
let allowed_instances = Instance::allowlist(pool).await.unwrap();
let blocked_instances = Instance::blocklist(pool).await.unwrap();
Ok(
CACHE
.try_get_with((), async {
// LocalSite may be missing
let local_site = LocalSite::read(pool).await.ok();
let allowed_instances = Instance::allowlist(pool).await?;
let blocked_instances = Instance::blocklist(pool).await?;
Arc::new(LocalSiteData {
local_site,
allowed_instances,
blocked_instances,
Ok::<_, diesel::result::Error>(Arc::new(LocalSiteData {
local_site,
allowed_instances,
blocked_instances,
}))
})
})
.await
.await?,
)
}
pub(crate) async fn check_apub_id_valid_with_strictness(
@ -133,7 +137,7 @@ pub(crate) async fn check_apub_id_valid_with_strictness(
return Ok(());
}
let local_site_data = local_site_data_cached(context.pool()).await;
let local_site_data = local_site_data_cached(context.pool()).await?;
check_apub_id_valid(apub_id, &local_site_data).map_err(LemmyError::from_message)?;
// Only check allowlist if this is a community, and there are instances in the allowlist

View file

@ -187,7 +187,7 @@ impl ApubCommunity {
) -> Result<Vec<Url>, LemmyError> {
let id = self.id;
let local_site_data = local_site_data_cached(context.pool()).await;
let local_site_data = local_site_data_cached(context.pool()).await?;
let follows = CommunityFollowerView::for_community(context.pool(), id).await?;
let inboxes: Vec<Url> = follows
.into_iter()

View file

@ -116,7 +116,7 @@ impl Object for ApubSite {
check_apub_id_valid_with_strictness(apub.id.inner(), true, data).await?;
verify_domains_match(expected_domain, apub.id.inner())?;
let local_site_data = local_site_data_cached(data.pool()).await;
let local_site_data = local_site_data_cached(data.pool()).await?;
let slur_regex = &local_site_opt_to_slur_regex(&local_site_data.local_site);
check_slurs(&apub.name, slur_regex)?;
check_slurs_opt(&apub.summary, slur_regex)?;

View file

@ -118,7 +118,7 @@ impl Object for ApubPerson {
expected_domain: &Url,
context: &Data<Self::DataType>,
) -> Result<(), LemmyError> {
let local_site_data = local_site_data_cached(context.pool()).await;
let local_site_data = local_site_data_cached(context.pool()).await?;
let slur_regex = &local_site_opt_to_slur_regex(&local_site_data.local_site);
check_slurs(&person.preferred_username, slur_regex)?;
check_slurs_opt(&person.name, slur_regex)?;

View file

@ -147,7 +147,7 @@ impl Object for ApubPost {
check_apub_id_valid_with_strictness(page.id.inner(), community.local, context).await?;
verify_person_in_community(&page.creator()?, &community, context).await?;
let local_site_data = local_site_data_cached(context.pool()).await;
let local_site_data = local_site_data_cached(context.pool()).await?;
let slur_regex = &local_site_opt_to_slur_regex(&local_site_data.local_site);
check_slurs_opt(&page.name, slur_regex)?;

View file

@ -83,11 +83,11 @@ impl Group {
check_apub_id_valid_with_strictness(self.id.inner(), true, context).await?;
verify_domains_match(expected_domain, self.id.inner())?;
let local_site_data = local_site_data_cached(context.pool()).await;
let local_site_data = local_site_data_cached(context.pool()).await?;
let slur_regex = &local_site_opt_to_slur_regex(&local_site_data.local_site);
check_slurs(&self.preferred_username, slur_regex)?;
check_slurs_opt(&self.name, slur_regex)?;
let description = read_from_string_or_source_opt(&self.summary, &None, &self.source);
check_slurs_opt(&description, slur_regex)?;
Ok(())