<commit_before># frozen_string_literal: true
class ApplicationController < ActionController::Base
  API_HEADER = 'X-Octobox-API'

  protect_from_forgery with: :exception, unless: -> { octobox_api_request? }
  helper_method :current_user, :logged_in?, :initial_sync?
  before_action :authenticate_user!

  before_bugsnag_notify :add_user_info_to_bugsnag if Rails.env.production?

  rescue_from Octokit::Unauthorized, Octokit::Forbidden do |exception|
    handle_exception(exception, :service_unavailable, I18n.t("exceptions.octokit.unauthorized"))
  end
  rescue_from Octokit::BadGateway, Octokit::ServiceUnavailable do |exception|
    handle_exception(exception, :service_unavailable, I18n.t("exceptions.octokit.unavailable"))
  end
  rescue_from Faraday::ClientError do |exception|
    handle_exception(exception, :service_unavailable, I18n.t("exceptions.faraday.connection_failed"))
  end

  private

  def add_user_info_to_bugsnag(notification)
    return unless logged_in?

    notification.user = {
      id: current_user.id,
      login: current_user.github_login
    }
  end

  def authenticate_user!
    return if logged_in?
    respond_to do |format|
      format.html { redirect_to root_path }
      format.json { render json: { "error" => "unauthorized" }, status: :unauthorized }
    end
  end

  def current_user
    @current_user ||= authenticate_with_http_token { |token, _| User.find_by(api_token: token) }
    @current_user ||= (cookies.permanent.signed[:user_id] && User.find_by(id: cookies.permanent.signed[:user_id]))
  end

  def logged_in?
    !current_user.nil?
  end

  def initial_sync?
    current_user.last_synced_at.nil?
  end

  def octobox_api_request?
    request.headers[API_HEADER].present?
  end

  def handle_exception(exception, status, message='')
    logger.error("[ERROR] #{controller_name}##{action_name} \
                 #{current_user.try(:github_login)} - #{exception.class}: #{exception.message}")

    flash[:error] = "#{message}. Please try again."

    respond_to do |format|
      format.html { redirect_back fallback_location: root_path }
      format.json { head status }
    end
  end
end
<commit_msg>Rescue Octokit::InternalServerErrors (#690)<commit_after># frozen_string_literal: true
class ApplicationController < ActionController::Base
  API_HEADER = 'X-Octobox-API'

  protect_from_forgery with: :exception, unless: -> { octobox_api_request? }
  helper_method :current_user, :logged_in?, :initial_sync?
  before_action :authenticate_user!

  before_bugsnag_notify :add_user_info_to_bugsnag if Rails.env.production?

  rescue_from Octokit::Unauthorized, Octokit::Forbidden do |exception|
    handle_exception(exception, :service_unavailable, I18n.t("exceptions.octokit.unauthorized"))
  end
  rescue_from Octokit::BadGateway, Octokit::ServiceUnavailable, Octokit::InternalServerError do |exception|
    handle_exception(exception, :service_unavailable, I18n.t("exceptions.octokit.unavailable"))
  end
  rescue_from Faraday::ClientError do |exception|
    handle_exception(exception, :service_unavailable, I18n.t("exceptions.faraday.connection_failed"))
  end

  private

  def add_user_info_to_bugsnag(notification)
    return unless logged_in?

    notification.user = {
      id: current_user.id,
      login: current_user.github_login
    }
  end

  def authenticate_user!
    return if logged_in?
    respond_to do |format|
      format.html { redirect_to root_path }
      format.json { render json: { "error" => "unauthorized" }, status: :unauthorized }
    end
  end

  def current_user
    @current_user ||= authenticate_with_http_token { |token, _| User.find_by(api_token: token) }
    @current_user ||= (cookies.permanent.signed[:user_id] && User.find_by(id: cookies.permanent.signed[:user_id]))
  end

  def logged_in?
    !current_user.nil?
  end

  def initial_sync?
    current_user.last_synced_at.nil?
  end

  def octobox_api_request?
    request.headers[API_HEADER].present?
  end

  def handle_exception(exception, status, message='')
    logger.error("[ERROR] #{controller_name}##{action_name} \
                 #{current_user.try(:github_login)} - #{exception.class}: #{exception.message}")

    flash[:error] = "#{message}. Please try again."

    respond_to do |format|
      format.html { redirect_back fallback_location: root_path }
      format.json { head status }
    end
  end
end

{'fraction_non_alphanumeric': 0.10760073260073261, 'fraction_numerical': 0.003434065934065934, 'mean_word_length': 4.5585241730279895, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '2260124', 'n_tokens_mistral': 1450, 'n_tokens_neox': 1408, 'n_words': 267}