class Step < ApplicationRecord
  belongs_to :step_by_step_page
  validates_presence_of :step_by_step_page
  validates :title, :logic, presence: true
  has_many :link_reports, :dependent => :destroy

  def broken_links?
    broken_links.present? && broken_links.any?
  end

  def link_report?
    most_recent_batch.present?
  end

  def links_last_checked_date
    most_recent_batch.created_at if link_report?
  end

  def broken_links
    collect_broken_links unless most_recent_batch.nil?
  end

  def request_broken_links
    LinkReport.new(step_id: self.id).create_record
  end

private

  def collect_broken_links
    return [] if batch_link_report.blank?

    batch_link_report.links.keep_if do |link|
      link.fetch('status') == 'broken'
    end
  end

  def batch_link_report
    begin
      @batch_link_report ||= Services.link_checker_api.get_batch(batch_link_report_id)
    rescue GdsApi::HTTPServerError
      nil
    end
  end

  def batch_link_report_id
    most_recent_batch.batch_id
  end

  def most_recent_batch
    LinkReport.where(step_id: self.id).last
  end
end

class Step < ApplicationRecord
  belongs_to :step_by_step_page
  validates_presence_of :step_by_step_page
  validates :title, :logic, presence: true
  has_many :link_reports, :dependent => :destroy

  def broken_links?
    broken_links.present? && broken_links.any?
  end

  def link_report?
    most_recent_batch.present?
  end

  def links_last_checked_date
    most_recent_batch.created_at if link_report?
  end

  def broken_links
    collect_broken_links unless most_recent_batch.nil?
  end

  def request_broken_links
    LinkReport.new(step_id: self.id).create_record
  end

private

  def collect_broken_links
    return [] if batch_link_report.blank?

    batch_link_report.links.keep_if do |link|
      link.fetch('status') == 'broken'
    end
  end

  def batch_link_report
    begin
      @batch_link_report ||= Services.link_checker_api.get_batch(batch_link_report_id)
    rescue GdsApi::HTTPServerError, GdsApi::HTTPNotFound
      nil
    end
  end

  def batch_link_report_id
    most_recent_batch.batch_id
  end

  def most_recent_batch
    LinkReport.where(step_id: self.id).last
  end
end

Handle missing batch link reports
Handle missing batch link reports

This commit adds an exception handler for 404 errors from link-checker-api. This can happen if the batch link report has been removed, which happens autmoatically on a schedule to prevent build-up of old reports. The app should handle this case.
