add an argument in the macro may_fail to have the account linked in the error template

This commit is contained in:
Didier Link 2018-06-22 00:50:06 +02:00
parent 9abb5887b1
commit b153a9ce2b
4 changed files with 12 additions and 11 deletions

View File

@ -19,7 +19,7 @@ use utils;
#[get("/~/<name>", rank = 2)] #[get("/~/<name>", rank = 2)]
fn details(name: String, conn: DbConn, user: Option<User>) -> Template { fn details(name: String, conn: DbConn, user: Option<User>) -> Template {
may_fail!(Blog::find_by_fqn(&*conn, name), "Requested blog couldn't be found", |blog| { may_fail!(user, Blog::find_by_fqn(&*conn, name), "Requested blog couldn't be found", |blog| {
let recents = Post::get_recents_for_blog(&*conn, &blog, 5); let recents = Post::get_recents_for_blog(&*conn, &blog, 5);
Template::render("blogs/details", json!({ Template::render("blogs/details", json!({

View File

@ -2,7 +2,7 @@ use rocket::response::NamedFile;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
macro_rules! may_fail { macro_rules! may_fail {
($expr:expr, $template:expr, $msg:expr, | $res:ident | $block:block) => { ($account:expr, $expr:expr, $template:expr, $msg:expr, | $res:ident | $block:block) => {
{ {
let res = $expr; let res = $expr;
if res.is_some() { if res.is_some() {
@ -10,18 +10,19 @@ macro_rules! may_fail {
$block $block
} else { } else {
Template::render(concat!("errors/", $template), json!({ Template::render(concat!("errors/", $template), json!({
"error_message": $msg "error_message": $msg,
"account": $account
})) }))
} }
} }
}; };
($expr:expr, $msg:expr, | $res:ident | $block:block) => { ($account:expr, $expr:expr, $msg:expr, | $res:ident | $block:block) => {
may_fail!($expr, "404", $msg, |$res| { may_fail!($account, $expr, "404", $msg, |$res| {
$block $block
}) })
}; };
($expr:expr, | $res:ident | $block:block) => { ($account:expr, $expr:expr, | $res:ident | $block:block) => {
may_fail!($expr, "", |$res| { may_fail!($account, $expr, "", |$res| {
$block $block
}) })
}; };

View File

@ -27,8 +27,8 @@ fn details(blog: String, slug: String, conn: DbConn, user: Option<User>) -> Temp
#[get("/~/<blog>/<slug>?<query>")] #[get("/~/<blog>/<slug>?<query>")]
fn details_response(blog: String, slug: String, conn: DbConn, user: Option<User>, query: Option<CommentQuery>) -> Template { fn details_response(blog: String, slug: String, conn: DbConn, user: Option<User>, query: Option<CommentQuery>) -> Template {
may_fail!(Blog::find_by_fqn(&*conn, blog), "Couldn't find this blog", |blog| { may_fail!(user, Blog::find_by_fqn(&*conn, blog), "Couldn't find this blog", |blog| {
may_fail!(Post::find_by_slug(&*conn, slug, blog.id), "Couldn't find this post", |post| { may_fail!(user, Post::find_by_slug(&*conn, slug, blog.id), "Couldn't find this post", |post| {
let comments = Comment::list_by_post(&*conn, post.id); let comments = Comment::list_by_post(&*conn, post.id);
Template::render("posts/details", json!({ Template::render("posts/details", json!({

View File

@ -33,7 +33,7 @@ fn me(user: Option<User>) -> Result<Redirect, Flash<Redirect>> {
#[get("/@/<name>", rank = 2)] #[get("/@/<name>", rank = 2)]
fn details(name: String, conn: DbConn, account: Option<User>) -> Template { fn details(name: String, conn: DbConn, account: Option<User>) -> Template {
may_fail!(User::find_by_fqn(&*conn, name), "Couldn't find requested user", |user| { may_fail!(account, User::find_by_fqn(&*conn, name), "Couldn't find requested user", |user| {
let recents = Post::get_recents_for_author(&*conn, &user, 6); let recents = Post::get_recents_for_author(&*conn, &user, 6);
let reshares = Reshare::get_recents_for_author(&*conn, &user, 6); let reshares = Reshare::get_recents_for_author(&*conn, &user, 6);
let user_id = user.id.clone(); let user_id = user.id.clone();
@ -92,7 +92,7 @@ fn follow_auth(name: String) -> Flash<Redirect> {
#[get("/@/<name>/followers", rank = 2)] #[get("/@/<name>/followers", rank = 2)]
fn followers(name: String, conn: DbConn, account: Option<User>) -> Template { fn followers(name: String, conn: DbConn, account: Option<User>) -> Template {
may_fail!(User::find_by_fqn(&*conn, name.clone()), "Couldn't find requested user", |user| { may_fail!(account, User::find_by_fqn(&*conn, name.clone()), "Couldn't find requested user", |user| {
let user_id = user.id.clone(); let user_id = user.id.clone();
Template::render("users/followers", json!({ Template::render("users/followers", json!({