Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Test & error handling #5

Open
wants to merge 4 commits into from

4 participants

@flashingpumpkin

The view function didn't handle errors properly. Now it actually looks at the content returned before calling the callback.
Also, I adopted a queue for views. :-o

@michael

Had the same problem.

Flashingpumpkin: Not sure if Tim would rather prefer delivering the original error object
{ error: 'not_found', reason: 'missing_named_view' }

instead of a modified one, in order to keep the code-base super-small.

Thx for the patch anyway :)

-- Michael

@candland
Collaborator

@devioustree Should this pull request be closed? Looks like it on a quick glance.

@michael

Fixed this in my fork, among other things. See michael@011698b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 8, 2010
  1. @flashingpumpkin
Commits on Dec 22, 2010
  1. @devioustree
  2. @devioustree
  3. @devioustree

    Fixed naming mismatch

    devioustree authored
This page is out of date. Refresh to see the latest.
Showing with 68 additions and 4 deletions.
  1. +29 −4 lib/couch-client.js
  2. +39 −0 viewTest.js
View
33 lib/couch-client.js
@@ -9,6 +9,9 @@ var Http = require('http'),
var POOL_SIZE = 200; // Maximum number of concurrent connections allowed.
var MAX_DOCS = 1000; // The maximum number of docs to send in a single batch
+// Handles changes made in node v0.3.0
+var NOT_FOUND_ERR_NO = process.ENOENT ? process.ENOENT : require('constants').ENOENT;
+
function noOp(err) { if (err) { throw err; } }
var pool = new Array(POOL_SIZE);
@@ -141,7 +144,7 @@ function CouchClient(url) {
request("POST", uri.pathname + "/_bulk_docs", {docs: body}, function (err, results) {
if (results.error) {
err = new Error("CouchDB Error: " + JSON.stringify(results));
- if (results.error === 'not_found') { err.errno = process.ENOENT; }
+ if (results.error === 'not_found') { err.errno = NOT_FOUND_ERR_NO; }
}
if (err) {
pending.forEach(function (callback) {
@@ -189,13 +192,13 @@ function CouchClient(url) {
if (includeDoc) {
if (result.error) {
err = new Error("CouchDB Error: " + JSON.stringify(result));
- if (result.error === 'not_found') { err.errno = process.ENOENT; }
+ if (result.error === 'not_found') { err.errno = NOT_FOUND_ERR_NO; }
pending[i](err);
return;
}
if (!result.doc) {
err = new Error("Document not found for " + JSON.stringify(result.key));
- err.errno = process.ENOENT;
+ err.errno = NOT_FOUND_ERR_NO;
pending[i](err);
return;
}
@@ -267,6 +270,7 @@ function CouchClient(url) {
});
}
+ var viewQueue = [];
function view(viewName, obj, callback) {
if (typeof obj === 'function') {
callback = obj;
@@ -281,7 +285,28 @@ function CouchClient(url) {
viewName = viewName + '?' + getParams;
}
}
- request("GET", viewName, 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([viewName, callback]);
+ process.nextTick(consumeQueue);
}
// Expose the public API
View
39 viewTest.js
@@ -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);
+ });
+ });
+ });
+});
Something went wrong with that request. Please try again.