Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

consistently raise Pusher::Error when JSON encoding or decoding problems occur #37

Closed
wants to merge 6 commits into from

2 participants

@markburns

also log messages consistently

@mloughran

What's the real purpose of logging JSON parse errors? Is it not rather presumptuous to assume they are at error level? This is surely application dependant.

Seems convenient to be able to catch JSON errors (which may be cased by users) separately from errors caused by one's networking environment for example.

I'm tempted to remove the json logging entirely rather than go this way.

@markburns

Good point. I was just applying the principle of consistency. I've just removed it here, the DRYing up made that a one line change

@markburns

Anyway, the main gist of this pull request is about having a library that consistently raises Pusher errors, rather than the logging

@mloughran

I'm sorry that this has been left hanging, but I don't agree with this change. Closing.

@mloughran mloughran closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
14 lib/pusher.rb
@@ -43,8 +43,22 @@ def logger
end
end
+ def encode_json data
+ process_json :encode, data
+ end
+
+ def decode_json body
+ process_json :decode, body
+ end
+
private
+ def process_json meth, data
+ MultiJson.send meth, data
+ rescue
+ raise Pusher::Error, "Could not #{meth} JSON: #{data.inspect}"
+ end
+
def default_client
@default_client ||= Pusher::Client.new
end
View
2  lib/pusher/channel.rb
@@ -137,7 +137,7 @@ def authentication_string(socket_id, custom_string = nil)
# @private Custom data is sent to server as JSON-encoded string
#
def authenticate(socket_id, custom_data = nil)
- custom_data = MultiJson.encode(custom_data) if custom_data
+ custom_data = Pusher.encode_json(custom_data) if custom_data
auth = authentication_string(socket_id, custom_data)
r = {:auth => auth}
r[:channel_data] = custom_data if custom_data
View
7 lib/pusher/client.rb
@@ -283,12 +283,7 @@ def trigger_params(channels, event_name, data, params)
when String
data
else
- begin
- MultiJson.encode(data)
- rescue MultiJson::DecodeError => e
- Pusher.logger.error("Could not convert #{data.inspect} into JSON")
- raise e
- end
+ Pusher.encode_json data
end
return params.merge({
View
8 lib/pusher/request.rb
@@ -35,7 +35,7 @@ def send_sync
'Content-Type'=> 'application/json'
})
else
- raise "Unsuported verb"
+ raise Pusher::Error, "Unsupported verb"
end
rescue Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED,
Errno::ETIMEDOUT, Errno::EHOSTUNREACH,
@@ -68,7 +68,7 @@ def send_async
:head => {'Content-Type'=> 'application/json'}
})
else
- raise "Unsuported verb"
+ raise Pusher::Error, "Unsupported verb"
end
http.callback {
begin
@@ -90,11 +90,11 @@ def send_async
def handle_response(status_code, body)
case status_code
when 200
- return symbolize_first_level(MultiJson.decode(body))
+ symbolize_first_level(Pusher.decode_json(body))
when 202
return true
when 400
- raise Error, "Bad request: #{body}"
+ raise Pusher::Error, "Bad request: #{body}"
when 401
raise AuthenticationError, body
when 404
View
4 lib/pusher/resource.rb
@@ -14,12 +14,12 @@ def get_async(params)
end
def post(params)
- body = MultiJson.encode(params)
+ body = Pusher.encode_json(params)
create_request(:post, {}, body).send_sync
end
def post_async(params)
- body = MultiJson.encode(params)
+ body = Pusher.encode_json(params)
create_request(:post, {}, body).send_async
end
View
4 lib/pusher/webhook.rb
@@ -84,9 +84,9 @@ def data
@data ||= begin
case @content_type
when 'application/json'
- MultiJson.decode(@body)
+ Pusher.decode_json(@body)
else
- raise "Unknown Content-Type (#{@content_type})"
+ raise Pusher::Error, "Unknown Content-Type (#{@content_type})"
end
end
end
Something went wrong with that request. Please try again.