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

Avoid rescuing argument error in next node blocks #2032

Merged
merged 3 commits into from
Oct 26, 2015

Conversation

chrisroos
Copy link
Contributor

I don't think it's a good idea to rescue from such a generic exception in our
next_node blocks. It hides real problems in our Ruby code and makes them look
as though the user has entered invalid data in the Smart Answer.

The rescuing of ArgumentError caused me problems twice recently. Once when I was
comparing a Date with a String and once when I was calling a method with
incorrect arguments. This change will mean that errors such as this cause the
application to fail fast.

>> Date.today < '2016-01-01'
ArgumentError: comparison of Date with String failed

>> def foo(arg); end
=> :foo
>> foo()
ArgumentError: wrong number of arguments (0 for 1)

The first two commits ("Raise InvalidResponse when parsing Integers in
Value#parse_input" and "Raise InvalidResponse when parsing Floats in
Value#parse_input") should have removed the only places where we were raising
an ArgumentError in response to bad user input.

We're still raising an ArgumentError from SmartAnswer::Calculators::VatPaymentDeadlines#last_payment_date and #funds_recevied_by. I haven't changed these because I don't believe this error will ever be raised by code in a next_node block. The error can only be raised by #last_payment_date and #funds_received_by if an option other than one of those listed in the multiple_choice options for the :how_do_you_want_to_pay? question is entered, and in that case the SmartAnswer::Question::MultipleChoice#parse_input method will raise an InvalidResponse exception first.

I ran all the regression tests locally against this change and didn't see any errors. That's no guarantee that we won't see problems if/when this is deployed to production.

Expected changes

  • None.

This is in preparation for removing the `rescue ArgumentError` statement from
`SmartAnswer::Flow#process`.

Rescuing `ArgumentError` makes it easy to mask real exceptions in the code in
our `next_node` blocks.

I think one of the legitimate reasons to rescue `ArgumentError` at the moment is
because they can be raised by bad user input in this method. Raising
`InvalidResponse` instead seems like the right thing to do and should allow me
to stop rescuing `ArgumentError`.
This is in preparation for removing the `rescue ArgumentError` statement from
`SmartAnswer::Flow#process`.

Rescuing `ArgumentError` makes it easy to mask real exceptions in the code in
our `next_node` blocks.

I think one of the legitimate reasons to rescue `ArgumentError` at the moment is
because they can be raised by bad user input in this method. Raising
`InvalidResponse` instead seems like the right thing to do and should allow me
to stop rescuing `ArgumentError`.
I don't think it's a good idea to rescue from such a generic exception in our
`next_node` blocks. It hides real problems in our Ruby code and makes them look
as though the user has entered invalid data in the Smart Answer.

The rescuing of ArgumentError caused me problems twice recently. Once when I was
comparing a Date with a String and once when I was calling a method with
incorrect arguments. This change will mean that errors such as this cause the
application to fail fast.

    >> Date.today < '2016-01-01'
    ArgumentError: comparison of Date with String failed

    >> def foo(arg); end
    => :foo
    >> foo()
    ArgumentError: wrong number of arguments (0 for 1)

The two preceeding commits ("Raise InvalidResponse when parsing Integers in
Value#parse_input" and "Raise InvalidResponse when parsing Floats in
Value#parse_input") should have removed the only places where we were raising
an `ArgumentError` in response to bad user input.
@floehopper
Copy link
Contributor

It looks as if there may be other places explicitly raising ArgumentError which might need attention:

Also I wonder whether it's worth inspecting the message of the legitimately rescued ArgumentErrors c.f. https://github.com/alphagov/smart-answers/blob/master/lib/smart_answer/question/date.rb#L75-L79

Apart from the above, this looks great to me! 👍

@chrisroos
Copy link
Contributor Author

It looks as if there may be other places explicitly raising ArgumentError which might need attention:

I've already addressed this in the description of this PR. Essentially, I don't think we need to worry about it.

This is the same as the first example above, so I don't think we need to worry about it. I've updated the description of this PR to include this case as well.

This ArgumentError exception will be raised if next_node is called without any arguments which seems OK to me. I'm only interested in exceptions that are raised while evaluating blocks passed to next_node.

@floehopper
Copy link
Contributor

Fair do's. Apologies if I missed the explanation in the PR description first time round.

Look good to me 👍

@chrisroos
Copy link
Contributor Author

Also I wonder whether it's worth inspecting the message of the legitimately rescued ArgumentErrors c.f. https://github.com/alphagov/smart-answers/blob/master/lib/smart_answer/question/date.rb#L75-L79

I guess my thinking was to replicate the rescue ArgumentError behaviour that was previously in Flow#process. I can see that it'd probably be better to explicitly rescue certain exceptions but I think we could attack that separately as it would lead to a slight change in behaviour.

chrisroos added a commit that referenced this pull request Oct 26, 2015
…in-next-node-blocks

Avoid rescuing argument error in next node blocks
@chrisroos chrisroos merged commit 90258e2 into master Oct 26, 2015
@chrisroos chrisroos deleted the avoid-rescuing-argument-error-in-next-node-blocks branch October 26, 2015 11:10
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