From 6253adf768b0ac3cd6f325f27f372501fd4bdeaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Gali=C4=87?= Date: Tue, 28 May 2019 17:54:01 +0200 Subject: [PATCH] simplify / unify error handling We want to return a Result, instead of a QueryResult. We can use map_err() to map it to the desired error type. thanks to review from @BaptisteGelez & @fdb-hiroshima. --- plume-models/src/blogs.rs | 5 +++-- src/routes/errors.rs | 6 ------ 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/plume-models/src/blogs.rs b/plume-models/src/blogs.rs index 5e36331c..ad6e40a2 100644 --- a/plume-models/src/blogs.rs +++ b/plume-models/src/blogs.rs @@ -1,6 +1,6 @@ use activitypub::{actor::Group, collection::OrderedCollection, object::Image, CustomObject}; use chrono::NaiveDateTime; -use diesel::{self, ExpressionMethods, QueryDsl, QueryResult, RunQueryDsl, SaveChangesDsl}; +use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl, SaveChangesDsl}; use openssl::{ hash::MessageDigest, pkey::{PKey, Private}, @@ -195,10 +195,11 @@ impl Blog { } } - pub fn find_by_host(c: &PlumeRocket, host: Host) -> QueryResult { + pub fn find_by_host(c: &PlumeRocket, host: Host) -> Result { blogs::table .filter(blogs::custom_domain.eq(host)) .first::(&*c.conn) + .map_err(|_| Error::NotFound) } fn fetch_from_webfinger(c: &PlumeRocket, acct: &str) -> Result { diff --git a/src/routes/errors.rs b/src/routes/errors.rs index 23f38030..1320a068 100644 --- a/src/routes/errors.rs +++ b/src/routes/errors.rs @@ -14,12 +14,6 @@ impl From for ErrorPage { } } -impl From for ErrorPage { - fn from(err: diesel::result::Error) -> ErrorPage { - ErrorPage(plume_models::Error::Db(err)) - } -} - impl<'r> Responder<'r> for ErrorPage { fn respond_to(self, req: &Request) -> response::Result<'r> { let rockets = req.guard::().unwrap();