decafjs / decaf-mongodb

MongoDB module for decaf
MIT License
0 stars 1 forks source link

Trying to catch error when database not started #2

Open mm765 opened 8 years ago

mm765 commented 8 years ago

I am trying to find out how i can catch errors upon db-connection. While i was testing the latest commit, i forgot to start my database before running my test-program and i get the following: \ EXCEPTION ** JavaException: com.mongodb.MongoTimeoutException: Timed out while waiting to connect after 6 ms at /opt/itk/libs/decaf-mongodb/lib/Cursor.js:56 (anonymous) at server.js:23 at /usr/local/decaf/builtins/rhino.js:117 (anonymous) at /usr/local/decaf/builtins/include.js:96 (includeFile) at /usr/local/decaf/builtins/include.js:107 (anonymous) at /usr/local/decaf/builtins/shell.js:72 (anonymous) at /usr/local/decaf/builtins/shell.js:18 at /usr/local/decaf/builtins/rhino.js:117 (anonymous) at /usr/local/decaf/builtins/include.js:96 (includeFile) at /usr/local/decaf/builtins/include.js:107 (anonymous) at /usr/local/decaf/builtins/all.js:160

First, i tried eMongoDB = require('libs/decaf-mongodb').MongoDB try db = new eMongoDB('databasename') catch console.log('Error connecting to database, exiting'); quit()

in my own code, didn't work. Then i tried to add try/catch blocks to MongoDB.js like this: try { var client = new MongoClient(); } catch (error) { throw error; }

and function MongoDB(db) { try { this.db = client.getDB(db); } catch (error) { throw error; } this.requestStarted = false; }

that didn't work either - since i don't really know about exception handling from java in javascript, i have no futher idea what i could do. Please help.

mm765 commented 8 years ago

Btw db = new eMongoDB('databasename') does not return undefined, so that does not work either, also try/catch around db.use('collectionname') also doesnt work, and since this always returns undefined, it doesnt help either.

mschwartz commented 8 years ago

The Mongo Java library may not throw an error until you try to do a query. That's my guess.

The API I implemented is 99.9% compatible with the mongo shell. The use() method is the only difference.

On Thu, Jan 7, 2016 at 5:00 AM, mm765 notifications@github.com wrote:

Btw db = new eMongoDB('databasename') does not return undefined, so that does not work either, also try/catch around db.use('collectionname') also doesnt work, and since this always returns undefined, it doesnt help either.

— Reply to this email directly or view it on GitHub https://github.com/decafjs/decaf-mongodb/issues/2#issuecomment-169656653 .

mschwartz commented 8 years ago

The JavaDocs say new MongoClient() should throw an error.

The Decaf driver creates MongoClient() when you require() the module.

require() might be catching the error, but it should also print an error message at least.

You may also try calling MongoDB.getLastError() and getPreviousError() and see what they return.

On Thu, Jan 7, 2016 at 8:48 AM, Mike Schwartz mykesx@gmail.com wrote:

The Mongo Java library may not throw an error until you try to do a query. That's my guess.

The API I implemented is 99.9% compatible with the mongo shell. The use() method is the only difference.

On Thu, Jan 7, 2016 at 5:00 AM, mm765 notifications@github.com wrote:

Btw db = new eMongoDB('databasename') does not return undefined, so that does not work either, also try/catch around db.use('collectionname') also doesnt work, and since this always returns undefined, it doesnt help either.

— Reply to this email directly or view it on GitHub https://github.com/decafjs/decaf-mongodb/issues/2#issuecomment-169656653 .

mm765 commented 8 years ago

I now tried try/catch blocks around the require as well as around the find() - neither catches any exception. Then i tried logging getLastError() after each call that is related to mongo and in every case it returned undefined. When i put logging-statements in-between each call, every one before the first find() is output, then the exception from java is printed and the software exits. The problem with this is, that if my database-connection fails for some reason, it would be a bad idea if my whole server goes down (the exception stops the process) - i need a way for it to fail gracefully (like displaying a page saying "oops, something is wrong, please come back later"). Any further ideas ?

mm765 commented 8 years ago

Ok, i found it - my code looks like this: eMongoDB = require('libs/decaf-mongodb').MongoDB db = new eMongoDB('databasename') db.use('users') cu = db.users.find({}) console.log('Count', cu.count())

and the exception actually happens on the cu.count() call, not before. At that point, i can actually catch it. Seems a little weird to me that it fails this late, but it should still work for me.

mschwartz commented 8 years ago

Yeah, the inner workings of the Java MongoDB driver are something of a black box to me. I presume it reconnects if the server goes away.

The good news is I've heavily used the MongoDB driver in DecafJS apps. Some databases with millions of rows/records. Obviously, none of my documents had arrays of strings :)

On Thu, Jan 7, 2016 at 5:29 PM, mm765 notifications@github.com wrote:

Ok, i found it - my code looks like this: eMongoDB = require('libs/decaf-mongodb').MongoDB db = new eMongoDB('databasename') db.use('users') cu = db.users.find({}) console.log('Count', cu.count())

and the exception actually happens on the cu.count() call, not before. At that point, i can actually catch it. Seems a little weird to me that it fails this late, but it should still work for me.

— Reply to this email directly or view it on GitHub https://github.com/decafjs/decaf-mongodb/issues/2#issuecomment-169861036 .