Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

response: parse: Include input JSON in Error, on JSON parsing errors #32

Merged
merged 5 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading