prepared-statement-error.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.PreparedStatementError
  16. * @augments external:Error
  17. * @description
  18. * {@link errors.PreparedStatementError PreparedStatementError} class, available from the {@link errors} namespace.
  19. *
  20. * This type represents all errors that can be reported by class {@link PreparedStatement}, whether it is used
  21. * explicitly or implicitly (via a simple `{name, text, values}` object).
  22. *
  23. * @property {string} name
  24. * Standard {@link external:Error Error} property - error type name = `PreparedStatementError`.
  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 PreparedStatement} used a {@link QueryFile} which threw the error.
  36. *
  37. * @property {object} result
  38. * Resulting Prepared Statement object.
  39. *
  40. * @see PreparedStatement
  41. */
  42. class PreparedStatementError extends Error {
  43. constructor(error, ps) {
  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 = ps;
  52. Error.captureStackTrace(this, this.constructor);
  53. }
  54. }
  55. /**
  56. * @method errors.PreparedStatementError#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. PreparedStatementError.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. 'PreparedStatementError {',
  74. gap1 + 'message: "' + this.message + '"',
  75. gap1 + 'result: {',
  76. gap2 + 'name: ' + npm.utils.toJson(this.result.name),
  77. gap2 + 'text: ' + npm.utils.toJson(this.result.text),
  78. gap2 + 'values: ' + npm.utils.toJson(this.result.values),
  79. gap1 + '}'
  80. ];
  81. if (this.error) {
  82. lines.push(gap1 + 'error: ' + this.error.toString(level + 1));
  83. }
  84. lines.push(gap0 + '}');
  85. return lines.join(npm.os.EOL);
  86. };
  87. npm.utils.addInspection(PreparedStatementError, function () {
  88. return this.toString();
  89. });
  90. module.exports = {PreparedStatementError};