<commit_before># encoding: utf-8

module Hyperdrive
  module Middleware
    class Error
      include Hyperdrive::Values
      
      def initialize(app)
        @app = app
      end

      def call(env)
        @app.call(env)
      rescue Hyperdrive::Errors::HTTPError => e
        status = e.http_status_code
        headers = { 'Content-Type' => 'application/json',
                    'Allow' => supported_request_methods.join(', ') }
        body = Oj.dump(error_message(e), mode: :compat)
        [status, headers, [body]]
      end

      private
      
      def error_message(e)
        {
          _links: { root: { href: '/', title: 'API Root' } },
          error: { 
            type: "#{e.class.to_s.split('::').last}",
            message: e.message
          }
        }
      end
    end
  end
end<commit_msg>switches to multi_json and rescues all errors<commit_after># encoding: utf-8

module Hyperdrive
  module Middleware
    class Error
      include Hyperdrive::Values

      def initialize(app)
        @app = app
      end

      def call(env)
        @app.call(env)
      rescue => e
        status = e.respond_to?(:http_status_code) ? e.http_status_code : 500
        headers = { 'Content-Type' => 'application/json' }
        [status, headers, [json_error(e)]]
      end

      private

      def json_error(e)
        MultiJson.dump({
          _links: { root: { href: '/', title: 'API Root' } },
          error: {
            type: "#{e.class.to_s.split('::').last}",
            message: e.message
          }
        })
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.12412919569347688, 'fraction_numerical': 0.0031665611146295125, 'mean_word_length': 1.7430555555555556, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '503936', 'n_tokens_mistral': 495, 'n_tokens_neox': 482, 'n_words': 111}