errors.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. 'use strict';
  2. const ErrorCodes = require('../const/error-code');
  3. class SqlError extends Error {
  4. constructor(msg, fatal, info, sqlState, errno, additionalStack, addHeader) {
  5. super(
  6. (addHeader === undefined || addHeader
  7. ? '(conn=' +
  8. (info ? (info.threadId ? info.threadId : -1) : -1) +
  9. ', no: ' +
  10. (errno ? errno : -1) +
  11. ', SQLState: ' +
  12. (sqlState ? sqlState : 'HY000') +
  13. ') '
  14. : '') + msg
  15. );
  16. this.fatal = fatal;
  17. this.errno = errno;
  18. this.sqlState = sqlState;
  19. if (errno > 45000 && errno < 46000) {
  20. //driver error
  21. this.code = errByNo[errno] || 'UNKNOWN';
  22. } else {
  23. this.code = ErrorCodes.codes[this.errno] || 'UNKNOWN';
  24. }
  25. if (additionalStack) {
  26. //adding caller stack, removing initial "Error:\n"
  27. this.stack +=
  28. '\n From event:\n' + additionalStack.substring(additionalStack.indexOf('\n') + 1);
  29. }
  30. }
  31. }
  32. /**
  33. * Error factory, so error get connection information.
  34. *
  35. * @param msg current error message
  36. * @param fatal is error fatal
  37. * @param info connection information
  38. * @param sqlState sql state
  39. * @param errno error number
  40. * @param additionalStack additional stack trace to see
  41. * @param addHeader add connection information
  42. * @returns {Error} the error
  43. */
  44. module.exports.createError = function (
  45. msg,
  46. fatal,
  47. info,
  48. sqlState,
  49. errno,
  50. additionalStack,
  51. addHeader
  52. ) {
  53. return new SqlError(msg, fatal, info, sqlState, errno, additionalStack, addHeader);
  54. };
  55. /********************************************************************************
  56. * Driver specific errors
  57. ********************************************************************************/
  58. module.exports.ER_CONNECTION_ALREADY_CLOSED = 45001;
  59. module.exports.ER_ALREADY_CONNECTING = 45002;
  60. module.exports.ER_MYSQL_CHANGE_USER_BUG = 45003;
  61. module.exports.ER_CMD_NOT_EXECUTED_DESTROYED = 45004;
  62. module.exports.ER_NULL_CHAR_ESCAPEID = 45005;
  63. module.exports.ER_NULL_ESCAPEID = 45006;
  64. module.exports.ER_NOT_IMPLEMENTED_FORMAT = 45007;
  65. module.exports.ER_NODE_NOT_SUPPORTED_TLS = 45008;
  66. module.exports.ER_SOCKET_UNEXPECTED_CLOSE = 45009;
  67. module.exports.ER_UNEXPECTED_PACKET = 45011;
  68. module.exports.ER_CONNECTION_TIMEOUT = 45012;
  69. module.exports.ER_CMD_CONNECTION_CLOSED = 45013;
  70. module.exports.ER_CHANGE_USER_BAD_PACKET = 45014;
  71. module.exports.ER_PING_BAD_PACKET = 45015;
  72. module.exports.ER_MISSING_PARAMETER = 45016;
  73. module.exports.ER_PARAMETER_UNDEFINED = 45017;
  74. module.exports.ER_PLACEHOLDER_UNDEFINED = 45018;
  75. module.exports.ER_SOCKET = 45019;
  76. module.exports.ER_EOF_EXPECTED = 45020;
  77. module.exports.ER_LOCAL_INFILE_DISABLED = 45021;
  78. module.exports.ER_LOCAL_INFILE_NOT_READABLE = 45022;
  79. module.exports.ER_SERVER_SSL_DISABLED = 45023;
  80. module.exports.ER_AUTHENTICATION_BAD_PACKET = 45024;
  81. module.exports.ER_AUTHENTICATION_PLUGIN_NOT_SUPPORTED = 45025;
  82. module.exports.ER_SOCKET_TIMEOUT = 45026;
  83. module.exports.ER_POOL_ALREADY_CLOSED = 45027;
  84. module.exports.ER_GET_CONNECTION_TIMEOUT = 45028;
  85. module.exports.ER_SETTING_SESSION_ERROR = 45029;
  86. module.exports.ER_INITIAL_SQL_ERROR = 45030;
  87. module.exports.ER_BATCH_WITH_NO_VALUES = 45031;
  88. module.exports.ER_RESET_BAD_PACKET = 45032;
  89. module.exports.ER_WRONG_IANA_TIMEZONE = 45033;
  90. module.exports.ER_LOCAL_INFILE_WRONG_FILENAME = 45034;
  91. module.exports.ER_ADD_CONNECTION_CLOSED_POOL = 45035;
  92. module.exports.ER_WRONG_AUTO_TIMEZONE = 45036;
  93. module.exports.ER_CLOSING_POOL = 45037;
  94. module.exports.ER_TIMEOUT_NOT_SUPPORTED = 45038;
  95. module.exports.ER_INITIAL_TIMEOUT_ERROR = 45039;
  96. module.exports.ER_DUPLICATE_FIELD = 45040;
  97. module.exports.ER_CLIENT_OPTION_INCOMPATIBILITY = 45041;
  98. module.exports.ER_PING_TIMEOUT = 45042;
  99. module.exports.ER_BAD_PARAMETER_VALUE = 45043;
  100. module.exports.ER_CANNOT_RETRIEVE_RSA_KEY = 45044;
  101. module.exports.ER_MINIMUM_NODE_VERSION_REQUIRED = 45045;
  102. const keys = Object.keys(module.exports);
  103. const errByNo = {};
  104. for (let i = 0; i < keys.length; i++) {
  105. const keyName = keys[i];
  106. if (keyName !== 'createError') {
  107. errByNo[module.exports[keyName]] = keyName;
  108. }
  109. }
  110. module.exports.SqlError = SqlError;