Skip to content
Browse files

Log payload parsing errors

[Finishes #34668605]
  • Loading branch information...
1 parent b246ccc commit 0b65ad10e3e09bb65610270c0b6b65b850a88e83 Nicholas Greenfield and Vanessa Sant'Anna committed Oct 18, 2012
View
25 lib/payload.rb
@@ -1,5 +1,8 @@
class Payload
+ class InvalidContentException < ::Exception
+ end
+
attr_writer :dependent_content
attr_accessor :parsed_url, :error_text, :error_type, :backtrace, :remote_addr
@@ -29,11 +32,20 @@ def webhook_status_content=(content)
end
def status_content=(content)
- @status_content = convert_content!(content).first(Project::RECENT_STATUS_COUNT)
+ begin
+ @status_content = convert_content!(content).first(Project::RECENT_STATUS_COUNT)
+ rescue InvalidContentException => e
+ log_error e
+ @status_content = []
+ end
end
def build_status_content=(content)
- @build_status_content = convert_build_content!(content)
+ begin
+ @build_status_content = convert_build_content!(content)
+ rescue InvalidContentException => e
+ log_error e
+ end
end
def status_is_processable?
@@ -65,7 +77,11 @@ def convert_content!(content)
end
def convert_webhook_content!(content)
- convert_content!(content)
+ begin
+ convert_content!(content)
+ rescue InvalidContentException => e
+ log_error e
+ end
end
def convert_build_content!(content)
@@ -80,4 +96,7 @@ def log_error(e)
attr_accessor :processable, :build_processable
attr_reader :status_content, :build_status_content, :dependent_content
+
end
+
+
View
9 lib/payload/cruise_control_xml_payload.rb
@@ -14,11 +14,15 @@ def building?
private
def convert_content!(content)
- [Nokogiri::XML.parse(content.downcase)]
+ parsed_xml = Nokogiri::XML.parse(content.downcase)
+ raise Payload::InvalidContentException, "Error converting content for project #{@project_name}" unless parsed_xml.root
+ [parsed_xml]
end
def convert_build_content!(content)
- Nokogiri::XML.parse(content.downcase)
+ parsed_xml = Nokogiri::XML.parse(content.downcase)
+ raise Payload::InvalidContentException, "Error converting content for project #{@project_name}" unless parsed_xml.root
+ parsed_xml
end
def parse_success(content)
@@ -45,5 +49,4 @@ def parse_published_at(content)
(pub_date == Time.at(0) ? Time.now : pub_date).localtime
end
end
-
end
View
3 lib/payload/jenkins_json_payload.rb
@@ -6,9 +6,8 @@ def building?
def convert_content!(content)
[JSON.parse(content)]
rescue => e
- log_error(e)
self.processable = self.build_processable = false
- []
+ raise Payload::InvalidContentException, e.message
end
def parse_success(content)
View
20 lib/payload/jenkins_xml_payload.rb
@@ -14,19 +14,21 @@ def building?
private
def convert_content!(content)
- if content
- Nokogiri::XML.parse(content.downcase).css('feed entry').to_a
- else
- self.processable = false
- []
- end
+ parsed_content = parse_content(content.downcase)
+ parsed_content.css('feed entry').to_a
end
def convert_build_content!(content)
- if content
- Nokogiri::XML.parse(content.downcase)
+ parse_content(content)
+ end
+
+ def parse_content(content)
+ parsed_content = Nokogiri::XML.parse(content.downcase)
+ if parsed_content.root
+ return parsed_content
else
- self.build_processable = false
+ raise Payload::InvalidContentException, "Error parsing content for build name #{@build_name}"
+ self.processable = false
end
end
View
8 lib/payload/legacy_team_city_xml_payload.rb
@@ -8,11 +8,15 @@ def building?
private
def convert_content!(content)
- Nokogiri::XML.parse(content).css('Build').to_a
+ parsed_content = Nokogiri::XML.parse(content)
+ raise Payload::InvalidContentException, "Error converting content" unless parsed_content.root
+ parsed_content.css('Build').to_a
end
def convert_build_content!(content)
- Nokogiri::XML.parse(content).css('Build')
+ parsed_content = Nokogiri::XML.parse(content)
+ raise Payload::InvalidContentException, "Error converting content for project #{@project_name}" unless parsed_content.root
+ parsed_content.css('Build')
end
def parse_success(content)
View
3 lib/payload/semaphore_payload.rb
@@ -7,9 +7,8 @@ def building?
def convert_content!(content)
[JSON.parse(content)]
rescue => e
- log_error(e)
self.processable = self.build_processable = false
- []
+ raise Payload::InvalidContentException, e.message
end
def parse_success(content)
View
3 lib/payload/tddium_payload.rb
@@ -21,9 +21,8 @@ def convert_content!(content)
[]
end
rescue => e
- log_error(e)
self.processable = self.build_processable = false
- []
+ raise Payload::InvalidContentException, e.message
end
def convert_build_content!(content)
View
3 lib/payload/team_city_json_payload.rb
@@ -7,9 +7,8 @@ def building?
def convert_content!(content)
[JSON.parse(content)["build"]]
rescue => e
- log_error(e)
self.processable = self.build_processable = false
- []
+ raise Payload::InvalidContentException, e.message
end
def parse_success(content)
View
4 lib/payload/team_city_xml_payload.rb
@@ -22,7 +22,9 @@ def each_child(project)
private
def convert_content!(content)
- Nokogiri::XML.parse(content).css('build').to_a
+ parsed_content = Nokogiri::XML.parse(content)
+ raise Payload::InvalidContentException, "Error converting content for project #{@project_name}" unless parsed_content.root
+ parsed_content.css('build').to_a
end
def parse_success(content)
View
3 lib/payload/travis_json_payload.rb
@@ -14,9 +14,8 @@ def convert_webhook_content!(content)
def convert_content!(content)
Array.wrap(JSON.parse(content))
rescue => e
- log_error(e)
self.processable = self.build_processable = false
- []
+ raise Payload::InvalidContentException, e.message
end
def parse_success(content)
View
54 spec/lib/payload/cruise_control_xml_payload_spec.rb
@@ -52,8 +52,6 @@
project.statuses.should == statuses
end
end
-
-
end
describe "building status" do
@@ -76,33 +74,51 @@
let(:example) { CCRssExample.new(xml) }
let(:status_content) { example.read }
let(:payload) { CruiseControlXmlPayload.new(project.name) }
- before { payload.status_content = status_content }
- describe "when build was successful" do
- let(:xml) { "success.rss" }
+ context "successful parsing" do
+ before { payload.status_content = status_content }
+ describe "when build was successful" do
+ let(:xml) { "success.rss" }
- its(:latest_status) { should be_success }
+ its(:latest_status) { should be_success }
- it "return the link to the checkin" do
- subject.latest_status.url.should == CCRssExample.new("success.rss").xpath_content("/rss/channel/item/link")
- end
+ it "return the link to the checkin" do
+ subject.latest_status.url.should == CCRssExample.new("success.rss").xpath_content("/rss/channel/item/link")
+ end
- it "should return the published date of the checkin" do
- subject.latest_status.published_at.should == Time.parse(CCRssExample.new("success.rss").xpath_content("/rss/channel/item/pubDate"))
+ it "should return the published date of the checkin" do
+ subject.latest_status.published_at.should == Time.parse(CCRssExample.new("success.rss").xpath_content("/rss/channel/item/pubDate"))
+ end
end
- end
- describe "when build failed" do
- let(:xml) { "failure.rss" }
+ describe "when build failed" do
+ let(:xml) { "failure.rss" }
+
+ its(:latest_status) { should_not be_success }
- its(:latest_status) { should_not be_success }
+ it "return the link to the checkin" do
+ subject.latest_status.url.should == CCRssExample.new("failure.rss").xpath_content("/rss/channel/item/link")
+ end
- it "return the link to the checkin" do
- subject.latest_status.url.should == CCRssExample.new("failure.rss").xpath_content("/rss/channel/item/link")
+ it "should return the published date of the checkin" do
+ subject.latest_status.published_at.should == Time.parse(CCRssExample.new("failure.rss").xpath_content("/rss/channel/item/pubDate"))
+ end
end
+ end
- it "should return the published date of the checkin" do
- subject.latest_status.published_at.should == Time.parse(CCRssExample.new("failure.rss").xpath_content("/rss/channel/item/pubDate"))
+ context "bad XML data" do
+ let(:wrong_status_content) { "some non xml content" }
+ describe "#status_content" do
+ it "should log errors" do
+ payload.should_receive("log_error")
+ payload.status_content = wrong_status_content
+ end
+ end
+ describe "#build_status_content" do
+ it "should log errors" do
+ payload.should_receive("log_error")
+ payload.build_status_content = wrong_status_content
+ end
end
end
end
View
9 spec/lib/payload/jenkins_json_payload_spec.rb
@@ -7,16 +7,19 @@
let(:converted_content) { payload.convert_content!(status_content).first }
let(:example_file) { "success.txt" }
- describe '#convert_content!' do
- subject { payload.convert_content!(status_content) }
+ describe '#status_content' do
+ subject { payload.status_content = status_content }
context 'when content is valid' do
let(:expected_content) { double }
before do
JSON.stub(:parse).and_return(expected_content)
end
- it { should == [expected_content] }
+ it 'should parse content' do
+ subject
+ payload.status_content.should == [expected_content]
+ end
end
context 'when content is corrupt / badly encoded' do
View
17 spec/lib/payload/jenkins_xml_payload_spec.rb
@@ -104,6 +104,15 @@
end
end
+ describe "#build_status_content" do
+ let(:wrong_status_content) { "some non xml content" }
+ context "invalid xml" do
+ it 'should log error message' do
+ jenkins_payload.should_receive("log_error")
+ jenkins_payload.build_status_content = wrong_status_content
+ end
+ end
+ end
describe "with invalid xml" do
let(:status_content) { "<foo><bar>baz</bar></foo>" }
@@ -112,5 +121,13 @@
it "should not create a status" do
expect { subject }.not_to change(ProjectStatus, :count)
end
+
+ context "bad XML data" do
+ let(:wrong_status_content) { "some non xml content" }
+ it "should log errors" do
+ jenkins_payload.should_receive("log_error")
+ jenkins_payload.status_content = wrong_status_content
+ end
+ end
end
end
View
18 spec/lib/payload/legacy_team_city_xml_payload_spec.rb
@@ -107,6 +107,16 @@
PayloadProcessor.new(project,payload).process
project.should_not be_building
end
+
+ describe "#build_status_content" do
+ let(:wrong_status_content) { "some non xml content" }
+ context "invalid xml" do
+ it 'should log error message' do
+ payload.should_receive("log_error")
+ payload.build_status_content = wrong_status_content
+ end
+ end
+ end
end
describe "with invalid xml" do
@@ -118,5 +128,13 @@
it "should not create a status" do
expect { subject }.not_to change(ProjectStatus, :count)
end
+
+ context "bad XML data" do
+ let(:wrong_status_content) { "some non xml content" }
+ it "should log errors" do
+ payload.should_receive("log_error")
+ payload.status_content = wrong_status_content
+ end
+ end
end
end
View
8 spec/lib/payload/semaphore_payload_spec.rb
@@ -27,6 +27,14 @@
it 'should be marked as unprocessable' do
payload.processable.should be_false
end
+
+ context "bad XML data" do
+ let(:wrong_status_content) { "some non xml content" }
+ it "should log errors" do
+ payload.should_receive("log_error")
+ payload.status_content = wrong_status_content
+ end
+ end
end
end
View
4 spec/lib/payload/tddium_payload_spec.rb
@@ -12,7 +12,7 @@
subject { payload.processable }
before do
payload.should_receive(:log_error)
- payload.convert_content!(content)
+ payload.status_content = content
end
context 'with empty string' do
@@ -68,7 +68,7 @@
expect do
subject
end.to raise_error NotImplementedError
- end
+ end
end
View
11 spec/lib/payload/team_city_json_payload_spec.rb
@@ -25,13 +25,22 @@
end
it 'should be marked as unprocessable' do
- subject
+ lambda {subject}.should raise_error Payload::InvalidContentException
payload.processable.should be_false
payload.build_processable.should be_false
end
end
end
+ describe "#status_content" do
+ context "invalid JSON" do
+ it "should log erros" do
+ payload.should_receive(:log_error)
+ payload.status_content = "non json content"
+ end
+ end
+ end
+
describe '#building?' do
subject do
View
8 spec/lib/payload/team_city_xml_payload_spec.rb
@@ -30,6 +30,14 @@
let(:status) { 'FAILURE' }
it { should be_red }
end
+
+ context "with bad XML data" do
+ let(:content) { "some non xml content" }
+ it "should log erros" do
+ payload.should_receive("log_error")
+ payload.status_content = content
+ end
+ end
end
context "when building" do
View
15 spec/lib/payload/travis_json_payload_spec.rb
@@ -7,16 +7,19 @@
let(:content) { payload.status_content.first }
let(:json) { "success.json" }
- describe '#convert_content!' do
- subject { payload.convert_content!(status_content) }
+ describe '#status_content' do
+ subject { payload.status_content = status_content }
context 'when content is valid' do
let(:expected_content) { double }
before do
JSON.stub(:parse).and_return(expected_content)
end
- it{ should == [expected_content] }
+ it 'should parse content' do
+ subject
+ payload.status_content.should == [expected_content]
+ end
end
context 'when content is corrupt / badly encoded' do
@@ -28,6 +31,12 @@
payload.processable.should be_false
payload.build_processable.should be_false
end
+
+ let(:wrong_status_content) { "some non xml content" }
+ it "should log errors" do
+ payload.should_receive("log_error")
+ payload.status_content = wrong_status_content
+ end
end
end

0 comments on commit 0b65ad1

Please sign in to comment.
Something went wrong with that request. Please try again.