<commit_before>#require 'action_dispatch/http/mime_type'

module Intermodal
  module Rack
    class Rescue
      include ActionController::Rescue

      rescue_from Exception do |exception|
        [500, {}, [ exception.message ] ]
      end

      rescue_from ActiveRecord::RecordNotFound do |exception|
        [404, {}, [ 'Not Found' ] ]
      end

      # TODO: Hack. Untested.
      if defined? ActiveResource::ResourceNotFound
        rescue_from ActiveResource::ResourceNotFound do |exception|
          [404, {}, [ 'Not Found' ] ]
        end
      end

      rescue_from MultiJson::DecodeError do |exception|
        [400, { 'Content-Type' => content_type(:json) }, { :parse_error => exception.message }.to_json]
      end

      rescue_from 'REXML::ParseException' do |exception|
        [400, { 'Content-Type' => content_type(:xml) }, { :parse_error => exception.message }.to_xml]
      end

      def content_type(format)
        "#{Mime::Type.lookup_by_extension(format)}; charset=utf-8"
      end

      def initialize(app, &block)
        @app = app
        self.class.instance_eval(&block) if block_given?
      end

      def call(env)<commit_msg>[RACK/UNTESTED] Added support for exception backtraces on 500 errors if running under Rails<commit_after>#require 'action_dispatch/http/mime_type'

module Intermodal
  module Rack
    class Rescue
      include ActionController::Rescue

      rescue_from Exception do |exception|
        if defined? Rails and Rails.env == 'production'
          [500, {}, [ "Unexpected error. Please contact support." ] ]
        else
          [500, {}, [ exception.message, "\n\n", exception.backtrace ] ]
        end
      end

      rescue_from ActiveRecord::RecordNotFound do |exception|
        [404, {}, [ 'Not Found' ] ]
      end

      # TODO: Hack. Untested.
      if defined? ActiveResource::ResourceNotFound
        rescue_from ActiveResource::ResourceNotFound do |exception|
          [404, {}, [ 'Not Found' ] ]
        end
      end

      rescue_from MultiJson::DecodeError do |exception|
        [400, { 'Content-Type' => content_type(:json) }, { :parse_error => exception.message }.to_json]
      end

      rescue_from 'REXML::ParseException' do |exception|
        [400, { 'Content-Type' => content_type(:xml) }, { :parse_error => exception.message }.to_xml]
      end

      def content_type(format)
        "#{Mime::Type.lookup_by_extension(format)}; charset=utf-8"
      end

      def initialize(app, &block)
        @app = app
        self.class.instance_eval(&block) if block_given?
      end

      def call(env)
{'fraction_non_alphanumeric': 0.11669242658423493, 'fraction_numerical': 0.014683153013910355, 'mean_word_length': 2.661951909476662, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '217671', 'n_tokens_mistral': 798, 'n_tokens_neox': 746, 'n_words': 183}