Commit 5145c399fabec1d70cf03338681a848b5e2cc8ac

Authored by Stephen Crosby
1 parent 9feadde1
Exists in master

v3 API allow notifier in the context hash

According to @zefer, the notifier can be passed in the context hash as
well as the top level when creating a notice through the JSON api.

https://github.com/airbrake/airbrake-js/pull/174#issuecomment-164918928
@@ -249,10 +249,6 @@ Airbrake.setProject("ERRBIT API KEY", "ERRBIT API KEY"); @@ -249,10 +249,6 @@ Airbrake.setProject("ERRBIT API KEY", "ERRBIT API KEY");
249 Airbrake.setHost("http://errbit.yourdomain.com"); 249 Airbrake.setHost("http://errbit.yourdomain.com");
250 ``` 250 ```
251 251
252 -NOTE: Errbit is known to work with airbrake-js v0.5.2. Newer versions of  
253 -will not work until we figure out how to deal with:  
254 -https://github.com/airbrake/airbrake-js/pull/174#issuecomment-164918928  
255 -  
256 Plugins and Integrations 252 Plugins and Integrations
257 ------------------------ 253 ------------------------
258 You can extend Errbit by adding Ruby gems and plugins which are typically gems. 254 You can extend Errbit by adding Ruby gems and plugins which are typically gems.
lib/airbrake_api/v3/notice_parser.rb
@@ -9,18 +9,20 @@ module AirbrakeApi @@ -9,18 +9,20 @@ module AirbrakeApi
9 @params = params || {} 9 @params = params || {}
10 end 10 end
11 11
12 - def report  
13 - attributes = { 12 + def attributes
  13 + {
14 error_class: error['type'], 14 error_class: error['type'],
15 message: error['message'], 15 message: error['message'],
16 backtrace: backtrace, 16 backtrace: backtrace,
17 request: request, 17 request: request,
18 server_environment: server_environment, 18 server_environment: server_environment,
19 api_key: params['key'].present? ? params['key'] : params['project_id'], 19 api_key: params['key'].present? ? params['key'] : params['project_id'],
20 - notifier: params['notifier'], 20 + notifier: context['notifier'] || params['notifier'],
21 user_attributes: user_attributes 21 user_attributes: user_attributes
22 } 22 }
  23 + end
23 24
  25 + def report
24 ErrorReport.new(attributes) 26 ErrorReport.new(attributes)
25 end 27 end
26 28
spec/lib/airbrake_api/v3/notice_parser_spec.rb
1 describe AirbrakeApi::V3::NoticeParser do 1 describe AirbrakeApi::V3::NoticeParser do
2 let(:app) { Fabricate(:app) } 2 let(:app) { Fabricate(:app) }
  3 + let(:notifier_params) do
  4 + {
  5 + 'name' => 'notifiername',
  6 + 'version' => 'notifierversion',
  7 + 'url' => 'notifierurl'
  8 + }
  9 + end
3 10
4 it 'raises error when errors attribute is missing' do 11 it 'raises error when errors attribute is missing' do
5 expect do 12 expect do
6 - AirbrakeApi::V3::NoticeParser.new({}).report 13 + described_class.new({}).report
7 end.to raise_error(AirbrakeApi::ParamsError) 14 end.to raise_error(AirbrakeApi::ParamsError)
8 15
9 expect do 16 expect do
10 - AirbrakeApi::V3::NoticeParser.new('errors' => []).report 17 + described_class.new('errors' => []).report
11 end.to raise_error(AirbrakeApi::ParamsError) 18 end.to raise_error(AirbrakeApi::ParamsError)
12 end 19 end
13 20
14 it 'parses JSON payload and returns ErrorReport' do 21 it 'parses JSON payload and returns ErrorReport' do
15 params = build_params(key: app.api_key) 22 params = build_params(key: app.api_key)
16 23
17 - report = AirbrakeApi::V3::NoticeParser.new(params).report 24 + report = described_class.new(params).report
18 notice = report.generate_notice! 25 notice = report.generate_notice!
19 26
20 expect(report.error_class).to eq('Error') 27 expect(report.error_class).to eq('Error')
@@ -37,7 +44,7 @@ describe AirbrakeApi::V3::NoticeParser do @@ -37,7 +44,7 @@ describe AirbrakeApi::V3::NoticeParser do
37 it 'parses JSON payload when api_key is missing but project_id is present' do 44 it 'parses JSON payload when api_key is missing but project_id is present' do
38 params = build_params(key: nil, project_id: app.api_key) 45 params = build_params(key: nil, project_id: app.api_key)
39 46
40 - report = AirbrakeApi::V3::NoticeParser.new(params).report 47 + report = described_class.new(params).report
41 expect(report).to be_valid 48 expect(report).to be_valid
42 end 49 end
43 50
@@ -46,7 +53,7 @@ describe AirbrakeApi::V3::NoticeParser do @@ -46,7 +53,7 @@ describe AirbrakeApi::V3::NoticeParser do
46 params = JSON.parse(json) 53 params = JSON.parse(json)
47 params['key'] = app.api_key 54 params['key'] = app.api_key
48 55
49 - report = AirbrakeApi::V3::NoticeParser.new(params).report 56 + report = described_class.new(params).report
50 report.generate_notice! 57 report.generate_notice!
51 58
52 expect(report.error_class).to eq('Error') 59 expect(report.error_class).to eq('Error')
@@ -54,6 +61,22 @@ describe AirbrakeApi::V3::NoticeParser do @@ -54,6 +61,22 @@ describe AirbrakeApi::V3::NoticeParser do
54 expect(report.backtrace.lines.size).to eq(0) 61 expect(report.backtrace.lines.size).to eq(0)
55 end 62 end
56 63
  64 + it 'takes the notifier from root' do
  65 + parser = described_class.new(
  66 + 'errors' => ['MyError'],
  67 + 'notifier' => notifier_params,
  68 + 'environment' => {})
  69 + expect(parser.attributes[:notifier]).to eq(notifier_params)
  70 + end
  71 +
  72 + it 'takes the notifier from the context' do
  73 + parser = described_class.new(
  74 + 'errors' => ['MyError'],
  75 + 'context' => { 'notifier' => notifier_params },
  76 + 'environment' => {})
  77 + expect(parser.attributes[:notifier]).to eq(notifier_params)
  78 + end
  79 +
57 def build_params(options = {}) 80 def build_params(options = {})
58 json = Rails.root.join('spec', 'fixtures', 'api_v3_request.json').read 81 json = Rails.root.join('spec', 'fixtures', 'api_v3_request.json').read
59 data = JSON.parse(json) 82 data = JSON.parse(json)