Skip to content

Commit

Permalink
Rename MissingGroupType and UnsupportedGroupType (#2227)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
ericproulx authored Feb 14, 2022
1 parent ed04e2c commit e93d278
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/grape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/grape/dsl/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 3 additions & 1 deletion lib/grape/exceptions/missing_group_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion lib/grape/exceptions/unsupported_group_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions lib/grape/validations/params_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
15 changes: 15 additions & 0 deletions spec/grape/exceptions/missing_group_type_spec.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions spec/grape/exceptions/unsupported_group_type_spec.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions spec/grape/validations/params_scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,15 @@ 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
optional :a do
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
Expand Down Expand Up @@ -324,15 +324,15 @@ 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
optional :a, type: Set do
requires :b
end
end
end.to raise_error Grape::Exceptions::UnsupportedGroupTypeError
end.to raise_error Grape::Exceptions::UnsupportedGroupType
end
end

Expand Down

0 comments on commit e93d278

Please sign in to comment.