From ff7532b3db202e69adda71a764175fe884a5eac5 Mon Sep 17 00:00:00 2001 From: rhymes Date: Tue, 14 Jan 2020 20:10:05 +0100 Subject: [PATCH] Handle liquid tag errors in comments form (#5485) [deploy] * Handle liquid tag errors in comments form * Do not hide Pundit::NotAuthorizedError errors --- .../initializers/initializeCommentsPage.js.erb | 8 +++++++- app/controllers/comments_controller.rb | 13 +++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/initializers/initializeCommentsPage.js.erb b/app/assets/javascripts/initializers/initializeCommentsPage.js.erb index 3c495438b..33db006aa 100644 --- a/app/assets/javascripts/initializers/initializeCommentsPage.js.erb +++ b/app/assets/javascripts/initializers/initializeCommentsPage.js.erb @@ -144,7 +144,7 @@ function initializeCommentsPage() { }; } if (document.getElementById('new_comment')) { - document.getElementById('new_comment').onsubmit = handleCommentSubmit; + document.getElementById('new_comment').addEventListener('submit', handleCommentSubmit); } } listenForDetailsToggle(); @@ -255,6 +255,12 @@ function handleCommentSubmit(event) { initializeCommentDate(); initializeCommentDropdown(); }) + } else { + response.json().then(function parseError(errorReponse) { + form.classList.remove('submitting'); + window.alert(errorReponse.error); // eslint-disable-line no-alert + return false; + }); } return false; }); diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index be33bfb41..8b57b398c 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -59,10 +59,13 @@ class CommentsController < ApplicationController authorize @comment if @comment.save - current_user.update(checked_code_of_conduct: true) if params[:checked_code_of_conduct].present? && !current_user.checked_code_of_conduct + checked_code_of_conduct = params[:checked_code_of_conduct].present? && !current_user.checked_code_of_conduct + current_user.update(checked_code_of_conduct: true) if checked_code_of_conduct Mention.create_all(@comment) - NotificationSubscription.create(user: current_user, notifiable_id: @comment.id, notifiable_type: "Comment", config: "all_comments") + NotificationSubscription.create( + user: current_user, notifiable_id: @comment.id, notifiable_type: "Comment", config: "all_comments", + ) Notification.send_new_comment_notifications_without_delay(@comment) if @comment.invalid? @@ -99,6 +102,12 @@ class CommentsController < ApplicationController else render json: { status: "errors" } end + rescue Pundit::NotAuthorizedError + raise + rescue StandardError => e + Rails.logger.error(e) + message = "There was a error in your markdown: #{e}" + render json: { error: message }, status: :unprocessable_entity end # PATCH/PUT /comments/1