Dieses Repository beinhaltet HTML- und Javascript Code zur einer NotizenWebApp auf Basis von Web Storage. Zudem sind Mocha/Chai Tests im Browser enthalten. https://meinenotizen.netlify.app/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

errors.js 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. 'use strict';
  2. var format = require('util').format;
  3. /**
  4. * Factory functions to create throwable error objects
  5. * @module Errors
  6. */
  7. /**
  8. * When Mocha throw exceptions (or otherwise errors), it attempts to assign a
  9. * `code` property to the `Error` object, for easier handling. These are the
  10. * potential values of `code`.
  11. */
  12. var constants = {
  13. /**
  14. * An unrecoverable error.
  15. */
  16. FATAL: 'ERR_MOCHA_FATAL',
  17. /**
  18. * The type of an argument to a function call is invalid
  19. */
  20. INVALID_ARG_TYPE: 'ERR_MOCHA_INVALID_ARG_TYPE',
  21. /**
  22. * The value of an argument to a function call is invalid
  23. */
  24. INVALID_ARG_VALUE: 'ERR_MOCHA_INVALID_ARG_VALUE',
  25. /**
  26. * Something was thrown, but it wasn't an `Error`
  27. */
  28. INVALID_EXCEPTION: 'ERR_MOCHA_INVALID_EXCEPTION',
  29. /**
  30. * An interface (e.g., `Mocha.interfaces`) is unknown or invalid
  31. */
  32. INVALID_INTERFACE: 'ERR_MOCHA_INVALID_INTERFACE',
  33. /**
  34. * A reporter (.e.g, `Mocha.reporters`) is unknown or invalid
  35. */
  36. INVALID_REPORTER: 'ERR_MOCHA_INVALID_REPORTER',
  37. /**
  38. * `done()` was called twice in a `Test` or `Hook` callback
  39. */
  40. MULTIPLE_DONE: 'ERR_MOCHA_MULTIPLE_DONE',
  41. /**
  42. * No files matched the pattern provided by the user
  43. */
  44. NO_FILES_MATCH_PATTERN: 'ERR_MOCHA_NO_FILES_MATCH_PATTERN',
  45. /**
  46. * Known, but unsupported behavior of some kind
  47. */
  48. UNSUPPORTED: 'ERR_MOCHA_UNSUPPORTED',
  49. /**
  50. * Invalid state transition occuring in `Mocha` instance
  51. */
  52. INSTANCE_ALREADY_RUNNING: 'ERR_MOCHA_INSTANCE_ALREADY_RUNNING',
  53. /**
  54. * Invalid state transition occuring in `Mocha` instance
  55. */
  56. INSTANCE_ALREADY_DISPOSED: 'ERR_MOCHA_INSTANCE_ALREADY_DISPOSED'
  57. };
  58. /**
  59. * Creates an error object to be thrown when no files to be tested could be found using specified pattern.
  60. *
  61. * @public
  62. * @param {string} message - Error message to be displayed.
  63. * @param {string} pattern - User-specified argument value.
  64. * @returns {Error} instance detailing the error condition
  65. */
  66. function createNoFilesMatchPatternError(message, pattern) {
  67. var err = new Error(message);
  68. err.code = constants.NO_FILES_MATCH_PATTERN;
  69. err.pattern = pattern;
  70. return err;
  71. }
  72. /**
  73. * Creates an error object to be thrown when the reporter specified in the options was not found.
  74. *
  75. * @public
  76. * @param {string} message - Error message to be displayed.
  77. * @param {string} reporter - User-specified reporter value.
  78. * @returns {Error} instance detailing the error condition
  79. */
  80. function createInvalidReporterError(message, reporter) {
  81. var err = new TypeError(message);
  82. err.code = constants.INVALID_REPORTER;
  83. err.reporter = reporter;
  84. return err;
  85. }
  86. /**
  87. * Creates an error object to be thrown when the interface specified in the options was not found.
  88. *
  89. * @public
  90. * @param {string} message - Error message to be displayed.
  91. * @param {string} ui - User-specified interface value.
  92. * @returns {Error} instance detailing the error condition
  93. */
  94. function createInvalidInterfaceError(message, ui) {
  95. var err = new Error(message);
  96. err.code = constants.INVALID_INTERFACE;
  97. err.interface = ui;
  98. return err;
  99. }
  100. /**
  101. * Creates an error object to be thrown when a behavior, option, or parameter is unsupported.
  102. *
  103. * @public
  104. * @param {string} message - Error message to be displayed.
  105. * @returns {Error} instance detailing the error condition
  106. */
  107. function createUnsupportedError(message) {
  108. var err = new Error(message);
  109. err.code = constants.UNSUPPORTED;
  110. return err;
  111. }
  112. /**
  113. * Creates an error object to be thrown when an argument is missing.
  114. *
  115. * @public
  116. * @param {string} message - Error message to be displayed.
  117. * @param {string} argument - Argument name.
  118. * @param {string} expected - Expected argument datatype.
  119. * @returns {Error} instance detailing the error condition
  120. */
  121. function createMissingArgumentError(message, argument, expected) {
  122. return createInvalidArgumentTypeError(message, argument, expected);
  123. }
  124. /**
  125. * Creates an error object to be thrown when an argument did not use the supported type
  126. *
  127. * @public
  128. * @param {string} message - Error message to be displayed.
  129. * @param {string} argument - Argument name.
  130. * @param {string} expected - Expected argument datatype.
  131. * @returns {Error} instance detailing the error condition
  132. */
  133. function createInvalidArgumentTypeError(message, argument, expected) {
  134. var err = new TypeError(message);
  135. err.code = constants.INVALID_ARG_TYPE;
  136. err.argument = argument;
  137. err.expected = expected;
  138. err.actual = typeof argument;
  139. return err;
  140. }
  141. /**
  142. * Creates an error object to be thrown when an argument did not use the supported value
  143. *
  144. * @public
  145. * @param {string} message - Error message to be displayed.
  146. * @param {string} argument - Argument name.
  147. * @param {string} value - Argument value.
  148. * @param {string} [reason] - Why value is invalid.
  149. * @returns {Error} instance detailing the error condition
  150. */
  151. function createInvalidArgumentValueError(message, argument, value, reason) {
  152. var err = new TypeError(message);
  153. err.code = constants.INVALID_ARG_VALUE;
  154. err.argument = argument;
  155. err.value = value;
  156. err.reason = typeof reason !== 'undefined' ? reason : 'is invalid';
  157. return err;
  158. }
  159. /**
  160. * Creates an error object to be thrown when an exception was caught, but the `Error` is falsy or undefined.
  161. *
  162. * @public
  163. * @param {string} message - Error message to be displayed.
  164. * @returns {Error} instance detailing the error condition
  165. */
  166. function createInvalidExceptionError(message, value) {
  167. var err = new Error(message);
  168. err.code = constants.INVALID_EXCEPTION;
  169. err.valueType = typeof value;
  170. err.value = value;
  171. return err;
  172. }
  173. /**
  174. * Creates an error object to be thrown when an unrecoverable error occurs.
  175. *
  176. * @public
  177. * @param {string} message - Error message to be displayed.
  178. * @returns {Error} instance detailing the error condition
  179. */
  180. function createFatalError(message, value) {
  181. var err = new Error(message);
  182. err.code = constants.FATAL;
  183. err.valueType = typeof value;
  184. err.value = value;
  185. return err;
  186. }
  187. /**
  188. * Dynamically creates a plugin-type-specific error based on plugin type
  189. * @param {string} message - Error message
  190. * @param {"reporter"|"interface"} pluginType - Plugin type. Future: expand as needed
  191. * @param {string} [pluginId] - Name/path of plugin, if any
  192. * @throws When `pluginType` is not known
  193. * @public
  194. * @returns {Error}
  195. */
  196. function createInvalidPluginError(message, pluginType, pluginId) {
  197. switch (pluginType) {
  198. case 'reporter':
  199. return createInvalidReporterError(message, pluginId);
  200. case 'interface':
  201. return createInvalidInterfaceError(message, pluginId);
  202. default:
  203. throw new Error('unknown pluginType "' + pluginType + '"');
  204. }
  205. }
  206. /**
  207. * Creates an error object to be thrown when a mocha object's `run` method is executed while it is already disposed.
  208. * @param {string} message The error message to be displayed.
  209. * @param {boolean} cleanReferencesAfterRun the value of `cleanReferencesAfterRun`
  210. * @param {Mocha} instance the mocha instance that throw this error
  211. */
  212. function createMochaInstanceAlreadyDisposedError(
  213. message,
  214. cleanReferencesAfterRun,
  215. instance
  216. ) {
  217. var err = new Error(message);
  218. err.code = constants.INSTANCE_ALREADY_DISPOSED;
  219. err.cleanReferencesAfterRun = cleanReferencesAfterRun;
  220. err.instance = instance;
  221. return err;
  222. }
  223. /**
  224. * Creates an error object to be thrown when a mocha object's `run` method is called while a test run is in progress.
  225. * @param {string} message The error message to be displayed.
  226. */
  227. function createMochaInstanceAlreadyRunningError(message, instance) {
  228. var err = new Error(message);
  229. err.code = constants.INSTANCE_ALREADY_RUNNING;
  230. err.instance = instance;
  231. return err;
  232. }
  233. /*
  234. * Creates an error object to be thrown when done() is called multiple times in a test
  235. *
  236. * @public
  237. * @param {Runnable} runnable - Original runnable
  238. * @param {Error} [originalErr] - Original error, if any
  239. * @returns {Error} instance detailing the error condition
  240. */
  241. function createMultipleDoneError(runnable, originalErr) {
  242. var title;
  243. try {
  244. title = format('<%s>', runnable.fullTitle());
  245. if (runnable.parent.root) {
  246. title += ' (of root suite)';
  247. }
  248. } catch (ignored) {
  249. title = format('<%s> (of unknown suite)', runnable.title);
  250. }
  251. var message = format(
  252. 'done() called multiple times in %s %s',
  253. runnable.type ? runnable.type : 'unknown runnable',
  254. title
  255. );
  256. if (runnable.file) {
  257. message += format(' of file %s', runnable.file);
  258. }
  259. if (originalErr) {
  260. message += format('; in addition, done() received error: %s', originalErr);
  261. }
  262. var err = new Error(message);
  263. err.code = constants.MULTIPLE_DONE;
  264. err.valueType = typeof originalErr;
  265. err.value = originalErr;
  266. return err;
  267. }
  268. module.exports = {
  269. createInvalidArgumentTypeError: createInvalidArgumentTypeError,
  270. createInvalidArgumentValueError: createInvalidArgumentValueError,
  271. createInvalidExceptionError: createInvalidExceptionError,
  272. createInvalidInterfaceError: createInvalidInterfaceError,
  273. createInvalidReporterError: createInvalidReporterError,
  274. createMissingArgumentError: createMissingArgumentError,
  275. createNoFilesMatchPatternError: createNoFilesMatchPatternError,
  276. createUnsupportedError: createUnsupportedError,
  277. createInvalidPluginError: createInvalidPluginError,
  278. createMochaInstanceAlreadyDisposedError: createMochaInstanceAlreadyDisposedError,
  279. createMochaInstanceAlreadyRunningError: createMochaInstanceAlreadyRunningError,
  280. createFatalError: createFatalError,
  281. createMultipleDoneError: createMultipleDoneError,
  282. constants: constants
  283. };