lib/lumberg/format_whm.rb in lumberg-3.1.0 vs lib/lumberg/format_whm.rb in lumberg-4.0.0
- old
+ new
@@ -13,29 +13,31 @@
def on_complete(env)
encoding = env[:response_headers]['content-encoding']
encoding = encoding.to_s.downcase if encoding
- body = case encoding
+ uncompressed_body = case encoding
when 'gzip'
env[:response_headers].delete('content-encoding')
Zlib::GzipReader.new(StringIO.new(env[:body])).read
when 'deflate'
env[:response_headers].delete('content-encoding')
Zlib::Inflate.inflate(env[:body])
else
env[:body]
end
- if body =~ /cPanel operations have been temporarily suspended/
- raise Lumberg::WhmConnectionError.new(body)
- end
-
- if @type == :whostmgr || response_type(body) == :whostmgr
- env[:body] = format_response body
+ env[:body] = if @type == :whostmgr
+ format_response(uncompressed_body)
else
- env[:body] = format_response JSON.parse(body)
+ begin
+ parsed_body = JSON.parse(uncompressed_body)
+ format_response(parsed_body)
+ rescue JSON::ParserError
+ # In some error cases, cpanel doesn't return JSON :smdh:
+ raise Lumberg::WhmConnectionError, uncompressed_body
+ end
end
end
def response_values(env)
{status: env[:status], headers: env[:response_headers], body: env[:body]}
@@ -57,10 +59,10 @@
success, message, params = format_whostmgr_response(response)
when :error
message = response['error']
when :xfer
success, message, params = format_xfer_response(response)
- else
+ when :unknown
message = "Unknown error occurred #{response.inspect}"
end
params = Whm::symbolize_keys(params)
params = Whm::to_bool(params, @boolean_params)