diff --git a/CHANGELOG.md b/CHANGELOG.md index fc149e1aa..c3c9f6347 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ * [#2232](https://github.com/ruby-grape/grape/pull/2232): Fix kwargs support in shared params definition - [@dm1try](https://github.com/dm1try). * [#2229](https://github.com/ruby-grape/grape/pull/2229): Do not collect params in route settings - [@dnesteryuk](https://github.com/dnesteryuk). * [#2234](https://github.com/ruby-grape/grape/pull/2234): Remove non-utf-8 characters from format before generating JSON error - [@bschmeck](https://github.com/bschmeck). +* [#2227](https://github.com/ruby-grape/grape/pull/2222): Rename "MissingGroupType" and "UnsupportedGroupType" exceptions - [@ericproulx](https://github.com/ericproulx). * Your contribution here. ### 1.6.2 (2021/12/30) diff --git a/UPGRADING.md b/UPGRADING.md index 8dc77ea67..969fc1347 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -1,6 +1,17 @@ Upgrading Grape =============== +### Upgrading to >= 1.6.3 + +#### Exceptions renaming + +The following exceptions has been renamed for consistency through exceptions naming : + +* `MissingGroupTypeError` => `MissingGroupType` +* `UnsupportedGroupTypeError` => `UnsupportedGroupType` + +See [#2227](https://github.com/ruby-grape/grape/pull/2227) for more information. + ### Upgrading to >= 1.6.0 #### Parameter renaming with :as diff --git a/lib/grape.rb b/lib/grape.rb index 0f0da8e1e..7aedc07db 100644 --- a/lib/grape.rb +++ b/lib/grape.rb @@ -73,8 +73,8 @@ module Exceptions autoload :UnknownParameter autoload :InvalidWithOptionForRepresent autoload :IncompatibleOptionValues - autoload :MissingGroupTypeError, 'grape/exceptions/missing_group_type' - autoload :UnsupportedGroupTypeError, 'grape/exceptions/unsupported_group_type' + autoload :MissingGroupType + autoload :UnsupportedGroupType autoload :InvalidMessageBody autoload :InvalidAcceptHeader autoload :InvalidVersionHeader diff --git a/lib/grape/dsl/parameters.rb b/lib/grape/dsl/parameters.rb index 3a8bad653..31a7b34e7 100644 --- a/lib/grape/dsl/parameters.rb +++ b/lib/grape/dsl/parameters.rb @@ -148,8 +148,8 @@ def optional(*attrs, &block) # check type for optional parameter group if attrs && block - raise Grape::Exceptions::MissingGroupTypeError.new if type.nil? - raise Grape::Exceptions::UnsupportedGroupTypeError.new unless Grape::Validations::Types.group?(type) + raise Grape::Exceptions::MissingGroupType if type.nil? + raise Grape::Exceptions::UnsupportedGroupType unless Grape::Validations::Types.group?(type) end if opts[:using] diff --git a/lib/grape/exceptions/missing_group_type.rb b/lib/grape/exceptions/missing_group_type.rb index 398113ff8..d400424b4 100644 --- a/lib/grape/exceptions/missing_group_type.rb +++ b/lib/grape/exceptions/missing_group_type.rb @@ -2,10 +2,12 @@ module Grape module Exceptions - class MissingGroupTypeError < Base + class MissingGroupType < Base def initialize super(message: compose_message(:missing_group_type)) end end end end + +Grape::Exceptions::MissingGroupTypeError = Grape::Exceptions::MissingGroupType diff --git a/lib/grape/exceptions/unsupported_group_type.rb b/lib/grape/exceptions/unsupported_group_type.rb index 9cbc7aac2..3fb6160b7 100644 --- a/lib/grape/exceptions/unsupported_group_type.rb +++ b/lib/grape/exceptions/unsupported_group_type.rb @@ -2,10 +2,12 @@ module Grape module Exceptions - class UnsupportedGroupTypeError < Base + class UnsupportedGroupType < Base def initialize super(message: compose_message(:unsupported_group_type)) end end end end + +Grape::Exceptions::UnsupportedGroupTypeError = Grape::Exceptions::UnsupportedGroupType diff --git a/lib/grape/validations/params_scope.rb b/lib/grape/validations/params_scope.rb index 6f6907141..d9dbff469 100644 --- a/lib/grape/validations/params_scope.rb +++ b/lib/grape/validations/params_scope.rb @@ -208,8 +208,8 @@ def new_scope(attrs, optional = false, &block) # if required params are grouped and no type or unsupported type is provided, raise an error type = attrs[1] ? attrs[1][:type] : nil if attrs.first && !optional - raise Grape::Exceptions::MissingGroupTypeError.new if type.nil? - raise Grape::Exceptions::UnsupportedGroupTypeError.new unless Grape::Validations::Types.group?(type) + raise Grape::Exceptions::MissingGroupType if type.nil? + raise Grape::Exceptions::UnsupportedGroupType unless Grape::Validations::Types.group?(type) end self.class.new( diff --git a/spec/grape/exceptions/missing_group_type_spec.rb b/spec/grape/exceptions/missing_group_type_spec.rb new file mode 100644 index 000000000..509f823f2 --- /dev/null +++ b/spec/grape/exceptions/missing_group_type_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +RSpec.describe Grape::Exceptions::MissingGroupType do + describe '#message' do + subject { described_class.new.message } + + it { is_expected.to include 'group type is required' } + end + + describe '#alias' do + subject { described_class } + + it { is_expected.to eq(Grape::Exceptions::MissingGroupTypeError) } + end +end diff --git a/spec/grape/exceptions/unsupported_group_type_spec.rb b/spec/grape/exceptions/unsupported_group_type_spec.rb new file mode 100644 index 000000000..167755aef --- /dev/null +++ b/spec/grape/exceptions/unsupported_group_type_spec.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +RSpec.describe Grape::Exceptions::UnsupportedGroupType do + subject { described_class.new } + + describe '#message' do + subject { described_class.new.message } + + it { is_expected.to include 'group type must be Array, Hash, JSON or Array[JSON]' } + end + + describe '#alias' do + subject { described_class } + + it { is_expected.to eq(Grape::Exceptions::UnsupportedGroupTypeError) } + end +end diff --git a/spec/grape/validations/params_scope_spec.rb b/spec/grape/validations/params_scope_spec.rb index cba61b17d..20f03f593 100644 --- a/spec/grape/validations/params_scope_spec.rb +++ b/spec/grape/validations/params_scope_spec.rb @@ -256,7 +256,7 @@ def initialize(value) requires :b end end - end.to raise_error Grape::Exceptions::MissingGroupTypeError + end.to raise_error Grape::Exceptions::MissingGroupType expect do subject.params do @@ -264,7 +264,7 @@ def initialize(value) requires :b end end - end.to raise_error Grape::Exceptions::MissingGroupTypeError + end.to raise_error Grape::Exceptions::MissingGroupType end it 'allows Hash as type' do @@ -324,7 +324,7 @@ def initialize(value) requires :b end end - end.to raise_error Grape::Exceptions::UnsupportedGroupTypeError + end.to raise_error Grape::Exceptions::UnsupportedGroupType expect do subject.params do @@ -332,7 +332,7 @@ def initialize(value) requires :b end end - end.to raise_error Grape::Exceptions::UnsupportedGroupTypeError + end.to raise_error Grape::Exceptions::UnsupportedGroupType end end