Pomax / node-flickrapi

A node.js (and client-library) implementation of the Flickr API with oauth API key authentication and API method proxying

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sometimes errors aften upload

Tloxipeuhca opened this issue · comments

Sometimes errors after upload.

  1. If an flickr API error occur (show error code)
    the body response is like this :
  <?xml version="1.0" encoding="utf-8" ?>
  <rsp stat="fail">
     <err code="3" msg="General upload failure" />
  </rsp>

The node-flickrapi return a succes with an empty array.

  1. Sometimes the returnred body is undefined and the node-flickrapi use indexOf of body the body.

Tracelog :

TypeError: Cannot call method 'indexOf' of undefined
    at Request._callback (.\flickrapi\src\utils.js:374:17)
    at self.callback (.\flickrapi\node_modules\request\index.js:148:22)
    at Request.emit (events.js:117:20)
    at ClientRequest.self.clientErrorHandler (.\flickrapi\node_modules\request\index.js:258:10)
    at ClientRequest.emit (events.js:95:17)
    at CleartextStream.socketErrorListener (http.js:1547:9)
    at CleartextStream.emit (events.js:95:17)
    at Socket.onerror (tls.js:1442:17)
    at Socket.emit (events.js:117:20)
    at net.js:440:14

To fix the error update the last request.post in utils.js with this code :

  var xpath = require('xpath'),
          dom = require('xmldom').DOMParser;

  var req = request.post(flickrURL, function(error, response, body) {
    if (error) {
      return callback(error);
    }
    if (!body) {
      return callback("Photo upload failed, serveur response is empty.");
    }
    // format:json does not actually work, so we need to grab the photo ID from the response XML:
    // <?xml version="1.0" encoding="utf-8" ?><rsp stat="fail"><err code="3" msg="General upload failure" /></rsp>
    // <?xml version="1.0" encoding="utf-8" ?><rsp stat="ok"><photoid>.........</photoid></rsp>
    var doc = new dom().parseFromString(body);
    var status = xpath.select1('//rsp/@stat', doc);
    if (status && status.value === 'ok') {
      var id = xpath.select('//rsp/photoid/text()', doc)[0].nodeValue;
      return callback(null, id);
    }
    if (status && status.value === 'fail') {
      var code = xpath.select1('//rsp/err/@code', doc).value;
      var msg = xpath.select1('//rsp/err/@msg', doc).value;
      return callback("Photo upload failed, "+msg);
    }
    if (body && body.indexOf("oauth_problem=signature_invalid") > -1) {
      return callback("Photo upload failed, oauth signature is invalid.");
    }
    // Unknown error
    return callback("Photo upload failed, unknown error.");
  });

hm, no need for xpath/dom here, a check for the substring <rsp stat="fail"> should be enough to tell us we have a problem, in which case msg="([^"]+)" will get the correct error message. That said, the fact that you get an error is also interesting because that could be your connect, or flickr, or node-flickr - any idea what prompted you to get the error? (because we might be able to add a finer level control than just grepping the error message and leaving it at that)

commented

closing due to lack of information with which to debug. Should it still happen, please refile.