Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rescue StandardError from explicit values validator procs #1679

Merged
merged 5 commits into from
Aug 25, 2017

Conversation

jlfaber
Copy link
Contributor

@jlfaber jlfaber commented Aug 24, 2017

Example:

params do
  requires :number, type: Integer, values: ->(v) { v.even? && v < 25 }
end

Right now, sending a non-numeric string or a nil value for number will result in a stack trace because neither String nor NilClass have an even? method. One could address this with an additional type check in the proc, but that would result in a lot of extra code that doesn't really contribute to the overall clarity of the declaration.

Instead, we should rescue any StandardError raised from an arity-one values validator proc and treat it as a false result.

@jlfaber
Copy link
Contributor Author

jlfaber commented Aug 24, 2017

This issue came up in both #1670 and #1676 so it's likely to keep biting people.

Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you feel that we're going to be hiding an exception here? Feel free to merge, I just wanted to raise this before we do.

return param_array.all? { |param| values.call(param) } if values.is_a? Proc
begin
return param_array.all? { |param| values.call(param) } if values.is_a? Proc
rescue => _e
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rescue StandardError => e, even if it's implicit to make it clear?

@jlfaber
Copy link
Contributor Author

jlfaber commented Aug 24, 2017

@dblock - Fair point. Yeah, it definitely hides the details of the exception. How would you feel about doing this inside the rescue?

warn "Error '#{e}' raised while validating attribute '#{attr_name}'"

Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A warning would be good enough I think.

@@ -43,7 +43,11 @@ def validate_param!(attr_name, params)
def check_values(param_array)
values = @values.is_a?(Proc) && @values.arity.zero? ? @values.call : @values
Copy link
Member

@dblock dblock Aug 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why aren't we also rescuing this @values.call here in that case? (and would need a spec if that makes sense)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that an error raised from an arity-zero proc is a different and more serious condition. The output there is expected to be an array of valid values (or invalid values in the case of except_values) and a raised error means that the validator is completely broken. Yes, we could return an empty array and warn in that case, but that doesn't feel like the right thing to do. In the values case it wouldn't ever accept anything; and, worse, in the except_values case it would always accept everything.

The primary reason I want to rescue in the arity-one case is because of the likelihood that unexpected input values (non-numeric string or nil in the example above) will raise errors in simple validation code that doesn't do thorough type-safety checks. But in the arity-zero case, there is no input value so that's not a consideration.

@jlfaber
Copy link
Contributor Author

jlfaber commented Aug 25, 2017

Added a warning for the arity-one case only. Let me know if you feel strongly about rescuing in the arity-zero case. Otherwise, this should be ready to merge.

@dblock dblock merged commit 37b7c47 into ruby-grape:master Aug 25, 2017
@dblock
Copy link
Member

dblock commented Aug 25, 2017

Merged. Thank you.

@jlfaber jlfaber deleted the handle_values_errors branch August 25, 2017 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants