Skip to content

Commit

Permalink
[0.2.2] Better error handling
Browse files Browse the repository at this point in the history
Based on "A string it's not an Error" from @rauch now the errors have a
better way to throw no more strings, the API doesn't change.
  • Loading branch information
alejandro committed Dec 28, 2011
1 parent aec96bc commit 5eb1a9c
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 94 deletions.
6 changes: 5 additions & 1 deletion examples/all.js
@@ -1,5 +1,9 @@
var vimeo = require('../index').vimeo;

vimeo('user','brad','videos',function(err,data){
// Throw a error
vimeo('user','numbusLLC','videos',function(err,data){
console.log(err || data);
});
vimeo('user','brad','videos',function(err,data){
console.log(err || data);
});
13 changes: 8 additions & 5 deletions examples/video.js
@@ -1,8 +1,11 @@
var vimeo = require('../index').video;
var vimeo = require('../index').vimeo;

vimeo('33544767', function(err,data){
vimeo('video', '33544767','', function(err,data){
// Data is Exposed with raw as the whole response, or thumb, username.
console.log(data.raw);
console.log(data.thumb);
console.log(data.username.name);
if (err) console.log(err)
else {
console.log(data.raw);
console.log(data.thumb);
console.log(data.username.name);
}
});
187 changes: 100 additions & 87 deletions lib/vimeo.js
Expand Up @@ -31,20 +31,25 @@ var video = vimeo.video = function req(id,cb){
var now = new Date().getTime();
var url = ep.video + id + '.json';
request(ep.video + id + '.json', function(err,res){
if (res.statusCode === 200) {
this.raw = this.get = JSON.parse(res.body)[0];
this.thumb = {s: this.raw.thumbnail_small,m:this.raw.thumbnail_medium,l:this.raw.thumbnail_large };
this.username = { name: this.raw.user_name, portrait:this.raw.user_portrait_medium} ;
var data = {
time: new Date().getTime() - now ,
statusCode: res.statusCode,
raw: this.raw,
thumb: this.thumb,
username:this.username
}
cb(null,data);
if (res.statusCode === 200 && res.body.length > 0) {
try {
console.log(res.body)
this.raw = this.get = JSON.parse(res.body)[0];
this.thumb = {s: this.raw.thumbnail_small,m:this.raw.thumbnail_medium,l:this.raw.thumbnail_large };
this.username = { name: this.raw.user_name, portrait:this.raw.user_portrait_medium} ;
var data = {
time: new Date().getTime() - now ,
statusCode: res.statusCode,
raw: this.raw,
thumb: this.thumb,
username:this.username
}
cb(null,data);
} catch(e) {
cb(new Error('Not Found'),null)
}
} else {
cb({"error":"request denied","data":err},null);
cb(new Error(err),null);
}

});
Expand Down Expand Up @@ -78,11 +83,11 @@ var user = vimeo.user = function(usr,req, cb){
}
cb(null,data);
} else {
cb({"error":"user no exists", "data": usr },null);
cb( new Error('User no exists'),null);
}
});
} else {
cb({"error":"I need a valid request type","data": JSON.stringify(userOption)} ,null);
} else {
cb(new Error( 'The request type do not exists see nvimeo.info') ,null);
}
}

Expand All @@ -106,7 +111,7 @@ var user = vimeo.user = function(usr,req, cb){
}
cb(null, data);
} else {
cb({"error": "user no exist", "data":usr},null);
cb(new Error(usr +' do not exists'),null);
}
});
} else {
Expand All @@ -133,77 +138,85 @@ var albumOption = {
"info":"Album info for the specified album"
}
var all = vimeo.vimeo = function (type, id,req, cb){
var now =Date.now();
var url,error = null;
switch(type) {
case 'group':
if (groupOption.hasOwnProperty(req)) {
url = ep.root + 'group/' + id + '/' + req ;
} else error.data = groupOption;
break;
case 'activity':
if (activityOptions.hasOwnProperty(req)){
url = ep.activity +id + '/'+ req
} else { error = {};error.data = activityOptions;}
break;
case 'user':
if (userOption.hasOwnProperty(req)){
url = ep.root + id + '/' + req;
} else {error={};error.data = userOption;}
break;
case 'video':
url = ep.video + id
var video = true;
break;
case 'channel':
if (channelOption.hasOwnProperty(req)) {
url = ep.root + 'channel/' + id + '/' + req;
} else { error= {}; error.data = channelOption}
break;
case 'album':
if (albumOption.hasOwnProperty(req)){
url = ep.root + 'album/' + id + '/' +req;
} else { error={};error.data = albumOption}
break;
default:
error={}; error.data= 'METHOD not allowed';
// code
}
if (!error && video){
request( url + '.json', function(err,res){
if (res.statusCode === 200) {
this.raw = JSON.parse(res.body)[0];
this.thumb = {s: this.raw.thumbnail_small,m:this.raw.thumbnail_medium,l:this.raw.thumbnail_large };
this.username = { name: this.raw.user_name, portrait:this.raw.user_portrait_medium} ;
data = {
time: Date.now() - now,
raw: this.raw,
thumb: this.thumb,
username: this.username
}
cb(null,data);
} else {
cb({"error":"request denied","data":err},null);
}
});
} else if (!error){
request( url + '.json', function(err,res){
if (res.statusCode === 200) {
var body = JSON.parse(res.body);
data = {
status:'ok',
statusCode: res.statusCode,
time: Date.now() - now,
items: body.length,
body: body
}
cb(null, data);
} else {
cb({"error": "user no exist", "data":id},null);
}
});
if (arguments.length !== 4 ){
throw new Error("One of the params is missing see nvimeo.info" );
} else {
cb({"error":"request data invalid","data":error.data},null);
var now =Date.now();
var url,error = null;
switch(type) {
case 'group':
if (groupOption.hasOwnProperty(req)) {
url = ep.root + 'group/' + id + '/' + req ;
} else error.data = groupOption;
break;
case 'activity':
if (activityOptions.hasOwnProperty(req)){
url = ep.activity +id + '/'+ req
} else { error = {};error.data = activityOptions;}
break;
case 'user':
if (userOption.hasOwnProperty(req)){
url = ep.root + id + '/' + req;
} else {error={};error.data = userOption;}
break;
case 'video':
url = ep.video + id
var video = true;
break;
case 'channel':
if (channelOption.hasOwnProperty(req)) {
url = ep.root + 'channel/' + id + '/' + req;
} else { error= {}; error.data = channelOption}
break;
case 'album':
if (albumOption.hasOwnProperty(req)){
url = ep.root + 'album/' + id + '/' +req;
} else { error={};error.data = albumOption}
break;
default:
error={}; error.data= 'METHOD not allowed';
// code
}
if (!error && video){
request( url + '.json', function(err,res){
if (res.statusCode === 200) {
try {
this.raw = JSON.parse(res.body)[0];
this.thumb = {s: this.raw.thumbnail_small,m:this.raw.thumbnail_medium,l:this.raw.thumbnail_large };
this.username = { name: this.raw.user_name, portrait:this.raw.user_portrait_medium} ;
data = {
time: Date.now() - now,
raw: this.raw,
thumb: this.thumb,
username: this.username
}
cb(null,data);
} catch (e){
cb (new Error(id + ' => Not Found'),null);
}
} else {
cb( new Error(err),null);
}
});
} else if (!error){
request( url + '.json', function(err,res){
if (res.statusCode === 200) {
var body = JSON.parse(res.body);
data = {
status:'ok',
statusCode: res.statusCode,
time: Date.now() - now,
items: body.length,
body: body
}
cb(null, data);
} else {
cb(new Error('User do not exists'),null);
}
});
} else {
cb(new Error('Invalid option see nvimeo.info'),null);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -2,7 +2,7 @@
"author": "Alejandro Morales",
"name": "n-vimeo",
"description": "Vimeo API (data) Integration, retrieve info about everything (video,user,activity) with ease",
"version": "0.2.2",
"version": "0.2.3",
"keywords": [
"api",
"vimeo" ],
Expand Down

0 comments on commit 5eb1a9c

Please sign in to comment.