diff --git a/app/controllers/counts_controller.rb b/app/controllers/counts_controller.rb index 9f5b16285..175983eed 100644 --- a/app/controllers/counts_controller.rb +++ b/app/controllers/counts_controller.rb @@ -1,7 +1,8 @@ class CountsController < ApplicationController respond_to :xml, :json + rescue_from Post::TimeoutError, with: :rescue_exception def posts - @count = Post.fast_count(params[:tags]) + @count = Post.fast_count(params[:tags], raise_on_timeout: true) end end diff --git a/app/models/post.rb b/app/models/post.rb index 1b922b314..195fbc99f 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -7,6 +7,7 @@ class Post < ApplicationRecord class RevertError < Exception ; end class SearchError < Exception ; end class DeletionError < Exception ; end + class TimeoutError < Exception ; end # Tags to copy when copying notes. NOTE_COPY_TAGS = %w[translated partially_translated check_translation translation_request reverse_translation] @@ -1202,6 +1203,10 @@ class Post < ApplicationRecord if count.nil? # give up + if options[:raise_on_timeout] + raise TimeoutError.new("timed out") + end + count = Danbooru.config.blank_tag_search_fast_count else set_count_in_cache(tags, count) diff --git a/test/functional/counts_controller_test.rb b/test/functional/counts_controller_test.rb index 24c6dad03..323339eb6 100644 --- a/test/functional/counts_controller_test.rb +++ b/test/functional/counts_controller_test.rb @@ -7,6 +7,12 @@ class CountsControllerTest < ActionDispatch::IntegrationTest get posts_counts_path assert_response :success end + + should "render an error during a timeout" do + Post.stubs(:fast_count).raises(Post::TimeoutError.new) + get posts_counts_path + assert_response :error + end end end end