From f6b737103a854ff3958fb288eadbbe2d74019cca Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 13 Aug 2019 21:30:20 -0500 Subject: [PATCH] post votes: fix error handling. * Clean up javascript. * Return HTTP 422 instead of HTTP 500 on "you have already voted for this post" errors. * In json/xml error responses, return the error message in the `message` field, not `reason`. * In json/xml success responses, return the post itself instead of a plain `{ success: true }` object. --- app/controllers/post_votes_controller.rb | 12 ++++++------ app/views/post_votes/create.js.erb | 8 ++------ app/views/post_votes/create.json.erb | 5 ----- app/views/post_votes/create.xml.erb | 6 ------ app/views/post_votes/destroy.js.erb | 9 +++------ app/views/post_votes/destroy.json.erb | 5 ----- app/views/post_votes/destroy.xml.erb | 6 ------ 7 files changed, 11 insertions(+), 40 deletions(-) delete mode 100644 app/views/post_votes/create.json.erb delete mode 100644 app/views/post_votes/create.xml.erb delete mode 100644 app/views/post_votes/destroy.json.erb delete mode 100644 app/views/post_votes/destroy.xml.erb diff --git a/app/controllers/post_votes_controller.rb b/app/controllers/post_votes_controller.rb index b90577460..0331c60ee 100644 --- a/app/controllers/post_votes_controller.rb +++ b/app/controllers/post_votes_controller.rb @@ -1,20 +1,20 @@ class PostVotesController < ApplicationController before_action :voter_only skip_before_action :api_check + respond_to :js, :json, :xml + rescue_with PostVote::Error, status: 422 def create @post = Post.find(params[:post_id]) @post.vote!(params[:score]) - rescue PostVote::Error, ActiveRecord::RecordInvalid => x - @error = x - render status: 500 + + respond_with(@post) end def destroy @post = Post.find(params[:post_id]) @post.unvote! - rescue PostVote::Error => x - @error = x - render status: 500 + + respond_with(@post) end end diff --git a/app/views/post_votes/create.js.erb b/app/views/post_votes/create.js.erb index 3deae6cd3..92bdeec28 100644 --- a/app/views/post_votes/create.js.erb +++ b/app/views/post_votes/create.js.erb @@ -1,9 +1,5 @@ -<% if @error %> - $(window).trigger("danbooru:notice", "<%= j @error.to_s %>"); -<% else %> - $(window).trigger("danbooru:notice", "Vote saved"); - $("#score-for-post-<%= @post.id %>").html(<%= @post.score %>); -<% end %> +Danbooru.Utility.notice("Vote saved"); +$("#score-for-post-<%= @post.id %>").html(<%= @post.score %>); $("#vote-links-for-post-<%= @post.id %>").hide(); $("#unvote-link-for-post-<%= @post.id %>").show(); diff --git a/app/views/post_votes/create.json.erb b/app/views/post_votes/create.json.erb deleted file mode 100644 index 052990686..000000000 --- a/app/views/post_votes/create.json.erb +++ /dev/null @@ -1,5 +0,0 @@ -<% if @error %> - {"success": false, "reason": <%= @error.to_s.to_json.html_safe %>} -<% else %> - {"success": true} -<% end %> diff --git a/app/views/post_votes/create.xml.erb b/app/views/post_votes/create.xml.erb deleted file mode 100644 index 46666469e..000000000 --- a/app/views/post_votes/create.xml.erb +++ /dev/null @@ -1,6 +0,0 @@ - -<% if @error %> - <%= @error.to_s %> -<% else %> - -<% end %> diff --git a/app/views/post_votes/destroy.js.erb b/app/views/post_votes/destroy.js.erb index 0e5d07fa9..5466b6cfa 100644 --- a/app/views/post_votes/destroy.js.erb +++ b/app/views/post_votes/destroy.js.erb @@ -1,8 +1,5 @@ -<% if @error %> - $(window).trigger("danbooru:notice", "<%= j @error.to_s %>"); -<% else %> - $(window).trigger("danbooru:notice", "Unvoted successfully"); - $("#score-for-post-<%= @post.id %>").html(<%= @post.score %>); -<% end %> +Danbooru.Utility.notice("Unvoted successfully"); +$("#score-for-post-<%= @post.id %>").html(<%= @post.score %>); + $("#vote-links-for-post-<%= @post.id %>").show(); $("#unvote-link-for-post-<%= @post.id %>").hide(); diff --git a/app/views/post_votes/destroy.json.erb b/app/views/post_votes/destroy.json.erb deleted file mode 100644 index 052990686..000000000 --- a/app/views/post_votes/destroy.json.erb +++ /dev/null @@ -1,5 +0,0 @@ -<% if @error %> - {"success": false, "reason": <%= @error.to_s.to_json.html_safe %>} -<% else %> - {"success": true} -<% end %> diff --git a/app/views/post_votes/destroy.xml.erb b/app/views/post_votes/destroy.xml.erb deleted file mode 100644 index 46666469e..000000000 --- a/app/views/post_votes/destroy.xml.erb +++ /dev/null @@ -1,6 +0,0 @@ - -<% if @error %> - <%= @error.to_s %> -<% else %> - -<% end %>