Commit 029ed1f6f59f9b4eb1165c6fd68a4a95704910ca
1 parent
483bfce2
Exists in
master
and in
1 other branch
[#227] Err creating now returns a XML with Err's ID (called "error code" by Airb…
…rake gem), so now Errbit is compatible with the user informer feature.
Showing
3 changed files
with
20 additions
and
4 deletions
Show diff stats
app/controllers/notices_controller.rb
| 1 | class NoticesController < ApplicationController | 1 | class NoticesController < ApplicationController |
| 2 | - respond_to :xml | ||
| 3 | - | ||
| 4 | skip_before_filter :authenticate_user!, :only => :create | 2 | skip_before_filter :authenticate_user!, :only => :create |
| 5 | 3 | ||
| 6 | def create | 4 | def create |
| 7 | # params[:data] if the notice came from a GET request, raw_post if it came via POST | 5 | # params[:data] if the notice came from a GET request, raw_post if it came via POST |
| 8 | @notice = App.report_error!(params[:data] || request.raw_post) | 6 | @notice = App.report_error!(params[:data] || request.raw_post) |
| 9 | - respond_with @notice | 7 | + render :xml => @notice |
| 10 | end | 8 | end |
| 11 | - | ||
| 12 | end | 9 | end |
| 13 | 10 |
app/models/notice.rb
| 1 | require 'hoptoad' | 1 | require 'hoptoad' |
| 2 | require 'recurse' | 2 | require 'recurse' |
| 3 | +require 'builder' | ||
| 3 | 4 | ||
| 4 | class Notice | 5 | class Notice |
| 5 | include Mongoid::Document | 6 | include Mongoid::Document |
| @@ -35,6 +36,15 @@ class Notice | @@ -35,6 +36,15 @@ class Notice | ||
| 35 | 36 | ||
| 36 | delegate :app, :problem, :to => :err | 37 | delegate :app, :problem, :to => :err |
| 37 | 38 | ||
| 39 | + def to_xml(options = {}) | ||
| 40 | + options[:indent] ||= 2 | ||
| 41 | + xml = options[:builder] ||= ::Builder::XmlMarkup.new(:indent => options[:indent]) | ||
| 42 | + xml.instruct! unless options[:skip_instruct] | ||
| 43 | + xml.notice do | ||
| 44 | + xml.id self.id | ||
| 45 | + end | ||
| 46 | + end | ||
| 47 | + | ||
| 38 | def user_agent | 48 | def user_agent |
| 39 | agent_string = env_vars['HTTP_USER_AGENT'] | 49 | agent_string = env_vars['HTTP_USER_AGENT'] |
| 40 | agent_string.blank? ? nil : UserAgent.parse(agent_string) | 50 | agent_string.blank? ? nil : UserAgent.parse(agent_string) |
spec/controllers/notices_controller_spec.rb
| @@ -17,16 +17,25 @@ describe NoticesController do | @@ -17,16 +17,25 @@ describe NoticesController do | ||
| 17 | App.should_receive(:report_error!).with(@xml).and_return(@notice) | 17 | App.should_receive(:report_error!).with(@xml).and_return(@notice) |
| 18 | request.should_receive(:raw_post).and_return(@xml) | 18 | request.should_receive(:raw_post).and_return(@xml) |
| 19 | post :create | 19 | post :create |
| 20 | + response.should be_success | ||
| 21 | + # Same RegExp from Airbrake::UserInformer#replacement (https://github.com/airbrake/airbrake/blob/master/lib/airbrake/user_informer.rb#L8) | ||
| 22 | + # Inspired by https://github.com/airbrake/airbrake/blob/master/test/sender_test.rb | ||
| 23 | + response.body.should match(%r{<id[^>]*>#{@notice.id}</id>}) | ||
| 20 | end | 24 | end |
| 21 | 25 | ||
| 22 | it "generates a notice from xml [GET]" do | 26 | it "generates a notice from xml [GET]" do |
| 23 | App.should_receive(:report_error!).with(@xml).and_return(@notice) | 27 | App.should_receive(:report_error!).with(@xml).and_return(@notice) |
| 24 | get :create, {:data => @xml} | 28 | get :create, {:data => @xml} |
| 29 | + response.should be_success | ||
| 30 | + response.body.should match(%r{<id[^>]*>#{@notice.id}</id>}) | ||
| 25 | end | 31 | end |
| 26 | 32 | ||
| 27 | it "sends a notification email" do | 33 | it "sends a notification email" do |
| 34 | + App.should_receive(:report_error!).with(@xml).and_return(@notice) | ||
| 28 | request.should_receive(:raw_post).and_return(@xml) | 35 | request.should_receive(:raw_post).and_return(@xml) |
| 29 | post :create | 36 | post :create |
| 37 | + response.should be_success | ||
| 38 | + response.body.should match(%r{<id[^>]*>#{@notice.id}</id>}) | ||
| 30 | email = ActionMailer::Base.deliveries.last | 39 | email = ActionMailer::Base.deliveries.last |
| 31 | email.to.should include(@app.watchers.first.email) | 40 | email.to.should include(@app.watchers.first.email) |
| 32 | email.subject.should include(@notice.message) | 41 | email.subject.should include(@notice.message) |