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
README.md
... ... @@ -249,10 +249,6 @@ Airbrake.setProject("ERRBIT API KEY", "ERRBIT API KEY");
249 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 252 Plugins and Integrations
257 253 ------------------------
258 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 9 @params = params || {}
10 10 end
11 11  
12   - def report
13   - attributes = {
  12 + def attributes
  13 + {
14 14 error_class: error['type'],
15 15 message: error['message'],
16 16 backtrace: backtrace,
17 17 request: request,
18 18 server_environment: server_environment,
19 19 api_key: params['key'].present? ? params['key'] : params['project_id'],
20   - notifier: params['notifier'],
  20 + notifier: context['notifier'] || params['notifier'],
21 21 user_attributes: user_attributes
22 22 }
  23 + end
23 24  
  25 + def report
24 26 ErrorReport.new(attributes)
25 27 end
26 28  
... ...
spec/lib/airbrake_api/v3/notice_parser_spec.rb
1 1 describe AirbrakeApi::V3::NoticeParser do
2 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 11 it 'raises error when errors attribute is missing' do
5 12 expect do
6   - AirbrakeApi::V3::NoticeParser.new({}).report
  13 + described_class.new({}).report
7 14 end.to raise_error(AirbrakeApi::ParamsError)
8 15  
9 16 expect do
10   - AirbrakeApi::V3::NoticeParser.new('errors' => []).report
  17 + described_class.new('errors' => []).report
11 18 end.to raise_error(AirbrakeApi::ParamsError)
12 19 end
13 20  
14 21 it 'parses JSON payload and returns ErrorReport' do
15 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 25 notice = report.generate_notice!
19 26  
20 27 expect(report.error_class).to eq('Error')
... ... @@ -37,7 +44,7 @@ describe AirbrakeApi::V3::NoticeParser do
37 44 it 'parses JSON payload when api_key is missing but project_id is present' do
38 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 48 expect(report).to be_valid
42 49 end
43 50  
... ... @@ -46,7 +53,7 @@ describe AirbrakeApi::V3::NoticeParser do
46 53 params = JSON.parse(json)
47 54 params['key'] = app.api_key
48 55  
49   - report = AirbrakeApi::V3::NoticeParser.new(params).report
  56 + report = described_class.new(params).report
50 57 report.generate_notice!
51 58  
52 59 expect(report.error_class).to eq('Error')
... ... @@ -54,6 +61,22 @@ describe AirbrakeApi::V3::NoticeParser do
54 61 expect(report.backtrace.lines.size).to eq(0)
55 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 80 def build_params(options = {})
58 81 json = Rails.root.join('spec', 'fixtures', 'api_v3_request.json').read
59 82 data = JSON.parse(json)
... ...