Skip to content

Commit

Permalink
raise a more informative NoMethodError for missing custom queries
Browse files Browse the repository at this point in the history
the custom query container used to raise:

    NoMethodError: undefined method 'new' for nil:NilClass`

this error doesn't do much to help callers figure out where they've gone
awry. the updated message includes:

  - the originally called method name
  - the list of available methods to call (as a kind of 'did you mean' feature)
  - reference to the idea that queries need to be registered

hopefully these are all helpful reminders in dubugging.
  • Loading branch information
tamsin johnson committed Sep 20, 2021
1 parent 00e24c7 commit 9783137
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/valkyrie/persistence/custom_query_container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ def register_query_handler(query_handler)
end

def method_missing(meth_name, *args, &block)
query_handler = find_query_handler(meth_name).new(query_service: query_service)
handler_class =
find_query_handler(meth_name) ||
raise(NoMethodError, "Custom query #{meth_name} is not registered. The registered queries are: #{queries}")

query_handler = handler_class.new(query_service: query_service)
return super unless query_handler
query_handler.__send__(meth_name, *args, &block)
end
Expand All @@ -59,5 +63,11 @@ def find_query_handler(method)
def respond_to_missing?(meth_name, _args)
find_query_handler(meth_name).present?
end

private

def queries
query_handlers.map(&:queries).flatten
end
end
end

0 comments on commit 9783137

Please sign in to comment.