Skip to content

feature: informative development error when search misconfigured #3098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 9, 2024
Merged
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
21 changes: 13 additions & 8 deletions app/controllers/avo/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ class SearchController < ApplicationController
before_action :set_resource, only: :show

def show
render json: search_resources([resource])
if resource.search_query.blank?
render_error _label: "Please configure the search for #{resource}",
_url: "https://docs.avohq.io/3.0/search.html#enable-search-for-a-resource"
else
render json: search_resources([resource])
end
rescue => error
render_search_error(error)
render_error _label: error.message
end

private
Expand All @@ -25,8 +30,6 @@ def search_resources(resources)
policy_class: resource.authorization_policy,
raise_exception: false
)
# Filter out the models without a search_query
next if resource.search_query.nil?

search_resource resource
end
Expand Down Expand Up @@ -179,15 +182,17 @@ def fetch_parent
parent_resource.find_record params[:via_reflection_id], params: params
end

def render_search_error(error)
def render_error(_label:, _url: "")
raise error unless Rails.env.development?

render json: {
error: {
header: "🚨 An error occurred while searching. 🚨",
help: "Please see the error and fix it before deploying.",
header: "🚨 An error occurred during search 🚨",
help: "Please review and resolve the issue before deployment 🚨",
results: {
_label: error.message
_label:,
_url:,
_error: true
},
count: 1
}
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/js/controllers/search_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export default class extends Controller {
}

handleOnSelect({ item }) {
if (this.isBelongsToSearch) {
if (this.isBelongsToSearch && !item._error) {
this.updateFieldAttribute(this.hiddenIdTarget, 'value', item._id)
this.updateFieldAttribute(this.buttonTarget, 'value', this.removeHTMLTags(item._label))

Expand Down
Loading