Skip to content

Commit

Permalink
throw an error when type is unsupported
Browse files Browse the repository at this point in the history
  • Loading branch information
GCorbel committed Aug 29, 2024
1 parent aa019aa commit 7f13aca
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ en:
message: "Unsupported field type."
summary: "The type of the field '%{field}' is not supported: %{type}."
resolution: "Please open a feature request in https://github.com/mongoid/mongoid-scroll."

unsupported_type:
message: "Unsupported type."
summary: "The type supplied in the cursor is not supported: %{type}."
resolution: "The cursor type can be either ':previous' or ':next'."
2 changes: 2 additions & 0 deletions lib/mongoid/scroll/base_cursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def initialize(value, options = {})
@direction = options[:direction] || 1
@include_current = options[:include_current] || false
@type = options[:type] || :next

raise Mongoid::Scroll::Errors::UnsupportedTypeError.new(type: @type) if ![:previous, :next].include?(@type)
end

def criteria
Expand Down
1 change: 1 addition & 0 deletions lib/mongoid/scroll/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
require 'mongoid/scroll/errors/invalid_base64_cursor_error'
require 'mongoid/scroll/errors/no_such_field_error'
require 'mongoid/scroll/errors/unsupported_field_type_error'
require 'mongoid/scroll/errors/unsupported_type_error'
11 changes: 11 additions & 0 deletions lib/mongoid/scroll/errors/unsupported_type_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Mongoid
module Scroll
module Errors
class UnsupportedTypeError < Mongoid::Scroll::Errors::Base
def initialize(opts = {})
super(compose_message('unsupported_type', opts))
end
end
end
end
end
8 changes: 8 additions & 0 deletions spec/mongoid/cursor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@
end.to raise_error ArgumentError
end
end
context 'an invalid type cursor' do
let(:feed_item) { Feed::Item.create!(a_string: 'astring') }
it 'raises Mongoid::Scroll::Errors::UnsupportedTypeError' do
expect do
Mongoid::Scroll::Cursor.new "#{feed_item.a_string}:#{feed_item.id}", field_name: 'a_string', field_type: String, include_current: true, type: :invalid
end.to raise_error Mongoid::Scroll::Errors::UnsupportedTypeError, /The type supplied in the cursor is not supported: invalid./
end
end
context 'a cursor with include_current set to true' do
let(:feed_item) { Feed::Item.create!(a_string: 'astring') }
subject do
Expand Down

0 comments on commit 7f13aca

Please sign in to comment.