mardi 2 octobre 2018

Ruby: Exception is not getting catched in rescue block (retry)

The exception ActiveRecord::StaleObjectError.new is not getting catch in rescue block (retry). Can you suggest why I am not able to catch Exception?

  attr_accessible :original_updated_at

  def original_updated_at
    @original_updated_at || updated_at.to_f
  end

   def set_total_and_buckets_used
    tries = 0
    begin
      if total_approved - attributes["total_amount_used"] < 0
          raise Exception.new("Total amount used is greater than approved")
      elsif self.updated_at.to_f == original_updated_at
        @original_updated_at = nil
        raise ActiveRecord::StaleObjectError.new("Car is changed while you were editing")
      else
        self.with_lock do
          attributes["updated_at"] = Time.now.utc
          Product.where(:id => self.id).update_all(attributes)
          self.attributes = attributes
        end
      end
    rescue ActiveRecord::StaleObjectError => e
      if tries < 3
        tries += 1
        retry
      else
        raise Exception.new(timeout.inspect)
      end
    end
    self.reload
    attributes
  end

Aucun commentaire:

Enregistrer un commentaire