From 0ba0babd85cbec2c19a45662646b5f43c9369471 Mon Sep 17 00:00:00 2001 From: Dan Zajdband Date: Mon, 20 Aug 2012 13:20:23 -0300 Subject: [PATCH] error message fix --- lib/moviedb.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/moviedb.js b/lib/moviedb.js index 9b2b361..a260ad1 100644 --- a/lib/moviedb.js +++ b/lib/moviedb.js @@ -10,11 +10,20 @@ var request = require('superagent') * Exports the constructor */ -var MovieDB = module.exports = function(api_key){ - if(api_key) this.api_key = api_key; +module.exports = function(api_key){ + if(api_key) return new MovieDB(api_key); else throw new Error('Bad api key'); }; +/* + * Constructor + */ + +function MovieDB(api_key) { + this.api_key = api_key; + return this; +} + /* * API auth */ @@ -31,6 +40,8 @@ MovieDB.prototype.requestToken = function(fn){ else throw new Error('Invalid authentication'); fn(); }); + + return this; }; /* @@ -55,8 +66,9 @@ Object.keys(endpoints.methods).forEach(function(method){ } else { 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){ if(res.ok) fn(null, res.body); 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); }); };