mirror of
https://github.com/elastic/logstash.git
synced 2025-04-23 22:27:21 -04:00
add Event method_missing to give better error messages for #[] and #[]=
Change strings and regexs to CONSTANTS. move method missing code to event.rb doh - put Event class in already defined Logstash module Fixes #6045
This commit is contained in:
parent
37243d1602
commit
8cec4fa514
3 changed files with 37 additions and 4 deletions
|
@ -25,4 +25,4 @@ end
|
|||
require "jruby_event_ext"
|
||||
require "jruby_timestamp_ext"
|
||||
require "logstash/event"
|
||||
require "logstash/timestamp"
|
||||
require "logstash/timestamp"
|
||||
|
|
|
@ -29,9 +29,26 @@ module LogStash
|
|||
|
||||
# LogStash::SHUTDOWN is used by plugins
|
||||
SHUTDOWN = ShutdownEvent.new
|
||||
|
||||
class Event
|
||||
MSG_BRACKETS_METHOD_MISSING = "Direct event field references (i.e. event['field']) have been disabled in favor of using event get and set methods (e.g. event.get('field')). Please consult the Logstash 5.0 breaking changes documentation for more details.".freeze
|
||||
MSG_BRACKETS_EQUALS_METHOD_MISSING = "Direct event field references (i.e. event['field'] = 'value') have been disabled in favor of using event get and set methods (e.g. event.set('field', 'value')). Please consult the Logstash 5.0 breaking changes documentation for more details.".freeze
|
||||
RE_BRACKETS_METHOD = /^\[\]$/.freeze
|
||||
RE_BRACKETS_EQUALS_METHOD = /^\[\]=$/.freeze
|
||||
|
||||
def method_missing(method_name, *arguments, &block)
|
||||
if RE_BRACKETS_METHOD.match(method_name.to_s)
|
||||
raise NoMethodError.new(MSG_BRACKETS_METHOD_MISSING)
|
||||
end
|
||||
if RE_BRACKETS_EQUALS_METHOD.match(method_name.to_s)
|
||||
raise NoMethodError.new(MSG_BRACKETS_EQUALS_METHOD_MISSING)
|
||||
end
|
||||
super
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# for backward compatibility, require "logstash/event" is used a lots of places so let's bootstrap the
|
||||
# Java code loading from here.
|
||||
# TODO: (colin) I think we should mass replace require "logstash/event" with require "logstash-core-event"
|
||||
require "logstash-core-event"
|
||||
require "logstash-core-event"
|
||||
|
|
|
@ -35,7 +35,7 @@ describe LogStash::Event do
|
|||
end
|
||||
end
|
||||
|
||||
context "[]" do
|
||||
context "#get" do
|
||||
it "should get simple values" do
|
||||
e = LogStash::Event.new({"foo" => "bar", "bar" => 1, "baz" => 1.0, TIMESTAMP => "2015-05-28T23:02:05.350Z"})
|
||||
expect(e.get("foo")).to eq("bar")
|
||||
|
@ -63,7 +63,7 @@ describe LogStash::Event do
|
|||
end
|
||||
end
|
||||
|
||||
context "[]=" do
|
||||
context "#set" do
|
||||
it "should set simple values" do
|
||||
e = LogStash::Event.new()
|
||||
expect(e.set("foo", "bar")).to eq("bar")
|
||||
|
@ -306,4 +306,20 @@ describe LogStash::Event do
|
|||
end
|
||||
|
||||
end
|
||||
|
||||
context "method missing exception messages" do
|
||||
subject { LogStash::Event.new({"foo" => "bar"}) }
|
||||
|
||||
it "#[] method raises a better exception message" do
|
||||
expect { subject["foo"] }.to raise_error(NoMethodError, /Direct event field references \(i\.e\. event\['field'\]\)/)
|
||||
end
|
||||
|
||||
it "#[]= method raises a better exception message" do
|
||||
expect { subject["foo"] = "baz" }.to raise_error(NoMethodError, /Direct event field references \(i\.e\. event\['field'\] = 'value'\)/)
|
||||
end
|
||||
|
||||
it "other missing method raises normal exception message" do
|
||||
expect { subject.baz() }.to raise_error(NoMethodError, /undefined method `baz' for/)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue