diff --git a/README.md b/README.md index 3c8fba71..8c66eb79 100644 --- a/README.md +++ b/README.md @@ -463,6 +463,10 @@ var options = { ``` It can be useful for diagnostics / logging within your application. +Notification happens just before the query execution. And if the notification handler throws +an error, the query execution will be intercepted and rejected with the error that's been +thrown by the handler function. + The function receives the following parameters: * `client` - object from the [PG] library that represents the connection; * `query` - query that's being executed; diff --git a/index.js b/index.js index e2727556..468500f7 100644 --- a/index.js +++ b/index.js @@ -369,7 +369,12 @@ function $query(client, query, values, qrm, options) { if (typeof(func) !== 'function') { throw new Error("Function was expected for 'options.query'"); } - func(client, req.query, params); // notify the client; + try { + func(client, req.query, params); // notify the client; + } catch (err) { + reject(err); + return; + } } try { client.query(req.query, params, function (err, result) { @@ -585,24 +590,13 @@ function $transact(obj, cb) { // Handles database connection acquire/release // events, notifying the client as needed. function $notify(open, db, opt) { - if (open) { - if (opt) { - var func = opt.connect; - if (func) { - if (typeof(func) !== 'function') { - throw new Error("Function was expected for 'options.connect'"); - } - func(db.client); // notify the client; - } - } - } else { - if (opt) { - var func = opt.disconnect; - if (func) { - if (typeof(func) !== 'function') { - throw new Error("Function was expected for 'options.disconnect'"); - } - func(db.client); // notify the client; + if (opt) { + var func = open ? opt.connect : opt.disconnect; + if (func) { + if (typeof(func) !== 'function') { + throw new Error("Function was expected for 'options." + (open ? "connect'" : "disconnect'")); + } else { + func(db.client); } } } diff --git a/package.json b/package.json index 2c5997c4..f2e81232 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pg-promise", - "version": "0.5.6", + "version": "0.5.7", "description": "PG + Promises/A+, with transactions support.", "main": "index.js", "scripts": {