Permalink
Browse files

Added proper error handling for views, a view queue for process.nextTick

and a simple view test.
  • Loading branch information...
1 parent e4218dd commit 0388fdd1083ddb6f9bce7673d38f0c333ecab48a @flashingpumpkin committed Dec 8, 2010
Showing with 62 additions and 1 deletion.
  1. +23 −1 lib/couch-client.js
  2. +39 −0 viewTest.js
View
@@ -264,6 +264,7 @@ function CouchClient(url) {
});
}
+ var viewQueue = [];
function view(view, obj, callback) {
if (typeof obj === 'function') {
callback = obj;
@@ -278,7 +279,28 @@ function CouchClient(url) {
view = view + '?' + getParams;
}
}
- request("GET", view, callback)
+ function consumeQueue(){
+ var pending = viewQueue.shift();
+ if (viewQueue.length) process.nextTick(consumeQueue);
+ var view = pending[0], callback = pending[1];
+ request("GET", view, function(err, result){
+ if(result.error){
+ if(result.error == 'not_found'){
+ err = new Error("Design document not found: " + JSON.stringify(view));
+ err.errno = process.ENOENT;
+ }else{
+ err = new Error("CouchDB Error " + JSON.stringify(result));
+ }
+ }
+ if(err){
+ callback(err);
+ return;
+ }
+ callback(null, result);
+ });
+ }
+ viewQueue.push([view, callback]);
+ process.nextTick(consumeQueue);
}
// Expose the public API
View
@@ -0,0 +1,39 @@
+var CouchClient = require('./lib/couch-client');
+
+var Meetings = CouchClient("http://hack:me@localhost:5984/meetings");
+
+var designDoc = {
+ '_id': '_design/all'
+ , 'language': 'javascript'
+ , 'views': {
+ 'all': {
+ 'map': (function(doc){
+ emit(doc._id, doc)
+ }).toString()
+ }
+ }
+};
+
+Meetings.view(Meetings.uri.pathname + '/_design/all/_view/all', function(err, result){
+ if(!err || result)
+ throw new Error('Fail. View is not in database, should have yielded an error.');
+
+ Meetings.save(designDoc, function(err, result){
+ if (err)
+ throw err;
+
+ Meetings.save({some: 'Document'}, function(err, result){
+ if (err)
+ throw err;
+
+ Meetings.view('/meetings/_design/all/_view/all', function(err, result){
+ if (err || !result){
+ throw new Error('Fail. View is in database but didn\'t yield any results');
+ }
+ if(result.rows.length != 1)
+ throw new Error('Fail. Wrong number of documents.');
+ process.exit(0);
+ });
+ });
+ });
+});

0 comments on commit 0388fdd

Please sign in to comment.