Skip to content

Commit

Permalink
update error handling, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcam-src committed Jan 6, 2025
1 parent 51b6064 commit 9f27743
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
13 changes: 5 additions & 8 deletions app/overrides/lib/kanamari/helpers/helper_methods_override.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ def link_to_specific_page(scope, page, total_entries, **options)
Rails.logger.warn "Specific page path could not be generated for page: #{page.to_i}"
nil
end
rescue ArgumentError => e
Rails.logger.error "Error in link_to_specific_page: #{e.message}"
nil
rescue StandardError => e
Rails.logger.error "Unexpected error in link_to_specific_page: #{e.message}"
rescue => e
Rails.logger.error "#{e.message}"
nil
end
end
Expand All @@ -34,17 +31,17 @@ def path_to_specific_page(scope, page_integer, total_entries, options = {})
limit = scope.instance_variable_get(:@limit)
total_pages = (total_entries.to_f / limit).ceil

Rails.logger.info "path_to_specific_page: total_entries=#{total_entries}, limit=#{limit}, calculated total_pages=#{total_pages}, page=#{page_integer}"
Rails.logger.debug "path_to_specific_page: total_entries=#{total_entries}, limit=#{limit}, calculated total_pages=#{total_pages}, page=#{page_integer}"

# Validate inputs
raise ArgumentError, 'Page number must be a positive integer' unless page_integer.positive?
raise ArgumentError, "Page number exceeds total pages (#{total_pages})" if page_integer > total_pages
# Generate URL using Kaminari's Page helper
Kaminari::Helpers::Page.new(self, **options.reverse_merge(page: page_integer)).url
rescue ArgumentError => e
Rails.logger.info "Error in path_to_specific_page: #{e.message}"
Rails.logger.error "Error in path_to_specific_page: #{e.message}"
nil
rescue StandardError => e
rescue => e
Rails.logger.error "Unexpected error in path_to_specific_page: #{e.message}\n#{e.backtrace.join("\n")}"
nil
end
Expand Down
46 changes: 46 additions & 0 deletions spec/lib/kanamari/helpers/helper_methods_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true
require 'rails_helper'

RSpec.describe Kaminari::Helpers::HelperMethods do
let(:dummy_class) { Class.new { extend Kaminari::Helpers::HelperMethods } }
let(:scope) { double('FacetPaginator', instance_variable_get: 20) }
let(:valid_page) { 1 }
let(:total_entries) { 100 }
let(:options) { { some_option: 'value' } }

describe '#link_to_specific_page' do
before do
# Stub Kaminari::Helpers::Page.new to return a mock URL
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 '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(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(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(dummy_class.link_to_specific_page(scope, valid_page, total_entries, **options))
.to be_nil
end
end
end

0 comments on commit 9f27743

Please sign in to comment.