Skip to content

Commit

Permalink
response: parse: Include input JSON in Error, on JSON parsing errors (#…
Browse files Browse the repository at this point in the history
…32)

For example, when the input is `["header", :{"return_code":-77}}`, a
parse error.
The error is then as follows.
```
`parse': unexpected token at ':{"return_code":-77}}' (JSON::ParserError)
```

Include the input JSON in the error since the error does not include the
full input JSON, making it difficult to debug.

---------

Co-authored-by: Sutou Kouhei <[email protected]>
  • Loading branch information
abetomo and kou authored Oct 16, 2024
1 parent c3ee1de commit 25f4b4a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 19 deletions.
30 changes: 30 additions & 0 deletions lib/groonga/client/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,35 @@ module Groonga
class Client
class Error < StandardError
end

class ErrorResponse < Error
attr_reader :response
def initialize(response)
@response = response
command = @response.command
status_code = @response.status_code
error_message = @response.error_message
message = "failed to execute: "
message << "#{command.command_name}: #{status_code}: "
message << "<#{error_message}>: "
message << command.to_command_format
super(message)
end
end

class InvalidResponse < Error
attr_reader :command
attr_reader :raw_response
def initialize(command, raw_response, error_message)
@command = command
@raw_response = raw_response
message = +"invalid response: "
message << "#{command.command_name}: "
message << "#{error_message}: "
message << "<#{command.to_command_format}>: "
message << "<#{raw_response}>"
super(message)
end
end
end
end
20 changes: 3 additions & 17 deletions lib/groonga/client/request/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,9 @@
module Groonga
class Client
module Request
class Error < Client::Error
end

class ErrorResponse < Error
attr_reader :response
def initialize(response)
@response = response
command = @response.command
status_code = @response.status_code
error_message = @response.error_message
message = "failed to execute: "
message << "#{command.command_name}: #{status_code}: "
message << "<#{error_message}>: "
message << command.to_command_format
super(message)
end
end
# For backward compatibility
Error = Client::Error
ErrorResponse = Client::ErrorResponse
end
end
end
11 changes: 9 additions & 2 deletions lib/groonga/client/response/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,16 @@ def parse(command, raw_response)
callback = command["callback"]
if callback and
/\A#{Regexp.escape(callback)}\((.+)\);\z/ =~ raw_response
response = JSON.parse($1)
json = $1
else
response = JSON.parse(raw_response)
json = raw_response
end
begin
response = JSON.parse(json)
rescue JSON::ParserError => error
raise InvalidResponse.new(command,
raw_response,
"invalid JSON: #{error}")
end
if response.is_a?(::Array)
header, body = response
Expand Down
15 changes: 15 additions & 0 deletions test/response/test-base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,20 @@ def test_jsonp
response = Groonga::Client::Response::Base.parse(command, raw_response)
assert_equal(1396012478, response.body["start_time"])
end

def test_invalid_json
command = Groonga::Command::Base.new("cancel")
raw_response = '["header", :{"return_code":-77}}'
begin
JSON.parse(raw_response)
rescue JSON::ParserError => error
parse_error_message = "invalid JSON: #{error}"
end
error = Groonga::Client::InvalidResponse.new(command, raw_response, parse_error_message)

assert_raise(error) do
Groonga::Client::Response::Base.parse(command, raw_response)
end
end
end
end

0 comments on commit 25f4b4a

Please sign in to comment.