Skip to content

Commit

Permalink
- minor code refactoring;
Browse files Browse the repository at this point in the history
- added exception handler for 'query' notification;
  • Loading branch information
vitaly-t committed Mar 22, 2015
1 parent e640b2d commit 859da39
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
32 changes: 13 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit 859da39

Please sign in to comment.