Description
Summary
The ClusterManager methods do not attach an error event handler to the http.ClientRequest which is used to perform the HTTP requests. Therefore, if an HTTP error occurs, it is thrown as an uncaught exception instead of calling the callback. There is no way to handle these errors gracefully aside from creating your own uncaughtException handler.
Example
const couchbase = require('couchbase'); |
|
const cnstr = 'couchbase://my.couchbase.address'; |
const cluster = new couchbase.Cluster(cnstr); |
const clusterManager = cluster.manager(); |
|
clusterManager.listBuckets((err, buckets) => {
|
if (err) { |
console.log('listBuckets failed', err); |
} else { |
console.log('listBuckets succeeded', buckets); |
}
|
});
|
process.on('uncaughtException', (err) => { |
console.log('Uncaught exception', err); |
});
|
If Couchbase is not running, this produces:
Uncaught exception { Error: connect ECONNREFUSED 10.204.101.143:8091
|
at Object.exports._errnoException (util.js:1026:11)
|
at exports._exceptionWithHostPort (util.js:1049:20)
|
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1081:14)
|
code: 'ECONNREFUSED',
|
errno: 'ECONNREFUSED',
|
syscall: 'connect',
|
address: '10.204.101.143',
|
port: 8091 }
|
Uncaught exception { Error: socket hang up
|
at createHangUpError (_http_client.js:252:15)
|
at Socket.socketCloseListener (_http_client.js:284:23)
|
at emitOne (events.js:101:20)
|
at Socket.emit (events.js:188:7)
|
at TCP._handle.close [as _onclose] (net.js:492:12) code: 'ECONNRESET' }
|
Proposed Change
Attach an error handler to the http.ClientRequest to forward errors to the callback.
httpReq.on('error', callback); |
Adding this to listBuckets gives the following output on the example script:
listBuckets failed { Error: connect ECONNREFUSED 10.204.101.143:8091
|
at Object.exports._errnoException (util.js:1026:11)
|
at exports._exceptionWithHostPort (util.js:1049:20)
|
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1081:14)
|
code: 'ECONNREFUSED',
|
errno: 'ECONNREFUSED',
|
syscall: 'connect',
|
address: '10.204.101.143',
|
port: 8091 }
|
This issue was discovered by Eduardo Barredo.