lib/pubnub.rb in pubnub-3.3.0.5 vs lib/pubnub.rb in pubnub-3.3.0.6

- old
+ new

@@ -27,10 +27,11 @@ class Pubnub SUCCESS_RESPONSE = 200 + MSG_TOO_LARGE_RESPONSE = 400 TIMEOUT_BAD_RESPONSE_CODE = 1 TIMEOUT_BAD_JSON_RESPONSE = 0.5 TIMEOUT_GENERAL_ERROR = 1 TIMEOUT_SUBSCRIBE = 310 @@ -290,15 +291,34 @@ req.errback{ logAndRetryGeneralError(is_reactor_running, req, request) } req.callback { - if checkForBadJSON(req) == true - logAndRetryBadJSON(is_reactor_running, req, request) + + if %w(subscribe presence).include?(request.operation) + if (checkForBadJSON(req) == true && request.operation == "subscribe") + logAndRetryBadJSON(is_reactor_running, req, request) + else + processGoodResponse(is_reactor_running, req, request) + end else - processGoodResponse(is_reactor_running, req, request) + if req.response_header.http_status.to_i != SUCCESS_RESPONSE + + begin + server_response = Yajl.load(req.response) + request.callback.call(server_response) + rescue => e + request.callback.call([0, "Bad server response: #{req.response_header.http_status.to_i}"]) + ensure + EM.stop unless is_reactor_running + end + + else + processGoodResponse(is_reactor_running, req, request) + end end + } rescue EventMachine::ConnectionError, RuntimeError => e # RuntimeError for catching "EventMachine not initialized" error_message = "Network Error: #{e.message}" puts(error_message) @@ -318,11 +338,15 @@ end def processGoodResponse(is_reactor_running, req, request) if (req.response_header.http_status.to_i != SUCCESS_RESPONSE) - logAndRetryBadResponseCode(is_reactor_running, req, request) + + unless (req.response_header.http_status.to_i == MSG_TOO_LARGE_RESPONSE) + logAndRetryBadResponseCode(is_reactor_running, req, request) + end + else request.package_response!(req.response) cycle = request.callback.call(request.response) @@ -334,27 +358,29 @@ end end def logAndRetryGeneralError(is_reactor_running, req, request) errMsg = "#{Time.now}: Network connectivity issue while attempting to reach #{request.url}" - logError(errMsg) + logError(errMsg, request.url) retryRequest(is_reactor_running, req, request, TIMEOUT_GENERAL_ERROR) end def logAndRetryBadJSON(is_reactor_running, req, request) errMsg = "#{Time.now}: Retrying from bad JSON: #{req.response.to_s}" - logError(errMsg) + logError(errMsg, request.url) retryRequest(is_reactor_running, req, request, TIMEOUT_BAD_JSON_RESPONSE) end def logAndRetryBadResponseCode(is_reactor_running, req, request) errMsg = "#{Time.now}: Retrying from bad server response code: (#{req.response_header.http_status.to_i}) #{req.response.to_s}" - logError(errMsg) + logError(errMsg, request.url) retryRequest(is_reactor_running, req, request, TIMEOUT_BAD_RESPONSE_CODE) end - def logError(errMsg) - PUBNUB_LOGGER.debug(errMsg) + def logError(errMsg, url) + PUBNUB_LOGGER.debug("url: #{url}") + PUBNUB_LOGGER.debug("#{errMsg}") + PUBNUB_LOGGER.debug("") end def retryRequest(is_reactor_running, req, request, delay) if %w(subscribe presence).include?(request.operation) \ No newline at end of file