<commit_before>require 'juggler/juggler_helper.rb'
require 'juggler/exception_handler.rb'
require 'juggler/exception_notifier.rb'

module Juggler

  def self.included(base)
    # only add in the controller-specific methods if the including class is one
    if class_has_ancestor?(base, ActionController::Base)
      base.send(:include, ControllerMethods)
    end
  end

  # module of instance methods to be added to the class including Juggler
  # only if the including class is a rails controller class
  #-----------------------------------------------------------------------------
  module ControllerMethods

    # called by rails if the exception has already been handled (e.g. by
    # calling the rescue_from method in a controller and rendering a response)
    #---------------------------------------------------------------------------
    def rescue_with_handler(exception)
      to_return = super
      if to_return
        handle_exception(exception, :request => request,
                                    :render_errors => false)
      end
      to_return
    end

    # conditionally adding these methods so that the exception handling is
    # only activated if configured to do so. these are the methods that rails
    # looks for to override default behavior
    #---------------------------------------------------------------------------
    if Juggler::ExceptionHandler.config[:handle_public_errors]
      def rescue_action_in_public(exception)
        handle_exception(exception, :request => request,
                                    :render_errors => true)
      end
    end
    if Juggler::ExceptionHandler.config[:handle_local_errors]
      def rescue_action_locally(exception)
        handle_exception(exception, :request => request,
                                    :render_errors => true)
      end
    end

    # extract a hash of relevant (and serializable) parameters from a request
    #---------------------------------------------------------------------------
    def request_data_from_request(request)
      return nil if request.nil?

      request_data = {}
      request.env.each_pair do |k,v|
        next if skip_request_env?(k)

        if self.respond_to?(:filter_parameters)
          request_data.merge! self.send(:filter_parameters, k => v)
        else
          request_data.merge! k => v
        end
      end

      params = {}
      if self.respond_to?(:filter_parameters)
        params.merge!(
                      filter_parameters(request.env['action_controller.request.query_parameters'])
                      )
        params.merge!(
                      filter_parameters(request.env['action_controller.request.request_parameters'])
                      )
      else
        params.merge! request.env['action_controller.request.query_parameters']
        params.merge! request.env['action_controller.request.request_parameters']
      end

      request_data.merge! :params => params unless params.blank?

      return request_data
    end

    # determine if the request env param should be ommitted from the request
    # data object, as specified in config (either for aesthetic reasons or
    # because the param won't serialize well).
    #---------------------------------------------------------------------------
    def skip_request_env?(request_param)
      skip_env = false
      Juggler::ExceptionHandler.config[:request_env_to_skip].each do |pattern|
        if (pattern.is_a?(String) && pattern == request_param) ||
           (pattern.is_a?(Regexp) && pattern =~ request_param)
          skip_env = true
          break
        end
      end

      return skip_env
    end

    # select the proper file to render and do so. if the usual places don't
    # turn up an appropriate template (see README), then fall back on
    # an app-specific default error page or the ultimate back up gem default
    # page.
    #---------------------------------------------------------------------------
    def render_error_template(exception, status_code)
      file_path = get_view_path_for_exception(exception, status_code)
      
      # if that didn't work, fall back on configured app-specific default
      if file_path.blank? || !File.exists?(file_path)
        file_path = config[:default_error_template]

        log_error(["Could not find an error template in the usual places " +
                  "for exception #{exception.class}, status code " +
                  "#{status_code}.",
                  "Trying to default to app-specific default: '#{file_path}'"])
      end

      # as a last resort, just render the gem's 500 error
      if file_path.blank? || !File.exists?(file_path)
        file_path = config[:absolute_last_resort_default_error_template]

        log_error("Still no template found. Using gem default of " +
                  file_path)
      end

      render :file => file_path,
             :status => status_code
    end

    # select the appropriate view path for the exception/status code. see
    # README or the code for the different attempts that are made to find
    # a template.
    #---------------------------------------------------------------------------
    def get_view_path_for_exception(exception, status_code)

      # maintenance note: this method has lots of RETURN statements, so be
      # a little careful, but basically, it returns as soon as it finds a
      # file match, so there shouldn't be any processing to perform after
      # a match is found. any such logic does not belong in this method

      if exception.is_a?(Class)
        exception_class = exception
      else
        exception_class = exception.class<commit_msg>got things cleaned up but one remaining problem: the switch to turn on/off exception handling for local requests isnt working any more...seems that something has changed and now the controller methods are being defined before the initializer has a chance to run? need to figure that out...<commit_after>require 'juggler/juggler_helper.rb'
require 'juggler/exception_handler.rb'
require 'juggler/exception_notifier.rb'

