Skip to content

Commit

Permalink
Merge pull request #6158 from empty-codes/handles-wikiapi-unexpected-…
Browse files Browse the repository at this point in the history
…responses

Raises a custom `PageFetchError` when the `response.status `is not 200 or 404
  • Loading branch information
ragesoss authored Jan 29, 2025
2 parents d3110bd + 6f62623 commit 4282de1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
9 changes: 9 additions & 0 deletions lib/wiki_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def get_page_content(page_title)
response.body.force_encoding('UTF-8')
when 404
''
else
raise PageFetchError.new(page_title, response&.status)
end
end

Expand Down Expand Up @@ -121,6 +123,13 @@ def too_many_requests?(e)
e.status == 429
end

class PageFetchError < StandardError
def initialize(page, status)
message = "Failed to fetch content for #{page} with response status: #{status.inspect}"
super(message)
end
end

TYPICAL_ERRORS = [Faraday::TimeoutError,
Faraday::ConnectionFailed,
MediawikiApi::HttpError,
Expand Down
1 change: 1 addition & 0 deletions spec/controllers/users/enrollment_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
allow_any_instance_of(WikiCourseEdits).to receive(:update_course)
allow_any_instance_of(WikiCourseEdits).to receive(:remove_assignment)
allow_any_instance_of(WikiCourseEdits).to receive(:update_assignments)
allow_any_instance_of(WikiApi).to receive(:get_page_content).and_return('Some content')
allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(user)
course.campaigns << Campaign.first
end
Expand Down
22 changes: 17 additions & 5 deletions spec/lib/wiki_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,45 @@
class UnexpectedError < StandardError; end

describe WikiApi do
describe 'error handling and calls ApiErrorHandling method' do
describe 'handles errors by calling ApiErrorHandling method and raising a PageFetchError' do
let(:subject) { described_class.new.get_page_content('Ragesoss') }

it 'handles mediawiki 503 errors gracefully' do
stub_wikipedia_503_error
expect(subject).to eq(nil)
expect { subject }.to raise_error(
WikiApi::PageFetchError,
/Failed to fetch content for Ragesoss with response status: 503/
)
end

it 'handles timeout errors gracefully' do
allow_any_instance_of(MediawikiApi::Client).to receive(:send)
.and_raise(Faraday::TimeoutError)
expect_any_instance_of(described_class).to receive(:log_error).once
expect(subject).to eq(nil)
expect { subject }.to raise_error(
WikiApi::PageFetchError,
/Failed to fetch content for Ragesoss with response status: nil/
)
end

it 'handles API errors gracefully' do
allow_any_instance_of(MediawikiApi::Client).to receive(:send)
.and_raise(MediawikiApi::ApiError)
expect_any_instance_of(described_class).to receive(:log_error).once
expect(subject).to eq(nil)
expect { subject }.to raise_error(
WikiApi::PageFetchError,
/Failed to fetch content for Ragesoss with response status: nil/
)
end

it 'handles HTTP errors gracefully' do
allow_any_instance_of(MediawikiApi::Client).to receive(:send)
.and_raise(MediawikiApi::HttpError, '')
expect_any_instance_of(described_class).to receive(:log_error).once
expect(subject).to eq(nil)
expect { subject }.to raise_error(
WikiApi::PageFetchError,
/Failed to fetch content for Ragesoss with response status: nil/
)
end
end

Expand Down

0 comments on commit 4282de1

Please sign in to comment.