Skip to content

Commit e2f6403

Browse files
committed
Merge pull request #265 from firebaseco/validation_error_module
The class ValidationError should be inside Grape module. Fixes #264
2 parents 53cbb58 + 24e0760 commit e2f6403

File tree

7 files changed

+17
-12
lines changed

7 files changed

+17
-12
lines changed

CHANGELOG.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
0.2.3 (Next Release)
22
====================
33

4+
* [#265](https://github.com/intridea/grape/issues/264): Fix: The class ValidationError should be in the module "Grape::Exceptions". Fixes [#264](https://github.com/intridea/grape/issues/264) - [@thepumpkin1979](https://github.com/thepumpkin1979).
45
* Your contribution here.
56

67
0.2.2

README.markdown

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,12 +299,12 @@ end
299299

300300
### Validation Errors
301301

302-
When validation and coercion erros occur an exception of type `ValidationError` is raised.
302+
When validation and coercion erros occur an exception of type `Grape::Exceptions::ValidationError` is raised.
303303
If the exception goes uncaught it will respond with a status of 400 and an error message.
304-
You can rescue a `ValidationError` and respond with a custom response.
304+
You can rescue a `Grape::Exceptions::ValidationError` and respond with a custom response.
305305

306306
```ruby
307-
rescue_from ValidationError do |e|
307+
rescue_from Grape::Exceptions::ValidationError do |e|
308308
Rack::Response.new({
309309
'status' => e.status,
310310
'message' => e.message,

lib/grape.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ module Grape
1313

1414
module Exceptions
1515
autoload :Base, 'grape/exceptions/base'
16+
autoload :ValidationError, 'grape/exceptions/validation_error'
1617
end
17-
autoload :ValidationError, 'grape/exceptions/validation_error'
1818

1919
module Middleware
2020
autoload :Base, 'grape/middleware/base'
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
require 'grape/exceptions/base'
22

3-
class ValidationError < Grape::Exceptions::Base
4-
attr_accessor :param
3+
module Grape
4+
module Exceptions
5+
class ValidationError < Grape::Exceptions::Base
6+
attr_accessor :param
57

6-
def initialize(args = {})
7-
@param = args[:param].to_s if args.has_key? :param
8-
super
8+
def initialize(args = {})
9+
@param = args[:param].to_s if args.has_key? :param
10+
super
11+
end
12+
end
913
end
1014
end

lib/grape/validations/coerce.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def validate_param!(attr_name, params)
1212
if valid_type?(new_value)
1313
params[attr_name] = new_value
1414
else
15-
raise ValidationError, :status => 400, :param => attr_name, :message => "invalid parameter: #{attr_name}"
15+
raise Grape::Exceptions::ValidationError, :status => 400, :param => attr_name, :message => "invalid parameter: #{attr_name}"
1616
end
1717
end
1818

lib/grape/validations/presence.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Validations
33
class PresenceValidator < Validator
44
def validate_param!(attr_name, params)
55
unless params.has_key?(attr_name)
6-
raise ValidationError, :status => 400, :param => attr_name, :message => "missing parameter: #{attr_name}"
6+
raise Grape::Exceptions::ValidationError, :status => 400, :param => attr_name, :message => "missing parameter: #{attr_name}"
77
end
88
end
99
end

lib/grape/validations/regexp.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Validations
44
class RegexpValidator < SingleOptionValidator
55
def validate_param!(attr_name, params)
66
if params[attr_name] && !( params[attr_name].to_s =~ @option )
7-
raise ValidationError, :status => 400, :param => attr_name, :message => "invalid parameter: #{attr_name}"
7+
raise Grape::Exceptions::ValidationError, :status => 400, :param => attr_name, :message => "invalid parameter: #{attr_name}"
88
end
99
end
1010
end

0 commit comments

Comments
 (0)