Skip to content

Commit

Permalink
Catch error during maintenance window request (#19482)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmtolmach authored Nov 19, 2024
1 parent 20508f0 commit cbfc618
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ maintenance:
carma: P6XLE0T
appeals: P9S4RFU
arcgis: P45YBFA
coe: PXXXXXX
coe: PSY4HU1
dslogon: P9DJJAV
es: PH7OPR4
evss: PZKWB6Y
Expand Down
16 changes: 15 additions & 1 deletion lib/pagerduty/maintenance_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def get_all(options = {})

def get_all_raw(options = {})
resp = get_raw(options)
return [] if resp.nil?

windows = resp['maintenance_windows']
while resp['more']
offset = resp['offset'] + resp['limit']
Expand All @@ -34,7 +36,19 @@ def get_raw(options = {})
'filter' => 'open',
'service_ids' => PagerDuty::Configuration.service_ids
}.merge(options)
perform(:get, 'maintenance_windows', query).body

begin
perform(:get, 'maintenance_windows', query).body
rescue => e
if e&.original_status == 400
Rails.logger.error(
"Invalid arguments sent to PagerDuty. One of the following Service IDs is bad: #{query['service_ids']}"
)
else
Rails.logger.error("Querying PagerDuty for maintenance windows failed with the error: #{e.message}")
end
nil
end
end

def convert(raw_mws)
Expand Down
34 changes: 34 additions & 0 deletions spec/lib/pagerduty/maintenance_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,40 @@
end
end

context 'with bad requests' do
before { allow(Settings.maintenance).to receive(:services).and_return({ evss: 'XBADXX' }) }

it 'returns empty results and error with bad service IDs' do
stub_request(:get, 'https://api.pagerduty.com/maintenance_windows')
.with(query: hash_including('service_ids' => %w[XBADXX], 'offset' => '0'))
.to_return(
status: 400
)

expect(Rails.logger).to receive(:error)
.with('Invalid arguments sent to PagerDuty. One of the following Service IDs is bad: ["XBADXX"]')

windows = subject.get_all
expect(windows).to be_empty
end

it 'returns empty results and custom error message on 429 error' do
stub_request(:get, 'https://api.pagerduty.com/maintenance_windows')
.with(query: hash_including('service_ids' => %w[XBADXX], 'offset' => '0'))
.to_return(
status: 429
)

# rubocop:disable Layout/LineLength
error_message = 'Querying PagerDuty for maintenance windows failed with the error: BackendServiceException: {:status=>429, :detail=>nil, :code=>"PAGERDUTY_429", :source=>nil}'
# rubocop:enable Layout/LineLength
expect(Rails.logger).to receive(:error).with(error_message)

windows = subject.get_all
expect(windows).to be_empty
end
end

context 'with options specified' do
let(:body) { File.read('spec/support/pagerduty/maintenance_windows_simple.json') }

Expand Down

0 comments on commit cbfc618

Please sign in to comment.