From e93d27897fbf5f360fd11064d7b3c4d8094b7c53 Mon Sep 17 00:00:00 2001 From: Eric Proulx Date: Mon, 14 Feb 2022 18:18:02 +0100 Subject: [PATCH] Rename MissingGroupType and UnsupportedGroupType (#2227) * Rename MissingGroupType and UnsupportedGroupType * Add CHANGELOG.md AND UPGRADING.md entries * Quote classes * Add for more information * Add alias for MissingGroupType and UnsupportedGroupeType * Add Final newline missing. --- CHANGELOG.md | 1 + UPGRADING.md | 11 +++++++++++ lib/grape.rb | 4 ++-- lib/grape/dsl/parameters.rb | 4 ++-- lib/grape/exceptions/missing_group_type.rb | 4 +++- lib/grape/exceptions/unsupported_group_type.rb | 4 +++- lib/grape/validations/params_scope.rb | 4 ++-- .../grape/exceptions/missing_group_type_spec.rb | 15 +++++++++++++++ .../exceptions/unsupported_group_type_spec.rb | 17 +++++++++++++++++ spec/grape/validations/params_scope_spec.rb | 8 ++++---- 10 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 spec/grape/exceptions/missing_group_type_spec.rb create mode 100644 spec/grape/exceptions/unsupported_group_type_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index fc149e1aab..c3c9f63476 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 8dc77ea677..969fc1347c 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 0f0da8e1e0..7aedc07dba 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 3a8bad653b..31a7b34e75 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 398113ff83..d400424b4e 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 9cbc7aac27..3fb6160b79 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 6f69071416..d9dbff4693 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 0000000000..509f823f2c --- /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 0000000000..167755aefd --- /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 cba61b17d5..20f03f5935 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