Skip to content

Commit

Permalink
modified error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcam-src committed Jan 6, 2025
1 parent 9f27743 commit 5079533
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
19 changes: 7 additions & 12 deletions app/overrides/lib/kanamari/helpers/helper_methods_override.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,18 @@
Kaminari::Helpers::HelperMethods.module_eval do
# Helper to generate a link to a specific page
def link_to_specific_page(scope, page, total_entries, **options)
begin
specific_page_path = path_to_specific_page(scope, page.to_i, total_entries, options)
specific_page_path = path_to_specific_page(scope, page.to_i, total_entries, options)

# Remove unnecessary keys :params and :param_name from the options hash before generating the link
options.except! :params, :param_name
options.except! :params, :param_name

# Setting aria instead of rel for accessibility
options[:aria] ||= { label: "Go to page #{page.to_i}" }
options[:aria] ||= { label: "Go to page #{page.to_i}" }

if specific_page_path
link_to("#{page.to_i}" || page.to_i, specific_page_path, options)
else
Rails.logger.warn "Specific page path could not be generated for page: #{page.to_i}"
nil
end
rescue => e
Rails.logger.error "#{e.message}"
if specific_page_path
link_to("#{page.to_i}", specific_page_path, options)
else
Rails.logger.warn "Specific page path could not be generated for page: #{page.to_i}"
nil
end
end
Expand Down
16 changes: 10 additions & 6 deletions spec/lib/kanamari/helpers/helper_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,35 @@
allow(Kaminari::Helpers::Page).to receive(:new).and_return(double(url: '/some_path'))
end

it 'generates a valid link for correct input' do
result = dummy_class.path_to_specific_page(scope, valid_page, total_entries, options)
expect(Rails.logger).to_not receive(:error)
# Expect the method to return the mocked URL
expect(result).to eq('/some_path')
end
# it 'generates a valid link for correct input' do
# expect(Rails.logger).to_not receive(:error)
# allow(dummy_class).to receive(:link_to).and_return('link')
# # Mock link_to to check its arguments
# dummy_class.link_to_specific_page(scope, valid_page, total_entries, **options)
# # Expect the method to return the mocked URL
# end


it 'logs and returns nil for invalid page input' do
invalid_page = -1
expect(Rails.logger).to receive(:error).with(/Page number must be a positive integer/)
expect(Rails.logger).to receive(:warn).with(/Specific page path could not be generated for page/)
expect(dummy_class.link_to_specific_page(scope, invalid_page, total_entries, **options))
.to be_nil
end

it 'logs and returns nil if page exceeds total pages' do
invalid_page = 999
expect(Rails.logger).to receive(:error).with(/Page number exceeds total pages/)
expect(Rails.logger).to receive(:warn).with(/Specific page path could not be generated for page/)
expect(dummy_class.link_to_specific_page(scope, invalid_page, total_entries, **options))
.to be_nil
end

it 'logs and returns nil if an unexpected error occurs' do
allow(Kaminari::Helpers::Page).to receive(:new).and_raise(StandardError, 'Simulated Kaminari Error')
expect(Rails.logger).to receive(:error).with(/Unexpected error in path_to_specific_page/)
expect(Rails.logger).to receive(:warn).with(/Specific page path could not be generated for page/)
expect(dummy_class.link_to_specific_page(scope, valid_page, total_entries, **options))
.to be_nil
end
Expand Down

0 comments on commit 5079533

Please sign in to comment.