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

Cloudinary service responded with HTTP Code: 500 and HTML instead of JSON #8

Closed
laurynas-karvelis opened this issue Jan 10, 2013 · 5 comments

Comments

@laurynas-karvelis
Copy link
Contributor

I've noticed that if your server returns HTTP 500 status code with HTML instead of JSON your npm module exits node.js app completely!

undefined:1
<!DOCTYPE html>
^
SyntaxError: Unexpected token <
    at Object.parse (native)
    at IncomingMessage.res.on.error (/Users/laurynas/www/reversio/node_modules/cloudinary/lib/uploader.coffee:304:25)
    at IncomingMessage.EventEmitter.emit (events.js:126:20)
    at IncomingMessage._emitEnd (http.js:366:10)
    at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:149:23)
    at CleartextStream.socketOnData [as ondata] (http.js:1367:20)
    at CleartextStream.CryptoStream._push (tls.js:526:27)
    at SecurePair.cycle (tls.js:880:20)
    at EncryptedStream.CryptoStream.write (tls.js:267:13)
    at Socket.ondata (stream.js:38:26)

uploader.coffee handle_response and api.coffee call_api tries to parse JSON assuming everything will be fine.

Please add a try catch sections for JSON.parse() there in case your servers are down and responding with non-JSON.

@nadavs
Copy link
Contributor

nadavs commented Jan 10, 2013

Thank you for reporting this issue. We will look into it.
Can you please share the code that reached the HTML response of the 500 error?

@laurynas-karvelis
Copy link
Contributor Author

Thank you! I will do that in the evening as currently have no access to my project from work. Btw, best practise would be to assume that everything could break especially when communicating with external resources (in this case - your HTTP API).

As the error fires in one of the response event callbacks, end developer does not have any control to make it fail-save as it the event callback fires out of try catch scope :(.

@laurynas-karvelis
Copy link
Contributor Author

The issue which was causing your servers to respond with HTTP 500 status code was that for some reasons binary buffer which I was sending upload_stream()'s object was saved as your temp file in FS, but was not able to process it further properly:

exports.sampleExpressJsAction = function(req, res) {
  var stream = cloudinary.uploader.upload_stream(function (response) {
    // before coming here it dies with error mentioned in my original issue comment
    console.log(response);
  });

  req.on('data', stream.write);
  req.on('end', stream.end);
};

...but... if I save the Buffer first into file and then re-read it passing into upload_stream() stream object in the same manner it WORKS! Completely bizzarre...

Both buffer objects (request and file's) are identical. Have no clue why does it happen.

@laurynas-karvelis
Copy link
Contributor Author

Fixed in #9

@nadavs
Copy link
Contributor

nadavs commented Jan 15, 2013

Thank you for all your contributions and for sharing the code. The pull requests were merged into the master branch and are part of the latest released v1.0.1 of the NPM.
We will make sure that the service avoids returning HTML responses for API requests.

@nadavs nadavs closed this as completed Jan 15, 2013
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