Update to NPM version

This commit is contained in:
Lucas Patenaude
2024-04-11 04:23:19 -06:00
parent 886d197fa7
commit 6d6ef4f257
8225 changed files with 863748 additions and 1 deletions

View File

@@ -0,0 +1,13 @@
### `errors` namespace
This folder contains everything that's available via the [errors] namespace, before and after initialization:
```js
const pgpLib = require('pg-promise');
const pgp = pgpLib(/*initialization options*/);
pgpLib.errors; // `errors` namespace
pgp.errors; // `errors` namespace
```
[errors]:http://vitaly-t.github.io/pg-promise/errors.html

View File

@@ -0,0 +1,51 @@
/*
* Copyright (c) 2015-present, Vitaly Tomilov
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Removal or modification of this copyright notice is prohibited.
*/
const {QueryResultError, queryResultErrorCode} = require(`./query-result-error`);
const {PreparedStatementError} = require(`./prepared-statement-error`);
const {ParameterizedQueryError} = require(`./parameterized-query-error`);
const {QueryFileError} = require(`./query-file-error`);
/**
* @namespace errors
* @description
* Error types namespace, available as `pgp.errors`, before and after initializing the library.
*
* @property {function} PreparedStatementError
* {@link errors.PreparedStatementError PreparedStatementError} class constructor.
*
* Represents all errors that can be reported by class {@link PreparedStatement}.
*
* @property {function} ParameterizedQueryError
* {@link errors.ParameterizedQueryError ParameterizedQueryError} class constructor.
*
* Represents all errors that can be reported by class {@link ParameterizedQuery}.
*
* @property {function} QueryFileError
* {@link errors.QueryFileError QueryFileError} class constructor.
*
* Represents all errors that can be reported by class {@link QueryFile}.
*
* @property {function} QueryResultError
* {@link errors.QueryResultError QueryResultError} class constructor.
*
* Represents all result-specific errors from query methods.
*
* @property {errors.queryResultErrorCode} queryResultErrorCode
* Error codes `enum` used by class {@link errors.QueryResultError QueryResultError}.
*
*/
module.exports = {
QueryResultError,
queryResultErrorCode,
PreparedStatementError,
ParameterizedQueryError,
QueryFileError
};

View File

@@ -0,0 +1,95 @@
/*
* Copyright (c) 2015-present, Vitaly Tomilov
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Removal or modification of this copyright notice is prohibited.
*/
const {QueryFileError} = require(`./query-file-error`);
const npm = {
os: require(`os`),
utils: require(`../utils`)
};
/**
* @class errors.ParameterizedQueryError
* @augments external:Error
* @description
* {@link errors.ParameterizedQueryError ParameterizedQueryError} class, available from the {@link errors} namespace.
*
* This type represents all errors that can be reported by class {@link ParameterizedQuery}, whether it is used
* explicitly or implicitly (via a simple `{text, values}` object).
*
* @property {string} name
* Standard {@link external:Error Error} property - error type name = `ParameterizedQueryError`.
*
* @property {string} message
* Standard {@link external:Error Error} property - the error message.
*
* @property {string} stack
* Standard {@link external:Error Error} property - the stack trace.
*
* @property {errors.QueryFileError} error
* Internal {@link errors.QueryFileError} object.
*
* It is set only when the source {@link ParameterizedQuery} used a {@link QueryFile} which threw the error.
*
* @property {object} result
* Resulting Parameterized Query object.
*
* @see ParameterizedQuery
*/
class ParameterizedQueryError extends Error {
constructor(error, pq) {
const isQueryFileError = error instanceof QueryFileError;
const message = isQueryFileError ? `Failed to initialize 'text' from a QueryFile.` : error;
super(message);
this.name = this.constructor.name;
if (isQueryFileError) {
this.error = error;
}
this.result = pq;
Error.captureStackTrace(this, this.constructor);
}
}
/**
* @method errors.ParameterizedQueryError#toString
* @description
* Creates a well-formatted multi-line string that represents the error.
*
* It is called automatically when writing the object into the console.
*
* @param {number} [level=0]
* Nested output level, to provide visual offset.
*
* @returns {string}
*/
ParameterizedQueryError.prototype.toString = function (level) {
level = level > 0 ? parseInt(level) : 0;
const gap0 = npm.utils.messageGap(level),
gap1 = npm.utils.messageGap(level + 1),
gap2 = npm.utils.messageGap(level + 2),
lines = [
`ParameterizedQueryError {`,
gap1 + `message: "` + this.message + `"`,
gap1 + `result: {`,
gap2 + `text: ` + npm.utils.toJson(this.result.text),
gap2 + `values: ` + npm.utils.toJson(this.result.values),
gap1 + `}`
];
if (this.error) {
lines.push(gap1 + `error: ` + this.error.toString(level + 1));
}
lines.push(gap0 + `}`);
return lines.join(npm.os.EOL);
};
npm.utils.addInspection(ParameterizedQueryError, function () {
return this.toString();
});
module.exports = {ParameterizedQueryError};

View File

