errors.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. 'use strict';
  2. /**
  3. * @module Errors
  4. */
  5. /**
  6. * Factory functions to create throwable error objects
  7. */
  8. /**
  9. * Creates an error object to be thrown when no files to be tested could be found using specified pattern.
  10. *
  11. * @public
  12. * @param {string} message - Error message to be displayed.
  13. * @param {string} pattern - User-specified argument value.
  14. * @returns {Error} instance detailing the error condition
  15. */
  16. function createNoFilesMatchPatternError(message, pattern) {
  17. var err = new Error(message);
  18. err.code = 'ERR_MOCHA_NO_FILES_MATCH_PATTERN';
  19. err.pattern = pattern;
  20. return err;
  21. }
  22. /**
  23. * Creates an error object to be thrown when the reporter specified in the options was not found.
  24. *
  25. * @public
  26. * @param {string} message - Error message to be displayed.
  27. * @param {string} reporter - User-specified reporter value.
  28. * @returns {Error} instance detailing the error condition
  29. */
  30. function createInvalidReporterError(message, reporter) {
  31. var err = new TypeError(message);
  32. err.code = 'ERR_MOCHA_INVALID_REPORTER';
  33. err.reporter = reporter;
  34. return err;
  35. }
  36. /**
  37. * Creates an error object to be thrown when the interface specified in the options was not found.
  38. *
  39. * @public
  40. * @param {string} message - Error message to be displayed.
  41. * @param {string} ui - User-specified interface value.
  42. * @returns {Error} instance detailing the error condition
  43. */
  44. function createInvalidInterfaceError(message, ui) {
  45. var err = new Error(message);
  46. err.code = 'ERR_MOCHA_INVALID_INTERFACE';
  47. err.interface = ui;
  48. return err;
  49. }
  50. /**
  51. * Creates an error object to be thrown when a behavior, option, or parameter is unsupported.
  52. *
  53. * @public
  54. * @param {string} message - Error message to be displayed.
  55. * @returns {Error} instance detailing the error condition
  56. */
  57. function createUnsupportedError(message) {
  58. var err = new Error(message);
  59. err.code = 'ERR_MOCHA_UNSUPPORTED';
  60. return err;
  61. }
  62. /**
  63. * Creates an error object to be thrown when an argument is missing.
  64. *
  65. * @public
  66. * @param {string} message - Error message to be displayed.
  67. * @param {string} argument - Argument name.
  68. * @param {string} expected - Expected argument datatype.
  69. * @returns {Error} instance detailing the error condition
  70. */
  71. function createMissingArgumentError(message, argument, expected) {
  72. return createInvalidArgumentTypeError(message, argument, expected);
  73. }
  74. /**
  75. * Creates an error object to be thrown when an argument did not use the supported type
  76. *
  77. * @public
  78. * @param {string} message - Error message to be displayed.
  79. * @param {string} argument - Argument name.
  80. * @param {string} expected - Expected argument datatype.
  81. * @returns {Error} instance detailing the error condition
  82. */
  83. function createInvalidArgumentTypeError(message, argument, expected) {
  84. var err = new TypeError(message);
  85. err.code = 'ERR_MOCHA_INVALID_ARG_TYPE';
  86. err.argument = argument;
  87. err.expected = expected;
  88. err.actual = typeof argument;
  89. return err;
  90. }
  91. /**
  92. * Creates an error object to be thrown when an argument did not use the supported value
  93. *
  94. * @public
  95. * @param {string} message - Error message to be displayed.
  96. * @param {string} argument - Argument name.
  97. * @param {string} value - Argument value.
  98. * @param {string} [reason] - Why value is invalid.
  99. * @returns {Error} instance detailing the error condition
  100. */
  101. function createInvalidArgumentValueError(message, argument, value, reason) {
  102. var err = new TypeError(message);
  103. err.code = 'ERR_MOCHA_INVALID_ARG_VALUE';
  104. err.argument = argument;
  105. err.value = value;
  106. err.reason = typeof reason !== 'undefined' ? reason : 'is invalid';
  107. return err;
  108. }
  109. /**
  110. * Creates an error object to be thrown when an exception was caught, but the `Error` is falsy or undefined.
  111. *
  112. * @public
  113. * @param {string} message - Error message to be displayed.
  114. * @returns {Error} instance detailing the error condition
  115. */
  116. function createInvalidExceptionError(message, value) {
  117. var err = new Error(message);
  118. err.code = 'ERR_MOCHA_INVALID_EXCEPTION';
  119. err.valueType = typeof value;
  120. err.value = value;
  121. return err;
  122. }
  123. module.exports = {
  124. createInvalidArgumentTypeError: createInvalidArgumentTypeError,
  125. createInvalidArgumentValueError: createInvalidArgumentValueError,
  126. createInvalidExceptionError: createInvalidExceptionError,
  127. createInvalidInterfaceError: createInvalidInterfaceError,
  128. createInvalidReporterError: createInvalidReporterError,
  129. createMissingArgumentError: createMissingArgumentError,
  130. createNoFilesMatchPatternError: createNoFilesMatchPatternError,
  131. createUnsupportedError: createUnsupportedError
  132. };