<commit_before>module David
  class ShowExceptions
    def initialize(app)
      @app = app
    end

    def call(env)
      dup._call(env)
    end

    def _call(env)
      @app.call(env)
    rescue Exception => exception
      if env['action_dispatch.show_exceptions'] == false
        raise exception
      else
        render_exception(env, exception)
      end
    end

    private

    def render_exception(env, exception)
      body = {
        error: exception.class.to_s,
        message: exception.message
      }
      
      body = body.to_json

      [500,
        {
          'Content-Type' => 'application/json',
          'Content-Length' => body.bytesize.to_s
        },
        [body]
      ]
    end
  end
end<commit_msg>Also log exceptions in ShowExceptions middleware.<commit_after>module David
  class ShowExceptions
    def initialize(app)
      @app = app
    end

    def call(env)
      dup._call(env)
    end

    def _call(env)
      @app.call(env)
    rescue Exception => exception
      if env['action_dispatch.show_exceptions'] == false
        raise exception
      end

      @env = env

      render_exception(exception)
    end

    private

    def render_exception(exception)
      body = {
        error: exception.class.to_s,
        message: exception.message
      }

      log(:info, [body[:error], body[:message]].join("\n"))
      
      body = body.to_json

      [500,
        {
          'Content-Type' => 'application/json',
          'Content-Length' => body.bytesize.to_s
        },
        [body]
      ]
    end

    def log(level, message)
      @logger ||= @env['rack.logger']
      @logger.send(level, message) if @logger
    end
  end
end
{'fraction_non_alphanumeric': 0.10330578512396695, 'fraction_numerical': 0.0035419126328217238, 'mean_word_length': 1.8583473861720068, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '3801743', 'n_tokens_mistral': 527, 'n_tokens_neox': 508, 'n_words': 114}