require 'bencode'

module Torckapi
  module Response
    class Base
      protected

      def self.bdecode_and_check(data, key)
        begin
          bdecoded_data = BEncode.load(data)
        rescue BEncode::DecodeError
          raise Torckapi::Tracker::MalformedResponseError, "Can't decode '%s'" % data
        end

        raise Torckapi::Tracker::MalformedResponseError, "bdecoded data: '%s'" % bdecoded_data + " didn't contain key: '%s'" % key unless bdecoded_data.is_a? Hash and bdecoded_data.has_key? key

        bdecoded_data
      end
    end
  end
end

require 'bencode'

module Torckapi
  module Response
    class Base
      protected

      def self.bdecode_and_check(data, key)
        begin
          bdecoded_data = BEncode.load(data, :ignore_trailing_junk => true)
        rescue BEncode::DecodeError
          raise Torckapi::Tracker::MalformedResponseError, "Can't decode '%s'" % data
        end

        raise Torckapi::Tracker::MalformedResponseError, "bdecoded data: '%s'" % bdecoded_data + " didn't contain key: '%s'" % key unless bdecoded_data.is_a? Hash and bdecoded_data.has_key? key

        bdecoded_data
      end
    end
  end
end

Allow proper loading of failed Bencoded data
Allow proper loading of failed Bencoded data

When returned with a failure of a response, originally this will raise a
MalformedResponseError well, erroneously. The response is proper, and
should carry on to raise the next error later, for proper debugging
purposes.
