From 6ff02c653d05e7e7afa1cf51b1e5885827da5de9 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Tue, 21 Apr 2015 18:39:42 -0700 Subject: [PATCH] dmail bulk update request errors to admin --- app/helpers/bulk_update_requests_helper.rb | 10 ++++---- app/logical/alias_and_implication_importer.rb | 2 +- app/models/bulk_update_request.rb | 23 +++++++++++++++++++ test/unit/bulk_update_request_test.rb | 11 +++++++++ 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/app/helpers/bulk_update_requests_helper.rb b/app/helpers/bulk_update_requests_helper.rb index 9f47ce7b9..e03750b78 100644 --- a/app/helpers/bulk_update_requests_helper.rb +++ b/app/helpers/bulk_update_requests_helper.rb @@ -6,28 +6,28 @@ module BulkUpdateRequestsHelper arg1_count = Tag.find_by_name(arg1).try(:post_count).to_i arg2_count = Tag.find_by_name(arg2).try(:post_count).to_i - "create_alias " + link_to(arg1, posts_path(:tags => arg1)) + " (#{arg1_count}) -> " + link_to(arg2, posts_path(:tags => arg2)) + " (#{arg2_count})" + "create alias " + link_to(arg1, posts_path(:tags => arg1)) + " (#{arg1_count}) -> " + link_to(arg2, posts_path(:tags => arg2)) + " (#{arg2_count})" when :create_implication arg1_count = Tag.find_by_name(arg1).try(:post_count).to_i arg2_count = Tag.find_by_name(arg2).try(:post_count).to_i - "create_implication " + link_to(arg1, posts_path(:tags => arg1)) + " (#{arg1_count}) -> " + link_to(arg2, posts_path(:tags => arg2)) + " (#{arg2_count})" + "create implication " + link_to(arg1, posts_path(:tags => arg1)) + " (#{arg1_count}) -> " + link_to(arg2, posts_path(:tags => arg2)) + " (#{arg2_count})" when :remove_alias arg1_count = Tag.find_by_name(arg1).try(:post_count).to_i arg2_count = Tag.find_by_name(arg2).try(:post_count).to_i - "remove_alias " + link_to(arg1, posts_path(:tags => arg1)) + " (#{arg1_count}) -> " + link_to(arg2, posts_path(:tags => arg2)) + " (#{arg2_count})" + "remove alias " + link_to(arg1, posts_path(:tags => arg1)) + " (#{arg1_count}) -> " + link_to(arg2, posts_path(:tags => arg2)) + " (#{arg2_count})" when :remove_implication arg1_count = Tag.find_by_name(arg1).try(:post_count).to_i arg2_count = Tag.find_by_name(arg2).try(:post_count).to_i - "remove_implication " + link_to(arg1, posts_path(:tags => arg1)) + " (#{arg1_count}) -> " + link_to(arg2, posts_path(:tags => arg2)) + " (#{arg2_count})" + "remove implication " + link_to(arg1, posts_path(:tags => arg1)) + " (#{arg1_count}) -> " + link_to(arg2, posts_path(:tags => arg2)) + " (#{arg2_count})" when :mass_update - "mass_update " + link_to(arg1, posts_path(:tags => arg1)) + " -> " + link_to(arg2, posts_path(:tags => arg2)) + "mass update " + link_to(arg1, posts_path(:tags => arg1)) + " -> " + link_to(arg2, posts_path(:tags => arg2)) end end.join("\n") diff --git a/app/logical/alias_and_implication_importer.rb b/app/logical/alias_and_implication_importer.rb index ee5556dfe..17735b7b4 100644 --- a/app/logical/alias_and_implication_importer.rb +++ b/app/logical/alias_and_implication_importer.rb @@ -32,7 +32,7 @@ class AliasAndImplicationImporter [:remove_implication, $1, $2] elsif line =~ /^mass update (.+?) -> (.*)$/i [:mass_update, $1, $2] - elsif line.empty? + elsif line.strip.empty? # do nothing else raise "Unparseable line: #{line}" diff --git a/app/models/bulk_update_request.rb b/app/models/bulk_update_request.rb index 116704916..3d68f4c2e 100644 --- a/app/models/bulk_update_request.rb +++ b/app/models/bulk_update_request.rb @@ -35,6 +35,29 @@ class BulkUpdateRequest < ActiveRecord::Base AliasAndImplicationImporter.new(script, forum_topic_id, "1").process! update_forum_topic_for_approve update_attribute(:status, "approved") + + rescue Exception => x + admin = User.admins.first + msg = <<-EOS + Bulk Update Request ##{id} failed\n + Exception: #{x.class}\n + Message: #{x.to_s}\n + Stack trace:\n + EOS + + x.backtrace.each do |line| + msg += "#{line}\n" + end + + dmail = Dmail.new( + :from_id => admin.id, + :to_id => admin.id, + :owner_id => admin.id, + :title => "Bulk update request approval failed", + :body => msg + ) + dmail.owner_id = admin.id + dmail.save end def editable?(user) diff --git a/test/unit/bulk_update_request_test.rb b/test/unit/bulk_update_request_test.rb index 252ae5e8c..7f81ebc93 100644 --- a/test/unit/bulk_update_request_test.rb +++ b/test/unit/bulk_update_request_test.rb @@ -23,6 +23,17 @@ class BulkUpdateRequestTest < ActiveSupport::TestCase @req = FactoryGirl.create(:bulk_update_request, :script => "create alias AAA -> BBB", :forum_topic => @topic) end + should "handle errors gracefully" do + @req.stubs(:update_forum_topic_for_approve).raises(RuntimeError.new("blah")) + assert_difference("Dmail.count", 1) do + CurrentUser.scoped(@admin, "127.0.0.1") do + @req.approve! + end + end + assert_match(/Exception: RuntimeError/, Dmail.last.body) + assert_match(/Message: blah/, Dmail.last.body) + end + should "downcase the text" do assert_equal("create alias aaa -> bbb", @req.script) end