alexwhitman / node-pushbullet-api

PushBullet API module for Node.js
165 stars 36 forks source link

new Error(body) is useless #13

Closed Zren closed 9 years ago

Zren commented 9 years ago

https://github.com/alexwhitman/node-pushbullet-api/blob/master/lib/pushbullet.js#L556

The argument entered into Error is .toString()ed.

err.message is equal to [object Object] Which tells you nothing.

alexwhitman commented 9 years ago

In this case the body should be a string anyway, or at least not an object. Do you have a way to reproduce the error?

Zren commented 9 years ago

body is an object if you pass json:true in the request options. Try console.log()ing the err with an invalid apiKey (returns a 401).

Zren commented 9 years ago
var PushBullet = require('pushbullet');
var pusher = new PushBullet('asdfasdfasdfasdf');
pusher.devices(function(err, response) {
    console.log(err, response);
    console.log(err.message);

    pusher.handleResponse = function handleResponse(error, response, body, callback) {
        if (error) {
            if (typeof callback === 'function') {
                callback(error);
            }
            return;
        }

        if (response.statusCode !== 200) {
            console.log('===========================');
            console.log('response.statusCode !== 200');
            console.log('response.statusCode', response.statusCode);
            console.log('body', body);
            console.log('===========================');
            if (typeof callback === 'function') {
                callback(body);
            }
            return;
        }

        if (typeof callback === 'function') {
            callback(null, body);
        }
    };

    pusher.devices(function(err, response) {
        console.log(err, response);
    });

});
[Error: [object Object]] undefined
[object Object]
===========================
response.statusCode !== 200
response.statusCode 401
body { error: 
   { type: 'invalid_request',
     message: 'Access token is missing or invalid.',
     cat: '(=^・ω・^)y=' } }
===========================
{ error: 
   { type: 'invalid_request',
     message: 'Access token is missing or invalid.',
     cat: '(=^・ω・^)y=' } } undefined
alexwhitman commented 9 years ago

Looks like that's a leftover from when json wasn't used everywhere. I'll get this fixed over the weekend.

alexwhitman commented 9 years ago

Fixed in v1.4.2.