Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception thrown when trying to find an inexisting resource #26

Closed
zedalaye opened this issue Aug 24, 2014 · 4 comments
Closed

Exception thrown when trying to find an inexisting resource #26

zedalaye opened this issue Aug 24, 2014 · 4 comments

Comments

@zedalaye
Copy link
Contributor

cl = Mailjet::Contactslist.find(42)

Instead of returning nil, raises an "unexpected end of file" exception

/home/pierre/.rvm/gems/ruby-2.1.2/gems/rest-client-1.7.2/lib/restclient/request.rb:504:in `read': unexpected end of file (Zlib::GzipFile::Error)
    from /home/pierre/.rvm/gems/ruby-2.1.2/gems/rest-client-1.7.2/lib/restclient/request.rb:504:in 'decode'
    from /home/pierre/.rvm/gems/ruby-2.1.2/gems/rest-client-1.7.2/lib/restclient/request.rb:489:in 'process_result'
    from /home/pierre/.rvm/gems/ruby-2.1.2/gems/rest-client-1.7.2/lib/restclient/request.rb:421:in 'block in transmit'
    from /home/pierre/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/net/http.rb:853:in `start'
    from /home/pierre/.rvm/gems/ruby-2.1.2/gems/rest-client-1.7.2/lib/restclient/request.rb:413:in 'transmit'
    from /home/pierre/.rvm/gems/ruby-2.1.2/gems/rest-client-1.7.2/lib/restclient/request.rb:176:in 'execute'
    from /home/pierre/.rvm/gems/ruby-2.1.2/gems/rest-client-1.7.2/lib/restclient/request.rb:41:in 'execute'
    from /home/pierre/.rvm/gems/ruby-2.1.2/gems/rest-client-1.7.2/lib/restclient/resource.rb:51:in 'get'
    from /home/pierre/.rvm/gems/ruby-2.1.2/gems/mailjet-1.0.0/lib/mailjet/connection.rb:47:in 'handle_api_call'
    from /home/pierre/.rvm/gems/ruby-2.1.2/gems/mailjet-1.0.0/lib/mailjet/connection.rb:26:in `get'
    from /home/pierre/.rvm/gems/ruby-2.1.2/gems/mailjet-1.0.0/lib/mailjet/resource.rb:52:in `find'
    from /home/pierre/Projets/ng/mailjet_test/test.rb:66:in `<top (required)>'
    from -e:1:in `load'
    from -e:1:in `<main>'
@zedalaye
Copy link
Contributor Author

This issue may be related to ZLib/GZip. Not sure. The workaround is to ask RestClient to send "Accept-Encoding: identity" header instead of its default "Accept-Encoding: gzip, deflate"

@zedalaye
Copy link
Contributor Author

I traced this issue into RestClient::Request#decode. I dumped the "body" param to a base64 string and I used it to check whether the problem is in RestClient, Ruby, ZLib or MailJet v3 API.

To dump the "body" buffer I used RubyMine debugger, used the "Evaluate Expression" function and I evaluated Base64.strict_encode64(body)

The resulting string is : H4sIAAAAAAAAA6pWUHItKsov8sxLy1dSsFJQUtKBivimFhcnpqeCBf2TslKTSxTy8ksU0vJL81JAioJLEktKi53zU8BKTAxMFGoBAAA=

Then I wrote this very simple program :

require 'base64'
require 'zlib'
require 'stringio'

puts "RUBY_VERSION: #{RUBY_VERSION}"
puts "ZLib version: #{Zlib.zlib_version}"

DATA = "H4sIAAAAAAAAA6pWUHItKsov8sxLy1dSsFJQUtKBivimFhcnpqeCBf2TslKTSxTy8ksU0vJL81JAioJLEktKi53zU8BKTAxMFGoBAAA="

puts "\nBase64 string\n#{DATA}\n"

buffer = Base64.strict_decode64(DATA)

puts "\nRaw buffer\n#{buffer}\n\n"

gz = Zlib::GzipReader.new(StringIO.new(buffer))

begin
  puts "\nZlib::GzipReader.new(StringIO.new(buffer)).read"
  puts gz.read
rescue Exception => e
  puts e
end

puts "\nZlib::GzipReader.new(StringIO.new(buffer)).read(77)"
puts gz.read(77)

puts "\nUsing Unix tools (base64 + gunzip)"
puts `echo #{DATA} | base64 --decode | gunzip -c - 2>&1`

Which outputs :

RUBY_VERSION: 2.1.2
ZLib version: 1.2.8

Base64 string
H4sIAAAAAAAAA6pWUHItKsov8sxLy1dSsFJQUtKBivimFhcnpqeCBf2TslKTSxTy8ksU0vJL81JAioJLEktKi53zU8BKTAxMFGoBAAA=

Raw buffer
���VPr-*�/��K�WR�RPRҁ�����'������R�K���K���K�R@��K�KJ���S�JL
                                                            L�j�


Zlib::GzipReader.new(StringIO.new(buffer)).read
unexpected end of file

Zlib::GzipReader.new(StringIO.new(buffer)).read(77)
{ "ErrorInfo" : "", "ErrorMessage" : "Object not found", "StatusCode" : 404 }

Using Unix tools (base64 + gunzip)
{ "ErrorInfo" : "", "ErrorMessage" : "Object not found", "StatusCode" : 404 }
gzip: stdin: unexpected end of file

So I suspect the MailJet server to return invalid GZip encoded data but at the same time, the same request run with curl works as expected :

curl -s -X GET --user "$MJ_APIKEY_PUBLIC:$MJ_APIKEY_PRIVATE" https://api.mailjet.com/v3/REST/contactslist/42
{ "ErrorInfo" : "", "ErrorMessage" : "Object not found", "StatusCode" : 404 }

But the same request with wget never output anything (nor print any error message) 👍

wget -d --http-user $MJ_APIKEY_PUBLIC --http-password=$MJ_APIKEY_PRIVATE -O result.json --header "Accept: application/json" --header "Accept-Encoding: gzip" https://api.mailjet.com/v3/REST/contactslist/42

The result.json file remains empty.

@zedalaye
Copy link
Contributor Author

I made another change to the Mailjet::Resource code to force deflate encoding instead of gzip and it works like a charm ! (I'll push these changes to my fork for those interrested)

zedalaye added a commit to zedalaye/mailjet-gem that referenced this issue Aug 24, 2014
RestClient::ResourceNotFound exceptions are handled by Mailjet::Connection and turned into Mailjet::ApiError exceptions
@tylerjnap
Copy link
Contributor

Fixed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants