[Groonga-commit] groonga/grntest [master] Report invalid result type error

アーカイブの一覧に戻る

Kouhei Sutou null+****@clear*****
Tue Aug 14 15:08:19 JST 2012


Kouhei Sutou	2012-08-14 15:08:19 +0900 (Tue, 14 Aug 2012)

  New Revision: fb46af62cc1c7425e65897559d5d3269b63f972d
  https://github.com/groonga/grntest/commit/fb46af62cc1c7425e65897559d5d3269b63f972d

  Log:
    Report invalid result type error

  Modified files:
    lib/grntest/tester.rb

  Modified: lib/grntest/tester.rb (+39 -15)
===================================================================
--- lib/grntest/tester.rb    2012-08-14 14:59:47 +0900 (f7a93cd)
+++ lib/grntest/tester.rb    2012-08-14 15:08:19 +0900 (9252794)
@@ -31,6 +31,27 @@ require "grntest/version"
 
 module Grntest
   class Tester
+    class Error < StandardError
+    end
+
+    class NotExist < Error
+      attr_reader :path
+      def initialize(path)
+        @path = path
+        super("<#{path}> doesn't exist.")
+      end
+    end
+
+    class ParseError < Error
+      attr_reader :type, :content, :reason
+      def initialize(type, content, reason)
+        @type = type
+        @content = content
+        @reason = reason
+        super("failed to parse <#{@type}> content: #{reason}: <#{content}>")
+      end
+    end
+
     class << self
       def run(argv=nil)
         argv ||= ARGV.dup
@@ -952,7 +973,13 @@ EOF
         type = options[:type]
         case type
         when "json", "msgpack"
-          status, *values = parse_result(content, type)
+          status = nil
+          values = nil
+          begin
+            status, *values = parse_result(content.chomp, type)
+          rescue ParseError
+            return $!.message
+          end
           normalized_status = normalize_status(status)
           normalized_output_content = [normalized_status, *values]
           normalized_output = JSON.generate(normalized_output_content)
@@ -968,11 +995,19 @@ EOF
       def parse_result(result, type)
         case type
         when "json"
-          JSON.parse(result)
+          begin
+            JSON.parse(result)
+          rescue JSON::ParserError
+            raise ParseError.new(type, result, $!.message)
+          end
         when "msgpack"
-          MessagePack.unpack(result.chomp)
+          begin
+            MessagePack.unpack(result.chomp)
+          rescue MessagePack::UnpackError, NoMemoryError
+            raise ParseError.new(type, result, $!.message)
+          end
         else
-          raise "Unknown type: #{type}"
+          raise ParseError.new(type, result, "unknown type")
         end
       end
 
@@ -1083,17 +1118,6 @@ EOF
         end
       end
 
-      class Error < StandardError
-      end
-
-      class NotExist < Error
-        attr_reader :path
-        def initialize(path)
-          @path = path
-          super("<#{path}> doesn't exist.")
-        end
-      end
-
       attr_reader :context
       def initialize(context=nil)
         @loading = false
-------------- next part --------------
HTML����������������������������...
ダウンロード 



More information about the Groonga-commit mailing list
アーカイブの一覧に戻る