diff --git a/error.js b/error.js index b767d2a6..112f90a9 100644 --- a/error.js +++ b/error.js @@ -10,17 +10,18 @@ * * @return An error augmented an driver specific code */ -function gen_err(error, code, http_code, type) { - if(typeof error === 'string') { error = new Error(error); } +function gen_err(reason,code,request,http_code,type) { + if(typeof reason === 'string') { error = new Error(reason); } if(!type) { type = http_code; http_code = 500; } - error.nano_code = code; + error.error = code; error.http_code = http_code; - error.type = type; + error.namespace = type; + error.request = request; return error; } -exports.request_err = function (e,c,h) { return gen_err(e,c,h,"request"); }; -exports.couch_err = function (e,c,h) { return gen_err(e,c,h,"couch"); }; +exports.request_err = function (e,c,r,h) { return gen_err(e,c,r,h,"request"); }; +exports.couch_err = function (e,c,r,h) { return gen_err(e,c,r,h,"couch"); }; diff --git a/nano.js b/nano.js index 93cdb83b..887f774d 100644 --- a/nano.js +++ b/nano.js @@ -24,6 +24,9 @@ module.exports = exports = nano = function nano_module(cfg) { * @return Execution of the code in your callback. Hopefully you are handling */ function request_db(name,method,callback) { + if(!callback) { // Then ignore callback + callback = function () { return; }; + } var req = { uri: cfg.database(name) , method: method @@ -33,13 +36,13 @@ module.exports = exports = nano = function nano_module(cfg) { var status_code = h.statusCode , parsed; if(e) { - callback(error.request_err(e, "connect_db",status_code)); + callback(error.request_err(e,"connect_db",req,status_code)); return; } parsed = JSON.parse(b); if (status_code === 200 || status_code === 201) { callback(null,parsed); } else { // Proxy the error - callback(error.couch_err(parsed.reason,parsed.error,status_code)); + callback(error.couch_err(parsed.reason,parsed.error,req,status_code)); } }); } @@ -61,18 +64,38 @@ module.exports = exports = nano = function nano_module(cfg) { * @see request_db */ function create_db(name, callback) { - request_db(name, "PUT", callback); + request_db(name,"PUT",callback); } /* + * Annihilates a CouchDB Database * + * e.g. nano.db.delete(db_name); + * + * Even though this looks sync it is an async function + * and therefor order is not guaranteed + * + * @see request_db */ function destroy_db(name, callback) { - request_db(name, "DELETE", callback); + request_db(name,"DELETE",callback); + } + + /* + * Gets information about a CouchDB Database + * + * e.g. nano.db.get(db_name, function(e,b) { + * console.log(b); + * }); + * + * @see request_db + */ + function get_db(name, callback) { + request_db(name,"GET",callback); } public_functions = { db: { create: create_db - //, get: get_db + , get: get_db , destroy: destroy_db } //, doc: { create: create_doc diff --git a/tests/db/create.js b/tests/db/create.js index 4c47f39e..b2dd4cd9 100644 --- a/tests/db/create.js +++ b/tests/db/create.js @@ -1,17 +1,16 @@ var vows = require('/usr/lib/node_modules/vows/lib/vows') , assert = require('assert') , cfg = require('../../cfg/tests.js') - , nano = require('../../nano')(cfg) - , db_name = "foo"; + , nano = require('../../nano')(cfg); /***************************************************************************** * create_db * *****************************************************************************/ function create_db (callback) { - nano.db.destroy(db_name, function () { - nano.db.create(db_name, function (e,b) { + nano.db.destroy("cr1", function () { + nano.db.create("cr1", function (e,b) { callback(e,b); - return; + nano.db.destroy("cr1"); }); }); } @@ -21,35 +20,15 @@ function create_db_ok(e,b) { assert.equal(b.ok, true); } -/***************************************************************************** - * create_db_recursive_error * - *****************************************************************************/ -function create_db_recursive_error(callback) { - nano.db.destroy(db_name, function () { - nano.db.create(db_name, function (e,b) { - if(e) { - callback(e); - } - else { - create_db_recursive_error(name,callback); - } - }); - }); -} - -function create_db_recursive_error_ok(e,tried) { - assert.equal(e.nano_code,"file_exists"); -} - /***************************************************************************** * recursive_retries_create_db * *****************************************************************************/ function recursive_retries_create_db(tried,callback) { - nano.db.destroy(db_name, function () { - nano.db.create(db_name, function (e,b) { + nano.db.destroy("cr2", function () { + nano.db.create("cr2", function (e,b) { if(tried.tried === tried.max_retries) { callback(true); - return; + nano.db.destroy("cr2"); } else { tried.tried += 1; @@ -67,9 +46,6 @@ vows.describe('nano.db.create').addBatch({ "create_db": { topic: function () { create_db(this.callback); } , "=": create_db_ok }, - "create_db_recursive_error": { - topic: function () { create_db_recursive_error(this.callback); } - , "=": create_db_recursive_error_ok }, "recursive_retries_create_db": { topic: function () { recursive_retries_create_db({tried:0, max_retries:5},this.callback); } , "=": recursive_retries_create_db_ok } diff --git a/tests/db/destroy.js b/tests/db/destroy.js index f379c44d..e8d21a9f 100644 --- a/tests/db/destroy.js +++ b/tests/db/destroy.js @@ -3,10 +3,12 @@ var vows = require('/usr/lib/node_modules/vows/lib/vows') , cfg = require('../../cfg/tests.js') , nano = require('../../nano')(cfg); -function destroyDb (name,callback) { - nano.db.destroy(name, function (e,b) { - callback(e,b); - return; +function destroyDb (callback) { + nano.db.create("de1", function () { + nano.db.destroy("de1", function (e,b) { + callback(e,b); + return; + }); }); } @@ -17,7 +19,7 @@ function destroyDbWorked (e,b) { vows.describe('nano.db.destroy').addBatch({ "nano.db.destroy(foo)": { - topic: function () { destroyDb("foo", this.callback); } + topic: function () { destroyDb(this.callback); } , "=": destroyDbWorked } }).exportTo(module); \ No newline at end of file