Skip to content

Commit

Permalink
enhancing PreparedStatementError.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Apr 25, 2016
1 parent eb1273e commit 12a6e3f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
23 changes: 17 additions & 6 deletions lib/errors/prepared.js
Expand Up @@ -26,13 +26,16 @@ var $npm = {
* Standard {@link external:Error Error} property - the stack trace.
*
* @property {QueryFileError} error
* Internal {@link QueryFileError} object.
* Internal {@link QueryFileError} object.
*
* It is set only when the source {@link PreparedStatement} used a {@link QueryFile} which threw the error.
*
* @property {Object} result
* Resulting Prepared Statement object.
*
* @returns {PreparedStatementError}
*/
function PreparedStatementError(error) {
function PreparedStatementError(error, ps) {
var temp = Error.apply(this, arguments);
temp.name = this.name = 'PreparedStatementError';
this.stack = temp.stack;
Expand All @@ -42,6 +45,7 @@ function PreparedStatementError(error) {
} else {
this.message = error;
}
this.result = ps;
}

PreparedStatementError.prototype = Object.create(Error.prototype, {
Expand All @@ -66,15 +70,22 @@ PreparedStatementError.prototype = Object.create(Error.prototype, {
*/
PreparedStatementError.prototype.toString = function (level) {
level = level > 0 ? parseInt(level) : 0;
var gap = $npm.utils.messageGap(level + 1);
var gap0 = $npm.utils.messageGap(level),
gap1 = $npm.utils.messageGap(level + 1),
gap2 = $npm.utils.messageGap(level + 2)
var lines = [
'PreparedStatementError {',
gap + 'message: "' + this.message + '"'
gap1 + 'message: "' + this.message + '"',
gap1 + 'result: {',
gap2 + 'name: ' + JSON.stringify(this.result.name),
gap2 + 'text: ' + JSON.stringify(this.result.text),
gap2 + 'values: ' + JSON.stringify(this.result.values),
gap1 + '}'
];
if (this.error) {
lines.push(gap + 'error: ' + this.error.toString(level + 1));
lines.push(gap1 + 'error: ' + this.error.toString(level + 1));
}
lines.push($npm.utils.messageGap(level) + '}');
lines.push(gap0 + '}');
return lines.join($npm.os.EOL);
};

Expand Down
39 changes: 22 additions & 17 deletions lib/prepared.js
Expand Up @@ -104,7 +104,7 @@ function PreparedStatement(name, text, values) {
* A non-empty query string or a {@link QueryFile} object.
*/
this.text = text;

/**
* @name PreparedStatement#values
* @type {Array}
Expand All @@ -118,7 +118,7 @@ function PreparedStatement(name, text, values) {
* @type {PreparedStatementError}
* @description
* When in an error state, it is set to a {@link PreparedStatementError} object. Otherwise, it is `undefined`.
*
*
* This property is meant primarily for internal use by the library.
*/
this.error = undefined;
Expand All @@ -144,29 +144,34 @@ function PreparedStatement(name, text, values) {
*/
PreparedStatement.prototype.parse = function () {
this.error = undefined;
var ps = {}, PSE = $npm.errors.PreparedStatementError;
if (!$npm.utils.isText(this.name)) {
return this.error = new PSE("Property 'name' must be a non-empty text string.");
var errors = [], ps = {
name: this.name,
text: this.text,
values: this.values
};
if (!$npm.utils.isText(ps.name)) {
errors.push("Property 'name' must be a non-empty text string.");
}
ps.name = this.name;
if (!$npm.utils.isNull(this.values)) {
if (!Array.isArray(this.values)) {
return this.error = new PSE("Property 'values' must be an array or null/undefined.");
if (!$npm.utils.isNull(ps.values)) {
if (!Array.isArray(ps.values)) {
errors.push("Property 'values' must be an array or null/undefined.");
}
ps.values = this.values;
}
if (this.text instanceof $npm.QueryFile) {
var qf = this.text;
if (ps.text instanceof $npm.QueryFile) {
var qf = ps.text;
qf.prepare();
if (qf.error) {
return this.error = new PSE(qf.error);
errors.push(qf.error);
} else {
ps.text = qf.query;
}
ps.text = qf.query;
} else {
ps.text = this.text;
}
if (!$npm.utils.isText(ps.text)) {
return this.error = new PSE("Property 'text' must be a non-empty text string.");
errors.push("Property 'text' must be a non-empty text string.");
}
if (errors.length) {
this.error = new $npm.errors.PreparedStatementError(errors[0], ps);
return this.error;
}
return ps;
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "pg-promise",
"version": "4.0.1",
"version": "4.0.2",
"description": "PostgreSQL via promises",
"main": "lib/index.js",
"scripts": {
Expand Down

0 comments on commit 12a6e3f

Please sign in to comment.