Commit 2fe4d9b4 authored by Michael Rose's avatar Michael Rose

search: better error handling, validation of sorting

parent 1a54114f
......@@ -33,8 +33,9 @@ class SearchController < ApplicationController
unless @search.valid?
@characters = Character.for_fandoms(@fandoms)
@fandom_facets = Fandom.fandom_facets(@fandoms)
@tags = Tag.all.order(:id)
render 'index'
return true
return
end
@search_results = @search.execute(@page, 25)
......@@ -46,9 +47,17 @@ class SearchController < ApplicationController
rescue Elasticsearch::Transport::Transport::Errors::BadRequest => e
structured_error = JSON.parse(e.message[6..-1])
client_error_type = structured_error['error']['caused_by']['caused_by']['type']
client_error_message = structured_error['error']['caused_by']['caused_by']['reason']
redirect_to(root_path(params.permit!), flash: { error: "Search failed: #{client_error_type}: #{client_error_message}"})
client_error_type = structured_error.dig('error', 'caused_by', 'caused_by', 'type')
client_error_message = structured_error.dig('error', 'caused_by', 'caused_by', 'reason')
error_string = 'Search failed: '
if client_error_type.nil?
error_string += 'Unknown error. :('
else
error_string += "#{client_error_type}: #{client_error_message}"
end
redirect_to(root_path(params.permit!), flash: { error: error_string})
return true
end
......
......@@ -11,7 +11,6 @@ class Search < ActiveRecord::Base
validates :character_required, :length => { :maximum => 4, :message => 'may have at most 4 characters' }
validates :status, inclusion: {in: Scryer::Constants::STATUS.values}
validates :order_by, inclusion: {in: Scryer::Constants::ORDERING.values}, presence: true
validates :order_by, inclusion: {in: Scryer::Constants::ORDERING.values}, presence: true
validates :category_optional_exclude, :character_optional_exclude, :inclusion => {:in => [true, false]}
validates_each :fandoms,
:crossovers,
......@@ -23,7 +22,8 @@ class Search < ActiveRecord::Base
record.errors.add(attr, "collection is not an all integers: #{value}")
end
end
validate :validate_ratings
validate :validate_sort_by
validate :validate_ratings
belongs_to :user
# has_many :tags_include, :class_name => 'Tag'
......@@ -131,6 +131,11 @@ private
errors.add(:rating, "contains unsupported values, found: #{diff}, allowed: #{Scryer::Constants::RATING.values}")
end
end
def validate_sort_by
unless Scryer::Constants::SORTING.values.include?(sort_by)
errors.add(:sort_by, "contains unsupported values, found: #{sort_by}, allowed: #{Scryer::Constants::SORTING.values}")
end
end
end
class SearchResults
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment