Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/controllers/autocomplete_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ def noncanonical_tag
body: { size: "100", query: { bool: { filter: [{ match: { tag_type: params[:type].capitalize } }, { match: { canonical: false } }], must: search_list } } }
)
render_output((match + search_results["hits"]["hits"].first(10).map { |t| t["_source"]["name"] }).uniq)
rescue Elastic::Transport::Transport::Errors::BadRequest
rescue Elastic::Transport::Transport::Errors::BadRequest => e
Sentry.capture_exception(e) if defined?(Sentry)
render_output(match)
end
end
Expand Down
3 changes: 2 additions & 1 deletion app/models/search/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def search
body: generated_query,
track_total_hits: true
)
rescue Elastic::Transport::Transport::Errors::BadRequest
rescue Elastic::Transport::Transport::Errors::BadRequest => e
Sentry.capture_exception(e) if defined?(Sentry)
{ error: "Your search failed because of a syntax error. Please try again." }
end
end
Expand Down
27 changes: 27 additions & 0 deletions spec/controllers/autocomplete_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,33 @@
describe AutocompleteController do
include LoginMacros

describe "GET #noncanonical_tag" do
context "when Elasticsearch raises a BadRequest error" do
before do
allow($elasticsearch).to receive(:search)
.and_raise(Elastic::Transport::Transport::Errors::BadRequest)
end

it "returns an empty result when there is no exact match" do
get :noncanonical_tag, params: { term: "test", type: "freeform", format: :json }
expect(JSON.parse(response.body)).to eq([])
end

it "falls back to the exact database match" do
create(:freeform, name: "test", canonical: false)
get :noncanonical_tag, params: { term: "test", type: "freeform", format: :json }
expect(JSON.parse(response.body)).to eq([{ "id" => "test", "name" => "test" }])
end

it "reports the exception to Sentry" do
sentry = class_double("Sentry", capture_exception: nil).as_stubbed_const
get :noncanonical_tag, params: { term: "test", type: "freeform", format: :json }
expect(sentry).to have_received(:capture_exception)
.with(instance_of(Elastic::Transport::Transport::Errors::BadRequest))
end
end
end

describe "tag" do
let!(:tag1) { create(:canonical_fandom, name: "Match") }
let!(:tag2) { create(:canonical_fandom, name: "Blargh") }
Expand Down
21 changes: 21 additions & 0 deletions spec/models/search/query_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
require 'spec_helper'

describe Query do
describe "#search" do
context "when Elasticsearch raises a BadRequest error" do
before do
allow($elasticsearch).to receive(:search)
.and_raise(Elastic::Transport::Transport::Errors::BadRequest)
end

it "returns an error hash" do
result = Query.new.search
expect(result).to eq(error: "Your search failed because of a syntax error. Please try again.")
end

it "reports the exception to Sentry" do
sentry = class_double("Sentry", capture_exception: nil).as_stubbed_const
Query.new.search
expect(sentry).to have_received(:capture_exception)
.with(instance_of(Elastic::Transport::Transport::Errors::BadRequest))
end
end
end

describe '#split_query_text_phrases' do
it "should add quoted phrases to a query string" do
q = Query.new
Expand Down
Loading