57uff3r / nodejs-vksdk

NodeJS SDK for vk.com
201 stars 82 forks source link

Parsing error in sdk.js #24

Closed JPellegrino closed 8 years ago

JPellegrino commented 8 years ago
undefined:1
<html>
^
SyntaxError: Unexpected token <
    at Object.parse (native)
    at IncomingMessage.<anonymous> (/app/node_modules/vksdk/sdk.js:288:26)
    at IncomingMessage.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:920:16
    at process._tickCallback (node.js:415:13)
57uff3r commented 8 years ago

Может, сервер отбил вместо ответа страницу с ошибкой. Что вызывали, как получился такой ответ?

JPellegrino commented 8 years ago

Sorry, I can't read cyrillic but I guess you want some details. I am using two APIs : 'newsfeed.search' and 'users.get'. I first call 'newsfeed.search' throught:

    client.request('newsfeed.search', query, function (data) {
      callback(data);
    });

with query:

      var query = {
        'q': 'example',
        'extended': 1,
        'count': 200,
        'version': 5.34
      }

Then for all the results, I call 'users.get' throught:

    client.request('users.get', user_query, function (data) {
      if (data.error) {
        return callback(null);
      }
      var user = data.response[0];
      callback(user);
    });

with query:

  var user_query = {
    'user_ids': user_id,
    'fields': user_fields,
    'version': 5.34
  };

Where the user_id obtained with the previous call and:

var user_fields = [
  "uid",
  "sex",
  "bdate",
  "city",
  "country",
  "counters",
  "photo_50",
  "timezone",
]

I don't know from which of the two types of call the error is coming but it completely crashes my app, although the error seems to be really rare...

57uff3r commented 8 years ago

Oh, sorry, I thought you Russian speaker (most of SDK users are Russian speakers). Thank you, you was right — I asked you about details.

Sometimes vk.com can send HTML page with error text. And try to run code like this

client.request('users.get', user_query, function (data) {
    console.log(data);
});

and check the output.

JPellegrino commented 8 years ago

Thanks for your quick responses but I though, by ready the error message that the crash occurs at in sdk.js line 288 column 26 where I can read:

var o = JSON.parse(apiResponse);

So my question now is: is the sdk.js script handling properly the possibility of html answer ?

57uff3r commented 8 years ago

Obviously no, but should do that. I think I can fix it on the week or you can do it by yourself and I'll merge it.

Thank you!

JPellegrino commented 8 years ago

Ok I'll try to propose you a fix as fast as possible, but if someone skilled guy is listening around it could be nice to have idea to handle this properly...

57uff3r commented 8 years ago

Thank you for pull request. I've made several changes and now SDK emits parse-error event in this case

vk.on('parse-error', function(_e) {
    console.log(_e);
});

NPM package has been updated. Feel free too download new version. Thank you!