query-file-error.js 2.8 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 npm = {
  10. os: require('os'),
  11. utils: require('../utils'),
  12. minify: require('pg-minify')
  13. };
  14. /**
  15. * @class errors.QueryFileError
  16. * @augments external:Error
  17. * @description
  18. * {@link errors.QueryFileError QueryFileError} class, available from the {@link errors} namespace.
  19. *
  20. * This type represents all errors related to {@link QueryFile}.
  21. *
  22. * @property {string} name
  23. * Standard {@link external:Error Error} property - error type name = `QueryFileError`.
  24. *
  25. * @property {string} message
  26. * Standard {@link external:Error Error} property - the error message.
  27. *
  28. * @property {string} stack
  29. * Standard {@link external:Error Error} property - the stack trace.
  30. *
  31. * @property {string} file
  32. * File path/name that was passed into the {@link QueryFile} constructor.
  33. *
  34. * @property {object} options
  35. * Set of options that was used by the {@link QueryFile} object.
  36. *
  37. * @property {SQLParsingError} error
  38. * Internal $[SQLParsingError] object.
  39. *
  40. * It is set only when the error was thrown by $[pg-minify] while parsing the SQL file.
  41. *
  42. * @see QueryFile
  43. *
  44. */
  45. class QueryFileError extends Error {
  46. constructor(error, qf) {
  47. const isSqlError = error instanceof npm.minify.SQLParsingError;
  48. const message = isSqlError ? 'Failed to parse the SQL.' : error.message;
  49. super(message);
  50. this.name = this.constructor.name;
  51. if (isSqlError) {
  52. this.error = error;
  53. }
  54. this.file = qf.file;
  55. this.options = qf.options;
  56. Error.captureStackTrace(this, this.constructor);
  57. }
  58. }
  59. /**
  60. * @method errors.QueryFileError#toString
  61. * @description
  62. * Creates a well-formatted multi-line string that represents the error.
  63. *
  64. * It is called automatically when writing the object into the console.
  65. *
  66. * @param {number} [level=0]
  67. * Nested output level, to provide visual offset.
  68. *
  69. * @returns {string}
  70. */
  71. QueryFileError.prototype.toString = function (level) {
  72. level = level > 0 ? parseInt(level) : 0;
  73. const gap0 = npm.utils.messageGap(level),
  74. gap1 = npm.utils.messageGap(level + 1),
  75. lines = [
  76. 'QueryFileError {',
  77. gap1 + 'message: "' + this.message + '"',
  78. gap1 + 'options: ' + npm.utils.toJson(this.options),
  79. gap1 + 'file: "' + this.file + '"'
  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(QueryFileError, function () {
  88. return this.toString();
  89. });
  90. module.exports = {QueryFileError};