error message fix

This commit is contained in:
Dan Zajdband
2012-08-20 13:20:23 -03:00
parent 081a562b6b
commit 0ba0babd85

View File

@@ -10,11 +10,20 @@ var request = require('superagent')
* Exports the constructor * Exports the constructor
*/ */
var MovieDB = module.exports = function(api_key){ module.exports = function(api_key){
if(api_key) this.api_key = api_key; if(api_key) return new MovieDB(api_key);
else throw new Error('Bad api key'); else throw new Error('Bad api key');
}; };
/*
* Constructor
*/
function MovieDB(api_key) {
this.api_key = api_key;
return this;
}
/* /*
* API auth * API auth
*/ */
@@ -31,6 +40,8 @@ MovieDB.prototype.requestToken = function(fn){
else throw new Error('Invalid authentication'); else throw new Error('Invalid authentication');
fn(); fn();
}); });
return this;
}; };
/* /*
@@ -55,8 +66,9 @@ Object.keys(endpoints.methods).forEach(function(method){
} else { } else {
execMethod.call(this, met[m].method, params, met[m].resource, fn); execMethod.call(this, met[m].method, params, met[m].resource, fn);
} }
};
return this;
};
}); });
}); });
@@ -71,6 +83,6 @@ var execMethod = function(type, params, endpoint, fn){
.end(function(res){ .end(function(res){
if(res.ok) fn(null, res.body); if(res.ok) fn(null, res.body);
else if(res.body && res.body.status_message) fn(new Error(res.body.status_message), null); else if(res.body && res.body.status_message) fn(new Error(res.body.status_message), null);
else fn(res.error, null); else fn(new Error(res.text), null);
}); });
}; };