parameterized-query-error.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2015-present, Vitaly Tomilov
  3. *
  4. * See the LICENSE file at the top-level directory of this distribution
  5. * for licensing information.
  6. *
  7. * Removal or modification of this copyright notice is prohibited.
  8. */
  9. const {QueryFileError} = require('./query-file-error');
  10. const npm = {
  11. os: require('os'),
  12. utils: require('../utils')
  13. };
  14. /**
  15. * @class errors.ParameterizedQueryError
  16. * @augments external:Error
  17. * @description
  18. * {@link errors.ParameterizedQueryError ParameterizedQueryError} class, available from the {@link errors} namespace.
  19. *
  20. * This type represents all errors that can be reported by class {@link ParameterizedQuery}, whether it is used
  21. * explicitly or implicitly (via a simple `{text, values}` object).
  22. *
  23. * @property {string} name
  24. * Standard {@link external:Error Error} property - error type name = `ParameterizedQueryError`.
  25. *
  26. * @property {string} message
  27. * Standard {@link external:Error Error} property - the error message.
  28. *
  29. * @property {string} stack
  30. * Standard {@link external:Error Error} property - the stack trace.
  31. *
  32. * @property {errors.QueryFileError} error
  33. * Internal {@link errors.QueryFileError} object.
  34. *
  35. * It is set only when the source {@link ParameterizedQuery} used a {@link QueryFile} which threw the error.
  36. *
  37. * @property {object} result
  38. * Resulting Parameterized Query object.
  39. *
  40. * @see ParameterizedQuery
  41. */
  42. class ParameterizedQueryError extends Error {
  43. constructor(error, pq) {
  44. const isQueryFileError = error instanceof QueryFileError;
  45. const message = isQueryFileError ? 'Failed to initialize \'text\' from a QueryFile.' : error;
  46. super(message);
  47. this.name = this.constructor.name;
  48. if (isQueryFileError) {
  49. this.error = error;
  50. }
  51. this.result = pq;
  52. Error.captureStackTrace(this, this.constructor);
  53. }
  54. }
  55. /**
  56. * @method errors.ParameterizedQueryError#toString
  57. * @description
  58. * Creates a well-formatted multi-line string that represents the error.
  59. *
  60. * It is called automatically when writing the object into the console.
  61. *
  62. * @param {number} [level=0]
  63. * Nested output level, to provide visual offset.
  64. *
  65. * @returns {string}
  66. */
  67. ParameterizedQueryError.prototype.toString = function (level) {
  68. level = level > 0 ? parseInt(level) : 0;
  69. const gap0 = npm.utils.messageGap(level),
  70. gap1 = npm.utils.messageGap(level + 1),
  71. gap2 = npm.utils.messageGap(level + 2),
  72. lines = [
  73. 'ParameterizedQueryError {',
  74. gap1 + 'message: "' + this.message + '"',
  75. gap1 + 'result: {',
  76. gap2 + 'text: ' + npm.utils.toJson(this.result.text),
  77. gap2 + 'values: ' + npm.utils.toJson(this.result.values),
  78. gap1 + '}'
  79. ];
  80. if (this.error) {
  81. lines.push(gap1 + 'error: ' + this.error.toString(level + 1));
  82. }
  83. lines.push(gap0 + '}');
  84. return lines.join(npm.os.EOL);
  85. };
  86. npm.utils.addInspection(ParameterizedQueryError, function () {
  87. return this.toString();
  88. });
  89. module.exports = {ParameterizedQueryError};