I work at Stripe if you have any questions not answered by the post.
Good article, also a good example of needlessly raising exceptions. Incorrect user input is not an exceptional event, handle it cleanly when it occurs.
I’m wondering, how would you handle it better (“cleanly”)? They currently do this:
raise UserError.new("No charge #{id}!")
The way I can think of to avoid using exceptions would be to just change raise to return:
raise
return
return UserError.new("No charge #{id}!")
And then in some higher-level module, instead of
begin # try to return a value catch UserError # format the error and return it end
, you would do
returned_value = handle_the_given_api_call if returned_value.is_a?(UserError) # format the error before returning it else # just return the value end
But what is the advantage of that?
I work at Stripe if you have any questions not answered by the post.
Good article, also a good example of needlessly raising exceptions. Incorrect user input is not an exceptional event, handle it cleanly when it occurs.
I’m wondering, how would you handle it better (“cleanly”)? They currently do this:
The way I can think of to avoid using exceptions would be to just change
raisetoreturn:And then in some higher-level module, instead of
, you would do
But what is the advantage of that?