Log an error if the require fails while loading a plugin.

Fixes #6834

Fixes #8147
This commit is contained in:
Jordan Sissel 2017-09-05 14:44:45 -07:00
parent 9acc61e751
commit acc6d50ab8

View file

@ -9,6 +9,8 @@ module LogStash module Plugins
class Registry class Registry
include LogStash::Util::Loggable include LogStash::Util::Loggable
class UnknownPlugin < NameError; end
# Add a bit more sanity with when interacting with the rubygems' # Add a bit more sanity with when interacting with the rubygems'
# specifications database, most of out code interact directly with really low level # specifications database, most of out code interact directly with really low level
# components of bundler/rubygems we need to encapsulate that and this is a start. # components of bundler/rubygems we need to encapsulate that and this is a start.
@ -152,14 +154,19 @@ module LogStash module Plugins
begin begin
path = "logstash/#{type}s/#{plugin_name}" path = "logstash/#{type}s/#{plugin_name}"
begin klass = begin
require path namespace_lookup(type, plugin_name)
rescue LoadError rescue UnknownPlugin => e
# Plugin might be already defined in the current scope # Plugin not registered. Try to load it.
# This scenario often happen in test when we write an adhoc class begin
require path
namespace_lookup(type, plugin_name)
rescue LoadError => e
logger.error("Tried to load a plugin's code, but failed.", :exception => e, :path => path, :type => type, :name => plugin_name)
raise
end
end end
klass = namespace_lookup(type, plugin_name)
plugin = lazy_add(type, plugin_name, klass) plugin = lazy_add(type, plugin_name, klass)
rescue => e rescue => e
logger.error("Problems loading a plugin with", logger.error("Problems loading a plugin with",
@ -223,7 +230,7 @@ module LogStash module Plugins
klass_sym = namespace.constants.find { |c| is_a_plugin?(namespace.const_get(c), name) } klass_sym = namespace.constants.find { |c| is_a_plugin?(namespace.const_get(c), name) }
klass = klass_sym && namespace.const_get(klass_sym) klass = klass_sym && namespace.const_get(klass_sym)
raise(NameError) unless klass raise(UnknownPlugin) unless klass
klass klass
end end