@@ -0,0 +1,96 @@
/*
* Copyright (c) 2015-present, Vitaly Tomilov
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Removal or modification of this copyright notice is prohibited.
*/
const {QueryFileError} = require(`./query-file-error`);
const npm = {
os: require(`os`),
utils: require(`../utils`)
};
/**
* @class errors.PreparedStatementError
* @augments external:Error
* @description
* {@link errors.PreparedStatementError PreparedStatementError} class, available from the {@link errors} namespace.
*
* This type represents all errors that can be reported by class {@link PreparedStatement}, whether it is used
* explicitly or implicitly (via a simple `{name, text, values}` object).
*
* @property {string} name
* Standard {@link external:Error Error} property - error type name = `PreparedStatementError`.
*
* @property {string} message
* Standard {@link external:Error Error} property - the error message.
*
* @property {string} stack
* Standard {@link external:Error Error} property - the stack trace.
*
* @property {errors.QueryFileError} error
* Internal {@link errors.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.
*
* @see PreparedStatement
*/
class PreparedStatementError extends Error {
constructor(error, ps) {
const isQueryFileError = error instanceof QueryFileError;
const message = isQueryFileError ? `Failed to initialize 'text' from a QueryFile.` : error;
super(message);
this.name = this.constructor.name;
if (isQueryFileError) {
this.error = error;
}
this.result = ps;
Error.captureStackTrace(this, this.constructor);
}
}
/**
* @method errors.PreparedStatementError#toString
* @description
* Creates a well-formatted multi-line string that represents the error.
*
* It is called automatically when writing the object into the console.
*
* @param {number} [level=0]
* Nested output level, to provide visual offset.
*
* @returns {string}
*/
PreparedStatementError.prototype.toString = function (level) {
level = level > 0 ? parseInt(level) : 0;
const gap0 = npm.utils.messageGap(level),
gap1 = npm.utils.messageGap(level + 1),
gap2 = npm.utils.messageGap(level + 2),
lines = [
`PreparedStatementError {`,
gap1 + `message: "` + this.message + `"`,
gap1 + `result: {`,
gap2 + `name: ` + npm.utils.toJson(this.result.name),
gap2 + `text: ` + npm.utils.toJson(this.result.text),
gap2 + `values: ` + npm.utils.toJson(this.result.values),
gap1 + `}`
];
if (this.error) {
lines.push(gap1 + `error: ` + this.error.toString(level + 1));
}
lines.push(gap0 + `}`);
return lines.join(npm.os.EOL);
};
npm.utils.addInspection(PreparedStatementError, function () {
return this.toString();
});
module.exports = {PreparedStatementError};

View File

@@ -0,0 +1,95 @@
/*
* Copyright (c) 2015-present, Vitaly Tomilov
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Removal or modification of this copyright notice is prohibited.
*/
const npm = {
os: require(`os`),
utils: require(`../utils`),
minify: require(`pg-minify`)
};
/**
* @class errors.QueryFileError
* @augments external:Error
* @description
* {@link errors.QueryFileError QueryFileError} class, available from the {@link errors} namespace.
*
* This type represents all errors related to {@link QueryFile}.
*
* @property {string} name
* Standard {@link external:Error Error} property - error type name = `QueryFileError`.
*
* @property {string} message
* Standard {@link external:Error Error} property - the error message.
*
* @property {string} stack
* Standard {@link external:Error Error} property - the stack trace.
*
* @property {string} file
* File path/name that was passed into the {@link QueryFile} constructor.
*
* @property {object} options
* Set of options that was used by the {@link QueryFile} object.
*
* @property {SQLParsingError} error
* Internal $[SQLParsingError] object.
*
* It is set only when the error was thrown by $[pg-minify] while parsing the SQL file.
*
* @see QueryFile
*
*/
class QueryFileError extends Error {
constructor(error, qf) {
const isSqlError = error instanceof npm.minify.SQLParsingError;
const message = isSqlError ? `Failed to parse the SQL.` : error.message;
super(message);
this.name = this.constructor.name;
if (isSqlError) {
this.error = error;
}
this.file = qf.file;
this.options = qf.options;
Error.captureStackTrace(this, this.constructor);
}
}
/**
* @method errors.QueryFileError#toString
* @description
* Creates a well-formatted multi-line string that represents the error.
*
* It is called automatically when writing the object into the console.
*
* @param {number} [level=0]
* Nested output level, to provide visual offset.
*
* @returns {string}
*/
QueryFileError.prototype.toString = function (level) {
level = level > 0 ? parseInt(level) : 0;
const gap0 = npm.utils.messageGap(level),
gap1 = npm.utils.messageGap(level + 1),
lines = [
`QueryFileError {`,
gap1 + `message: "` + this.message + `"`,
gap1 + `options: ` + npm.utils.toJson(this.options),
gap1 + `file: "` + this.file + `"`
];
if (this.error) {
lines.push(gap1 + `error: ` + this.error.toString(level + 1));
}
lines.push(gap0 + `}`);
return lines.join(npm.os.EOL);
};
npm.utils.addInspection(QueryFileError, function () {
return this.toString();
});
module.exports = {QueryFileError};

