From a14ece6af3a2d0f03af072c337ef50e311a79066 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sat, 26 May 2018 10:53:31 +0200 Subject: [PATCH] feat(errors): Introduce error variant for thread closing (other minor change: log the user ID when invalid post editing requests were constructed) --- src/errors.rs | 16 ++++++++++++---- src/handlers.rs | 10 ++++++++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index 3f00a2976..c73379f0c 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -56,8 +56,11 @@ pub enum ConverseError { #[fail(display = "error occured running timer: {}", error)] Timer { error: tokio_timer::Error }, - #[fail(display = "user does not have permission to edit post {}", id)] - PostEditForbidden { id: i32 }, + #[fail(display = "user {} does not have permission to edit post {}", user, id)] + PostEditForbidden { user: i32, id: i32 }, + + #[fail(display = "thread {} is closed and can not be responded to", id)] + ThreadClosed { id: i32 }, // This variant is used as a catch-all for wrapping // actix-web-compatible response errors, such as the errors it @@ -119,7 +122,12 @@ impl From for ConverseError { impl ResponseError for ConverseError { fn error_response(&self) -> HttpResponse { // Everything is mapped to internal server errors for now. - HttpResponse::build(StatusCode::INTERNAL_SERVER_ERROR) - .body(format!("An error occured: {}", self)) + match *self { + ConverseError::ThreadClosed { id } => HttpResponse::SeeOther() + .header("Location", format!("/thread/{}#edit-post", id)) + .finish(), + _ => HttpResponse::build(StatusCode::INTERNAL_SERVER_ERROR) + .body(format!("An error occured: {}", self)) + } } } diff --git a/src/handlers.rs b/src/handlers.rs index e5f21849f..1c3d020e7 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -207,7 +207,10 @@ pub fn edit_form(state: State, return Ok(post); } - Err(ConverseError::PostEditForbidden { id: post.id }) + Err(ConverseError::PostEditForbidden { + user: user_id, + id: post.id, + }) }) .and_then(move |post| { let edit_msg = EditPostPage { @@ -236,7 +239,10 @@ pub fn edit_post(state: State, if user_id != 1 && post.user_id == user_id { Ok(()) } else { - Err(ConverseError::PostEditForbidden { id: post.id }) + Err(ConverseError::PostEditForbidden { + user: user_id, + id: post.id, + }) } }) .and_then(move |_| state.db.send(update.0).from_err())