thingdom / node-neo4j

[RETIRED] Neo4j graph database driver (REST API client) for Node.js
Apache License 2.0
925 stars 135 forks source link

Promise-wrapped fails to catch #214

Closed ekkis closed 7 years ago

ekkis commented 7 years ago

I use bluebird with this library:

var neo = require('neo4j');
var Promise = require('bluebird');
var db = new new.GraphDatabase(...);
var cypher = Promise.promisify(db.cypher, {context: db});

but when I issue a request that fails, the code fails to catch it:

cypher('invalid cypher script').catch(e => { console.log('caught'); });

and instead I get a crash like this:

/Users/ekkis/dev/test/inc/node_modules/neo4j/lib-new/errors.js:20 Error.captureStackTrace(this, this.constructor); ^

TypeError: Error.captureStackTrace is not a function at ClientError.Error [as constructor] (/Users/ekkis/dev/test/inc/node_modules/neo4j/lib-new/errors.js:20:13) at new ClientError (/Users/ekkis/dev/test/inc/node_modules/neo4j/lib-new/errors.js:81:48) at Function.__dirname.Error.Error._fromObject (/Users/ekkis/dev/test/inc/node_modules/neo4j/lib-new/errors.js:70:14) at /Users/ekkis/dev/test/inc/node_modules/neo4j/lib-new/GraphDatabase.js:302:25 at Request._callback (/Users/ekkis/dev/test/inc/node_modules/neo4j/lib-new/GraphDatabase.js:92:20) at Request.self.callback (/Users/ekkis/dev/test/inc/node_modules/request/request.js:187:22) at emitTwo (events.js:106:13) at Request.emit (events.js:191:7) at Request. (/Users/ekkis/dev/test/inc/node_modules/request/request.js:1048:10) at emitOne (events.js:96:13) at Request.emit (events.js:188:7) at IncomingMessage. (/Users/ekkis/dev/test/inc/node_modules/request/request.js:969:12) at emitNone (events.js:91:20) at IncomingMessage.emit (events.js:185:7) at endReadableNT (_stream_readable.js:974:12) at _combinedTickCallback (internal/process/next_tick.js:74:11) at process._tickCallback (internal/process/next_tick.js:98:9)

I'm using node v7.3.0 and can't help but thinking it must be related to: https://github.com/thingdom/node-neo4j/issues/211

I need to stop crashing every time the cypher send is malformed. can anyone help?

ekkis commented 7 years ago

ok, to remove the issue of promises I've tested this in a simpler way:

var neo = require('neo4j');
var db = new new.GraphDatabase(...);

and a call with invalid cypher still blows up without returning an error:

db.cypher('invalid cypher script', (err, res) => {
    if (err) console.log('ERROR: ' + err);
    console.log(res);
})

help?

ekkis commented 7 years ago

this is making me crazy. here's a complete test. save the file as "q":

#!/usr/bin/env node

const url = 'http://neo4j:MyPass@localhost:7474';
const Neo4j = require('neo4j');
const db = new Neo4j.GraphDatabase(url);
try {
    db.cypher({query: process.argv[2]}, function(err, ls) {
        console.log('--- CALLBACK ---');
        if (err) {
            console.log('--- ERROR ---');
            console.log(err);
        }
        console.log('--- RESULT ---');
        console.log(ls);
    });
}
catch (e) {
    console.log('--- CAUGHT ---');
    console.log(e);
}

so if I run at the command line:

q "match (n:test) return n"

I get back what I would expect:

--- CALLBACK --- --- RESULT --- [ { n: Node { _id: 0, labels: [Object], properties: [Object] } } ]

but I miss-type the command:

q "match (n:test))) return n"

I see:

/Users/ekkis/Development/x/inc/node_modules/neo4j/lib-new/errors.js:20 Error.captureStackTrace(this, this.constructor); ^

TypeError: Error.captureStackTrace is not a function at ClientError.Error [as constructor] (/Users/ekkis/Development/x/inc/node_modules/neo4j/lib-new/errors.js:20:13) at new ClientError (/Users/ekkis/Development/x/inc/node_modules/neo4j/lib-new/errors.js:81:48) at Function.__dirname.Error.Error._fromObject (/Users/ekkis/Development/x/inc/node_modules/neo4j/lib-new/errors.js:70:14) at /Users/ekkis/Development/x/inc/node_modules/neo4j/lib-new/GraphDatabase.js:302:25 at Request._callback (/Users/ekkis/Development/x/inc/node_modules/neo4j/lib-new/GraphDatabase.js:92:20) at Request.self.callback (/Users/ekkis/Development/x/inc/node_modules/request/request.js:187:22) at emitTwo (events.js:106:13) at Request.emit (events.js:191:7) at Request. (/Users/ekkis/Development/x/inc/node_modules/request/request.js:1048:10) at emitOne (events.js:96:13)

not even the try/catch works! what??

ekkis commented 7 years ago

ok, with the help of jfhbrook from freenode's #node.js I've "fixed" this issue. the library remains broken but for those who care, the exceptions can be caught like this:

process.on('uncaughtException', function(e) {
    console.log('--- CAUGHT BY EVENT ---');
    console.log(e);
});
ImTheC commented 7 years ago

Thanks for the TON of hours of time and frustration you put into looking into this! That "fix" worked for me too to keep the server from crashing completely.

ghost commented 4 years ago

How can we return some value for the API? As catching the unhandled exception works, but unable to return a custom error or text