View File

@@ -0,0 +1,177 @@
/*
* Copyright (c) 2015-present, Vitaly Tomilov
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Removal or modification of this copyright notice is prohibited.
*/
const npm = {
os: require(`os`),
utils: require(`../utils`),
text: require(`../text`)
};
/**
* @enum {number}
* @alias errors.queryResultErrorCode
* @readonly
* @description
* `queryResultErrorCode` enumerator, available from the {@link errors} namespace.
*
* Represents an integer code for each type of error supported by type {@link errors.QueryResultError}.
*
* @see {@link errors.QueryResultError}
*/
const queryResultErrorCode = {
/** No data returned from the query. */
noData: 0,
/** No return data was expected. */
notEmpty: 1,
/** Multiple rows were not expected. */
multiple: 2
};
const errorMessages = [
{name: `noData`, message: npm.text.noData},
{name: `notEmpty`, message: npm.text.notEmpty},
{name: `multiple`, message: npm.text.multiple}
];
/**
* @class errors.QueryResultError
* @augments external:Error
* @description
*
* This error is specified as the rejection reason for all result-specific methods when the result doesn't match
* the expectation, i.e. when a query result doesn't match its Query Result Mask - the value of {@link queryResult}.
*
* The error applies to the result from the following methods: {@link Database#none none},
* {@link Database#one one}, {@link Database#oneOrNone oneOrNone} and {@link Database#many many}.
*
* Supported errors:
*
* - `No return data was expected.`, method {@link Database#none none}
* - `No data returned from the query.`, methods {@link Database#one one} and {@link Database#many many}
* - `Multiple rows were not expected.`, methods {@link Database#one one} and {@link Database#oneOrNone oneOrNone}
*
* Like any other error, this one is notified with through the global event {@link event:error error}.
*
* The type is available from the {@link errors} namespace.
*
* @property {string} name
* Standard {@link external:Error Error} property - error type name = `QueryResultError`.
*
* @property {string} message
* Standard {@link external:Error Error} property - the error message.
*
* @property {string} stack
* Standard {@link external:Error Error} property - the stack trace.
*
* @property {object} result
* The original $[Result] object that was received.
*
* @property {number} received
* Total number of rows received. It is simply the value of `result.rows.length`.
*
* @property {number} code
* Error code - {@link errors.queryResultErrorCode queryResultErrorCode} value.
*
* @property {string} query
* Query that was executed.
*
* Normally, it is the query already formatted with values, if there were any.
* But if you are using initialization option `pgFormatting`, then the query string is before formatting.
*
* @property {*} values
* Values passed in as query parameters. Available only when initialization option `pgFormatting` is used.
* Otherwise, the values are within the pre-formatted `query` string.
*
* @example
*
* const QueryResultError = pgp.errors.QueryResultError;
* const qrec = pgp.errors.queryResultErrorCode;
*
* const initOptions = {
*
* // pg-promise initialization options...
*
* error: (err, e) => {
* if (err instanceof QueryResultError) {
* // A query returned unexpected number of records, and thus rejected;
*
* // we can check the error code, if we want specifics:
* if(err.code === qrec.noData) {
* // expected some data, but received none;
* }
*
* // If you write QueryResultError into the console,
* // you will get a nicely formatted output.
*
* console.log(err);
*
* // See also: err, e.query, e.params, etc.
* }
* }
* };
*
* @see
* {@link queryResult}, {@link Database#none none}, {@link Database#one one},
* {@link Database#oneOrNone oneOrNone}, {@link Database#many many}
*
*/
class QueryResultError extends Error {
constructor(code, result, query, values) {
const message = errorMessages[code].message;
super(message);
this.name = this.constructor.name;
this.code = code;
this.result = result;
this.query = query;
this.values = values;
this.received = result.rows.length;
Error.captureStackTrace(this, this.constructor);
}
}
/**
* @method errors.QueryResultError#toString
* @description
* Creates a well-formatted multi-line string that represents the error.
*
* It is called automatically when writing the object into the console.
*
* @param {number} [level=0]
* Nested output level, to provide visual offset.
*
* @returns {string}
*/
QueryResultError.prototype.toString = function (level) {
level = level > 0 ? parseInt(level) : 0;
const gap0 = npm.utils.messageGap(level),
gap1 = npm.utils.messageGap(level + 1),
lines = [
`QueryResultError {`,
gap1 + `code: queryResultErrorCode.` + errorMessages[this.code].name,
gap1 + `message: "` + this.message + `"`,
gap1 + `received: ` + this.received,
gap1 + `query: ` + (typeof this.query === `string` ? `"` + this.query + `"` : npm.utils.toJson(this.query))
];
if (this.values !== undefined) {
lines.push(gap1 + `values: ` + npm.utils.toJson(this.values));
}
lines.push(gap0 + `}`);
return lines.join(npm.os.EOL);
};
npm.utils.addInspection(QueryResultError, function () {
return this.toString();
});
module.exports = {
QueryResultError,
queryResultErrorCode
};