-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Cop for raise/fail arguments is too restrictive. #552
Comments
The question is not whether this is legal Ruby code or not - obviously it is. The point is to enforce either |
Well the point is whether one allows the following expression is acceptable in Rubocop: It's true that by far exception constructors take a -part of- message as the sole argument. But that's not always true. Let's take an example from the standard library, namely rubygems. It defines the Well as it now rubocop will flag this as an offense while this is perfectly sensible code. |
Fair enough. I'll relax the requirements and allow the use of Exception.new(...) with more than 1 argument. |
Blagodaria. :) |
За нищо! :-) |
@bbatsov shouldn't the use of |
@segiddins Good remark. In principle, when designing exception constructors you should be completely be free to define their signature (number of arguments and type of arguments). On the other hand, if the current cop implementation satisfies 95% of the cases maybe one could just disable it when it shouts when it shouldn't. Maybe it is not worth to change the cop for less frequent cases. Now I am not aware of stats about the ways exceptions are raised in some examplar projects/standard libraries. This could help in figuring out how frequent are the cases you mention occurring in "popular" Ruby code. |
I don't understand why rubocop finds an offense there:
|
@AlexCppns It wants it to be raise ActionController::RoutingError, 'Not Found' This is the Style/RaiseArgs:
EnforcedStyle: compact |
@bbatsov A bit of an edge case, but rubocop finds an offence with something like the following as well: args = [message, other_arg_1, other_arg_2]
raise MyError.new(*args) |
@mattdowdell An edge case, yes. But still a bug since |
For an expression like:
fail(MyExceptionClass.new(some_exception_arg1,some_exception_arg1))
Where:
some_exception_argX
are arguments of that exception class constructor.Rubocop will report an offence like:
... C: Provide an exception class and message as arguments to fail.
fail(UnreachableSubstepArgument.new(substep_arg))
This is problematic because:
-When one raises an Exception it is a good programming practice to provide as much context data as needed for a proper understanding. Restricting the calls to
fail/raise
to an exception class plus a message argument limits the kind of feedback you can provide upon raising an exception.-Furthermore, calling fail/raise with an exception object is perfectly legal Ruby:
"if raise is called with a single Exception object as its argument, it raises that exception". Sentence taken from: page 157 of "The Ruby Programming Language" of D. Flanagan & Yukihiro Matsumoto, O'Reilly 2008 ISBN 978-0-596-51617-8.
The text was updated successfully, but these errors were encountered: