1. 10
  1.  

  2. 1

    I work at Stripe if you have any questions not answered by the post.

    1. 1

      Good article, also a good example of needlessly raising exceptions. Incorrect user input is not an exceptional event, handle it cleanly when it occurs.

      1. 1

        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:

        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?