module Juggler

  def self.included(base)
    # only add in the controller-specific methods if the including class is one
    if class_has_ancestor?(base, ActionController::Base)
      base.send(:include, ControllerMethods)
    end
  end

  # module of instance methods to be added to the class including Juggler
  # only if the including class is a rails controller class
  #-----------------------------------------------------------------------------
  module ControllerMethods

    # called by rails if the exception has already been handled (e.g. by
    # calling the rescue_from method in a controller and rendering a response)
    #---------------------------------------------------------------------------
    def rescue_with_handler(exception)
      to_return = super
      if to_return
        handle_exception(exception, :request => request,
                                    :render_errors => false)
      end
      to_return
    end


    # conditionally adding these methods so that the exception handling is
    # only activated if configured to do so. these are the methods that rails
    # looks for to override default behavior
    #---------------------------------------------------------------------------
    if Juggler::ExceptionHandler.config[:handle_public_errors]
      puts("Controllers will be configured to handle exceptions " +
           "in public with Juggler")
      def rescue_action_in_public(exception)
        handle_exception(exception, :request => request,
                                    :render_errors => true)
      end
    else
      puts("Controllers will NOT be configured to handle exceptions " +
           "in public with Juggler")
    end
    if Juggler::ExceptionHandler.config[:handle_local_errors]
      puts("Controllers will be configured to handle exceptions " +
           "locally with Juggler")
      def rescue_action_locally(exception)
        handle_exception(exception, :request => request,
                                    :render_errors => true)
      end
    else
      puts("Controllers will NOT be configured to handle exceptions " +
           "locally with Juggler")
    end


    # extract a hash of relevant (and serializable) parameters from a request
    #---------------------------------------------------------------------------
    def request_data_from_request(request)
      return nil if request.nil?

      request_data = {}
      request.env.each_pair do |k,v|
        next if skip_request_env?(k)

        if self.respond_to?(:filter_parameters)
          request_data.merge! self.send(:filter_parameters, k => v)
        else
          request_data.merge! k => v
        end
      end

      params = {}
      if self.respond_to?(:filter_parameters)
        params.merge!(
                      filter_parameters(request.env['action_controller.request.query_parameters'])
                      )
        params.merge!(
                      filter_parameters(request.env['action_controller.request.request_parameters'])
                      )
      else
        params.merge! request.env['action_controller.request.query_parameters']
        params.merge! request.env['action_controller.request.request_parameters']
      end

      request_data.merge! :params => params unless params.blank?

      return request_data
    end

    # determine if the request env param should be ommitted from the request
    # data object, as specified in config (either for aesthetic reasons or
    # because the param won't serialize well).
    #---------------------------------------------------------------------------
    def skip_request_env?(request_param)
      skip_env = false
      Juggler::ExceptionHandler.config[:request_env_to_skip].each do |pattern|
        if (pattern.is_a?(String) && pattern == request_param) ||
           (pattern.is_a?(Regexp) && pattern =~ request_param)
          skip_env = true
          break
        end
      end

      return skip_env
    end

    # select the proper file to render and do so. if the usual places don't
    # turn up an appropriate template (see README), then fall back on
    # an app-specific default error page or the ultimate back up gem default
    # page.
    #---------------------------------------------------------------------------
    def render_error_template(exception, status_code)
      file_path = get_view_path_for_exception(exception, status_code)
      
      # if that didn't work, fall back on configured app-specific default
      if file_path.blank? || !File.exists?(file_path)
        file_path = config[:default_error_template]

        log_error(["Could not find an error template in the usual places " +
                  "for exception #{exception.class}, status code " +
                  "#{status_code}.",
                  "Trying to default to app-specific default: '#{file_path}'"])
      end

      # as a last resort, just render the gem's 500 error
      if file_path.blank? || !File.exists?(file_path)
        file_path = config[:absolute_last_resort_default_error_template]

        log_error("Still no template found. Using gem default of " +
                  file_path)
      end

      log_error("Will render error template: '#{file_path}'")

      render :file => file_path,
             :status => status_code
    end

    # select the appropriate view path for the exception/status code. see
    # README or the code for the different attempts that are made to find
    # a template.
    #---------------------------------------------------------------------------
    def get_view_path_for_exception(exception, status_code)

      # maintenance note: this method has lots of RETURN statements, so be
      # a little careful, but basically, it returns as soon as it finds a
      # file match, so there shouldn't be any processing to perform after
      # a match is found. any such logic does not belong in this method

      if exception.is_a?(Class)
        exception_class = exception
      else
        exception_class = exception.class
{'fraction_non_alphanumeric': 0.1502103439742638, 'fraction_numerical': 0.0004949269982677555, 'mean_word_length': 1.8839200761179828, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5140053', 'n_tokens_mistral': 2936, 'n_tokens_neox': 2868, 'n_words': 988}