lib/quandl/client/middleware/parse_json.rb in quandl_client-0.1.14 vs lib/quandl/client/middleware/parse_json.rb in quandl_client-0.1.15
- old
+ new
@@ -12,11 +12,11 @@
parse(env[:body], env)
end
end
def parse(body, env)
- json = parse_json(body)
+ json = parse_json(body, env)
errors = json.delete(:errors) || {}
metadata = json.delete(:metadata) || {}
# collect some response data
metadata.merge!({
status: env[:status],
@@ -30,22 +30,26 @@
}
env[:status] = 200
object
end
- def parse_json(body = nil)
+ def parse_json(body = nil, env)
body ||= '{}'
- message = "Response from the API must behave like a Hash or an Array (last JSON response was #{body.inspect})"
-
json = begin
Yajl.load(body, :symbolize_keys => true)
rescue Yajl::ParseError
- { id: 1, errors: { parse_error: message } }
-
- # raise Her::Errors::ParseError, message
+ nil
end
- # raise Her::Errors::ParseError, message unless json.is_a?(Hash) or json.is_a?(Array)
-
+ # invalid json body?
+ if json.blank?
+ # fallback to error message
+ json = {
+ id: 1,
+ errors: {
+ parse_error: "Quandl::Client::ParseJSON error. status: #{env[:status]}, body: #{body.inspect}"
+ }
+ }
+ end
json
end
end
\ No newline at end of file