SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [rails] Agile WebDev Flash Error (Solution Found, Still Have Questions)

    i ran into another problem. when i added the flash code to redirect query errors back to the index page. i was able to adjust the rescue line, as shown in the code below, to account for this different error message.

    Code:
      def add_to_cart
        begin
          product = Product.find(params[:id])
        #rescue ActiveRecord::RecordNotFound #mysql error
        rescue ActiveRecord::StatementInvalid # postgresql error
          logger.error("Attempt to access invalid product #{params[:id]}" )
          flash[:notice] = "Invalid product"
          redirect_to :action => :index
        else
          @cart = find_cart
          @cart.add_product(product)
        end
      end
    the flash now works perfect... except, i thought it would be a nice idea to include BOTH error messages (even though they may be database specific, i think the different dbs may well have multiple errors that could possibly come up - so the principle is sound - i think).

    i tried:

    Code:
    rescue ActiveRecord::RecordNotFound || rescue ActiveRecord::StatementInvalid
    and

    Code:
    rescue ActiveRecord::RecordNotFound || ActiveRecord::StatementInvalid
    the former failed with...
    Code:
    app/controllers/store_controller.rb:10: parse error, unexpected kRESCUE
        rescue ActiveRecord::RecordNotFound || rescue ActiveRecord::StatementInvalid
    
    ...
    
    This error occured while loading the following files:
       script/../config/../app/controllers/store_controller.rb
    the latter failed with...
    Code:
    PGError: ERROR:  invalid input syntax for integer: "2gg"
    : SELECT * FROM products WHERE (products.id = '2gg')  LIMIT 1
    ...
    am i just off base trying to catch more than one possible ActiveRecord error?

    if it makes sense, can it be done? do i need to nest a second begin->resuce->end structure to achieve the result?

    how come "||" doesn't work?

    tia...

  2. #2
    SitePoint Enthusiast Stevenwulf's Avatar
    Join Date
    May 2002
    Location
    Berkeley
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to have logic for handling different exceptions, there are a few ways you can do it. You could use two rescue blocks like:

    Code:
    def add_to_cart
      begin
        # do something
      rescue ActiveRecord::RecordNotFound
        # handle exception
      rescue ActiveRecord::StatementInvalid
        # handle exception
      end
    end
    OR

    Code:
    def add_to_cart
      begin
        # do something
      rescue ActiveRecord::RecordNotFound, ActiveRecord::StatementInvalid
        # handle exception
      end
    end
    You could also just use the superclass of those exceptions, which I believe is [StandardError]

    Code:
    def add_to_cart
      begin
        #do somthing
      rescue StandardError
        # handle exception
      end
    end
    In the above example, any exception that is of the class StandardError or a subclass of StandardError will be caught by the rescue block.

    In your situation, I don't think you need to worry about catching the ActiveRecord::InvalidStatement Exception since you're just passing an id in. If you were building up an elaborate sql statment, then it might be more appropriate.

    It's also common to do:

    rescue Exception => e
    logger.info e.message
    end

    or with mulitple exceptions:

    rescue StandardError, SystemExit => e
    puts e.message
    end

    Hope this helps.

    Regards,
    Steven


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •