Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
0.2.3 (Next Release)
====================

* [#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).
* Your contribution here.

0.2.2
Expand Down
6 changes: 3 additions & 3 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,12 @@ end

### Validation Errors

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

```ruby
rescue_from ValidationError do |e|
rescue_from Grape::Exceptions::ValidationError do |e|
Rack::Response.new({
'status' => e.status,
'message' => e.message,
Expand Down
2 changes: 1 addition & 1 deletion lib/grape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ module Grape

module Exceptions
autoload :Base, 'grape/exceptions/base'
autoload :ValidationError, 'grape/exceptions/validation_error'
end
autoload :ValidationError, 'grape/exceptions/validation_error'

module Middleware
autoload :Base, 'grape/middleware/base'
Expand Down
14 changes: 9 additions & 5 deletions lib/grape/exceptions/validation_error.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
require 'grape/exceptions/base'

class ValidationError < Grape::Exceptions::Base
attr_accessor :param
module Grape
module Exceptions
class ValidationError < Grape::Exceptions::Base
attr_accessor :param

def initialize(args = {})
@param = args[:param].to_s if args.has_key? :param
super
def initialize(args = {})
@param = args[:param].to_s if args.has_key? :param
super
end
end
end
end
2 changes: 1 addition & 1 deletion lib/grape/validations/coerce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def validate_param!(attr_name, params)
if valid_type?(new_value)
params[attr_name] = new_value
else
raise ValidationError, :status => 400, :param => attr_name, :message => "invalid parameter: #{attr_name}"
raise Grape::Exceptions::ValidationError, :status => 400, :param => attr_name, :message => "invalid parameter: #{attr_name}"
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/presence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Validations
class PresenceValidator < Validator
def validate_param!(attr_name, params)
unless params.has_key?(attr_name)
raise ValidationError, :status => 400, :param => attr_name, :message => "missing parameter: #{attr_name}"
raise Grape::Exceptions::ValidationError, :status => 400, :param => attr_name, :message => "missing parameter: #{attr_name}"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Validations
class RegexpValidator < SingleOptionValidator
def validate_param!(attr_name, params)
if params[attr_name] && !( params[attr_name].to_s =~ @option )
raise ValidationError, :status => 400, :param => attr_name, :message => "invalid parameter: #{attr_name}"
raise Grape::Exceptions::ValidationError, :status => 400, :param => attr_name, :message => "invalid parameter: #{attr_name}"
end
end
end
Expand Down