<commit_before>class Viewer::PagesController < ViewerController

  before_filter :cors_check

  # rescue_from ActionController::RoutingError, :with => :error_404
  # rescue_from ActionView::MissingTemplate, :with => :error_500

  if Rails.env.production?
    rescue_from ActionController::RoutingError do |e|
      error_404(e)
    end

    rescue_from ActionView::MissingTemplate do |e|
      error_500(e)
    end
  end

  def home
    @current_page = current_site.home_page
    if @current_page.nil? || (@current_page.draft? && Rails.env.production?)
      not_found
    else
      show
    end
  end

  def show
    if @current_page.nil?
      slug = params[:page_path].split('/').last
      @current_page = current_site.pages.find_by_slug(slug)
    end
    if current_page.nil?
      rt_slug = params[:page_path].split('/').first
      rt = current_site.resource_types.where(:slug => [
        rt_slug.singularize, rt_slug.pluralize
      ]).first
      if rt.nil? || !rt.has_show_view?
        not_found
      else
        @current_resource_type = rt
        if rt_slug == slug
          file = rt_slug.pluralize
        else
          file = rt_slug.singularize
          @current_resource = rt.resources.find_by_slug(slug)
        end
        render(
          "viewer/#{current_site.slug}/#{file}",
          :layout => @layout
        )
      end
    elsif current_template.has_show_view?
      resolve_layout
      render(
        "viewer/#{current_site.slug}/#{current_page_template}",
        :layout => @layout
      )
    else
      not_found
    end
  end

  private

    def error_404(error)
      if current_site && current_site.title.present?
        @q = request.path.split('/').last.humanize.split('.').first.downcase
        @results = current_site.pages.search_content(@q).to_a.first(10)
        dir = Rails.root.join('app', 'views', 'viewer', current_site.slug).to_s
        if(
          File.exists?("#{dir}/404.html.erb") ||
          File.exists?("#{dir}/404.html")
        )
          render(
            "viewer/#{current_site.slug}/404",
            :layout => false,
            :formats => [:html]
          )
        else
          render '404', :formats => [:html]
        end
      else
        render :file => 'public/404.html'
      end
      Error._404(current_site, error, request, current_user)
    end

    def error_500(error)
      if current_site && current_site.title.present?
        dir = Rails.root.join('app', 'views', 'viewer', current_site.slug).to_s
        if(
          File.exists?("#{dir}/500.html.erb") ||
          File.exists?("#{dir}/500.html")
        )
          render(
            "viewer/#{current_site.slug}/500",
            :layout => false,
            :formats => [:html]
          )
        else
          render '500', :formats => [:html]
        end
      else
        render '500', :formats => [:html]
      end
      Error._500(current_site, error, request, current_user)
    end

    def resolve_layout
      if params[:layout] && params[:layout].to_bool == false
        @layout = false
      else
        @layout = "viewer/#{current_site.slug}"
      end
    end

    def cors_check
      headers['Access-Control-Allow-Origin'] = '*'
      headers['Access-Control-Allow-Methods'] = 'POST, PUT, DELETE, GET, OPTIONS'
      headers['Access-Control-Request-Method'] = '*'
      headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization'
    end

end
<commit_msg>an attempt to fix sending unecessary error notifications<commit_after>class Viewer::PagesController < ViewerController

  before_filter :cors_check

  # rescue_from ActionController::RoutingError, :with => :error_404
  # rescue_from ActionView::MissingTemplate, :with => :error_500

  if Rails.env.production?
    rescue_from ActionController::RoutingError do |e|
      error_404(e)
    end

    rescue_from ActionView::MissingTemplate do |e|
      error_500(e)
    end
  end

  def home
    @current_page = current_site.home_page
    if @current_page.nil? || (@current_page.draft? && Rails.env.production?)
      not_found
    else
      show
    end
  end

  def show
    if @current_page.nil?
      slug = params[:page_path].split('/').last
      @current_page = current_site.pages.find_by_slug(slug)
    end
    if current_page.nil?
      rt_slug = params[:page_path].split('/').first
      rt = current_site.resource_types.where(:slug => [
        rt_slug.singularize, rt_slug.pluralize
      ]).first
      if rt.nil? || !rt.has_show_view?
        not_found
      else
        @current_resource_type = rt
        if rt_slug == slug
          file = rt_slug.pluralize
        else
          file = rt_slug.singularize
          @current_resource = rt.resources.find_by_slug(slug)
        end
        render(
          "viewer/#{current_site.slug}/#{file}",
          :layout => @layout
        )
      end
    elsif current_template.has_show_view?
      resolve_layout
      render(
        "viewer/#{current_site.slug}/#{current_page_template}",
        :layout => @layout
      )
    else
      not_found
    end
  end

  private

    def error_404(error)
      if current_site && current_site.title.present?
        @q = request.path.split('/').last.humanize.split('.').first.downcase
        @results = current_site.pages.search_content(@q).to_a.first(10)
        dir = Rails.root.join('app', 'views', 'viewer', current_site.slug).to_s
        if(
          File.exists?("#{dir}/404.html.erb") ||
          File.exists?("#{dir}/404.html")
        )
          render(
            "viewer/#{current_site.slug}/404",
            :layout => false,
            :formats => [:html]
          )
        else
          render 'previewer/404', :formats => [:html]
        end
      else
        render :file => 'public/404.html'
      end
      Error._404(current_site, error, request, current_user)
    end

    def error_500(error)
      if current_site && current_site.title.present?
        dir = Rails.root.join('app', 'views', 'viewer', current_site.slug).to_s
        if(
          File.exists?("#{dir}/500.html.erb") ||
          File.exists?("#{dir}/500.html")
        )
          render(
            "viewer/#{current_site.slug}/500",
            :layout => false,
            :formats => [:html]
          )
        else
          render 'previewer/500', :formats => [:html]
        end
      else
        render 'previewer/500', :formats => [:html]
      end
      Error._500(current_site, error, request, current_user)
    end

    def resolve_layout
      if params[:layout] && params[:layout].to_bool == false
        @layout = false
      else
        @layout = "viewer/#{current_site.slug}"
      end
    end

    def cors_check
      headers['Access-Control-Allow-Origin'] = '*'
      headers['Access-Control-Allow-Methods'] = 'POST, PUT, DELETE, GET, OPTIONS'
      headers['Access-Control-Request-Method'] = '*'
      headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization'
    end

end

{'fraction_non_alphanumeric': 0.12966908109643516, 'fraction_numerical': 0.015906831415992046, 'mean_word_length': 2.2082004555808656, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5617247', 'n_tokens_mistral': 2304, 'n_tokens_neox': 2232, 'n_words': 407}