You get a TypeError . Occurs when you pass the wrong kind of argument to certain methods. You can save it like this:
begin parsed_response = JSON.parse(response) rescue JSON::ParserError, TypeError => e puts e end
I would not recommend this, though. The reason you get a TypeError is because JSON.parse requires a String object, and you passed it an HTTParty::Response object. Try giving it a String object instead. eg:
parsed_response = JSON.parse(response&.body || "{}")
Thus, the nil value in response or response.body returns a usable object. In this example, the object is an empty Hash , although you can replace the JSON string with any value that you want to represent with the value "not there" (for example, "[]" or "null" ).
source share