[Groonga-commit] groonga/groonga-command [master] Support correct error message for no record separate comma case

Back to archive index

Kouhei Sutou null+****@clear*****
Tue Nov 27 22:26:20 JST 2012


Kouhei Sutou	2012-11-27 22:26:20 +0900 (Tue, 27 Nov 2012)

  New Revision: bdb7ef5a3dc556031e74abc9159bd5ad91dfdad4
  https://github.com/groonga/groonga-command/commit/bdb7ef5a3dc556031e74abc9159bd5ad91dfdad4

  Log:
    Support correct error message for no record separate comma case

  Modified files:
    lib/groonga/command/parser.rb
    test/test-parser.rb

  Modified: lib/groonga/command/parser.rb (+33 -4)
===================================================================
--- lib/groonga/command/parser.rb    2012-11-27 17:46:54 +0900 (0a715b7)
+++ lib/groonga/command/parser.rb    2012-11-27 22:26:20 +0900 (1f310ff)
@@ -40,6 +40,32 @@ require "groonga/command/truncate"
 module Groonga
   module Command
     class ParseError < Error
+      attr_reader :reason, :location
+      def initialize(reason, before, after)
+        @reason = reason
+        @location = compute_location(before, after)
+        super("#{@reason}:\n#{@location}")
+      end
+
+      private
+      def compute_location(before, after)
+        location = ""
+        if before[-1] == ?\n
+          location << before
+          location << after
+          location << "^"
+        elsif after[0] == ?\n
+          location << before
+          location << "\n"
+          location << " " * before.bytesize + "^"
+          location << after
+        else
+          location << before
+          location << after
+          location << " " * before.bytesize + "^"
+        end
+        location
+      end
     end
 
     class Parser
@@ -201,7 +227,9 @@ module Groonga
                 @load_value_completed = false
                 return
               else
-                raise ParseError, "garbage before value"
+                raise ParseError.new("record separate comma is missing",
+                                     @command.original_source.lines.to_a.last,
+                                     json)
               end
             elsif separator == "]"
               if /\A\s*\z/ =~ json
@@ -251,9 +279,10 @@ module Groonga
         begin
           @json_parser << json
         rescue Yajl::ParseError
-          message = "Invalid JSON: #{$!.message}: <#{json}>:\n"
-          message << @command.original_source
-          raise ParseError.new(message)
+          before_json =****@comma*****_source[0..(-json.bytesize)]
+          message = "invalid JSON: #{$!.message}: <#{json}>:\n"
+          message << before_json
+          raise ParseError.new(message, before_json, json)
         else
         end
       end

  Modified: test/test-parser.rb (+30 -0)
===================================================================
--- test/test-parser.rb    2012-11-27 17:46:54 +0900 (4de167f)
+++ test/test-parser.rb    2012-11-27 22:26:20 +0900 (292d4b8)
@@ -205,6 +205,36 @@ load --table Users
 EOC
           assert_equal(expected_events, @events)
         end
+
+        class ErrorTest < self
+          def test_location
+            message = "record separate comma is missing"
+            before = "{\"_key\": \"alice\", \"name\": \"Alice\"}"
+            after = "\n{\"_key\": \"bob\""
+            error = Groonga::Command::ParseError.new(message, before, after)
+            assert_equal(<<-EOS.chomp, error.message)
+record separate comma is missing:
+{"_key": "alice", "name": "Alice"}
+                                  ^
+{"_key": "bob"
+EOS
+          end
+
+          def test_no_record_separate_comma
+            message = "record separate comma is missing"
+            before = "{\"_key\": \"alice\", \"name\": \"Alice\"}"
+            after = "\n{\"_key\": \"bob\""
+            error = Groonga::Command::ParseError.new(message, before, after)
+            assert_raise(error) do
+              @parser << <<-EOC
+load --table Users
+[
+{"_key": "alice", "name": "Alice"}
+{"_key": "bob",   "name": "Bob"}
+EOC
+            end
+          end
+        end
       end
 
       class CommentTest < self
-------------- next part --------------
HTML����������������������������...
다운로드 



More information about the Groonga-commit mailing list
Back to archive index