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
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.
  • Loading branch information
abetomo committed Oct 15, 2024
1 parent c3ee1de commit 1947f26
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/groonga/client/response/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,16 @@ def parse(command, raw_response)
case command.output_type
when :json
callback = command["callback"]
json_parse = -> (json) do
JSON.parse(json)
rescue JSON::ParserError => err
raise err.exception("source=`#{json}`")
end
if callback and
/\A#{Regexp.escape(callback)}\((.+)\);\z/ =~ raw_response
response = JSON.parse($1)
response = json_parse.call($1)
else
response = JSON.parse(raw_response)
response = json_parse.call(raw_response)
end
if response.is_a?(::Array)
header, body = response
Expand Down

0 comments on commit 1947f26

Please sign in to comment.