index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. 'use strict';
  2. /* !
  3. * Chai - checkError utility
  4. * Copyright(c) 2012-2016 Jake Luer <jake@alogicalparadox.com>
  5. * MIT Licensed
  6. */
  7. var getFunctionName = require('get-func-name');
  8. /**
  9. * ### .checkError
  10. *
  11. * Checks that an error conforms to a given set of criteria and/or retrieves information about it.
  12. *
  13. * @api public
  14. */
  15. /**
  16. * ### .compatibleInstance(thrown, errorLike)
  17. *
  18. * Checks if two instances are compatible (strict equal).
  19. * Returns false if errorLike is not an instance of Error, because instances
  20. * can only be compatible if they're both error instances.
  21. *
  22. * @name compatibleInstance
  23. * @param {Error} thrown error
  24. * @param {Error|ErrorConstructor} errorLike object to compare against
  25. * @namespace Utils
  26. * @api public
  27. */
  28. function compatibleInstance(thrown, errorLike) {
  29. return errorLike instanceof Error && thrown === errorLike;
  30. }
  31. /**
  32. * ### .compatibleConstructor(thrown, errorLike)
  33. *
  34. * Checks if two constructors are compatible.
  35. * This function can receive either an error constructor or
  36. * an error instance as the `errorLike` argument.
  37. * Constructors are compatible if they're the same or if one is
  38. * an instance of another.
  39. *
  40. * @name compatibleConstructor
  41. * @param {Error} thrown error
  42. * @param {Error|ErrorConstructor} errorLike object to compare against
  43. * @namespace Utils
  44. * @api public
  45. */
  46. function compatibleConstructor(thrown, errorLike) {
  47. if (errorLike instanceof Error) {
  48. // If `errorLike` is an instance of any error we compare their constructors
  49. return thrown.constructor === errorLike.constructor || thrown instanceof errorLike.constructor;
  50. } else if (errorLike.prototype instanceof Error || errorLike === Error) {
  51. // If `errorLike` is a constructor that inherits from Error, we compare `thrown` to `errorLike` directly
  52. return thrown.constructor === errorLike || thrown instanceof errorLike;
  53. }
  54. return false;
  55. }
  56. /**
  57. * ### .compatibleMessage(thrown, errMatcher)
  58. *
  59. * Checks if an error's message is compatible with a matcher (String or RegExp).
  60. * If the message contains the String or passes the RegExp test,
  61. * it is considered compatible.
  62. *
  63. * @name compatibleMessage
  64. * @param {Error} thrown error
  65. * @param {String|RegExp} errMatcher to look for into the message
  66. * @namespace Utils
  67. * @api public
  68. */
  69. function compatibleMessage(thrown, errMatcher) {
  70. var comparisonString = typeof thrown === 'string' ? thrown : thrown.message;
  71. if (errMatcher instanceof RegExp) {
  72. return errMatcher.test(comparisonString);
  73. } else if (typeof errMatcher === 'string') {
  74. return comparisonString.indexOf(errMatcher) !== -1; // eslint-disable-line no-magic-numbers
  75. }
  76. return false;
  77. }
  78. /**
  79. * ### .getConstructorName(errorLike)
  80. *
  81. * Gets the constructor name for an Error instance or constructor itself.
  82. *
  83. * @name getConstructorName
  84. * @param {Error|ErrorConstructor} errorLike
  85. * @namespace Utils
  86. * @api public
  87. */
  88. function getConstructorName(errorLike) {
  89. var constructorName = errorLike;
  90. if (errorLike instanceof Error) {
  91. constructorName = getFunctionName(errorLike.constructor);
  92. } else if (typeof errorLike === 'function') {
  93. // If `err` is not an instance of Error it is an error constructor itself or another function.
  94. // If we've got a common function we get its name, otherwise we may need to create a new instance
  95. // of the error just in case it's a poorly-constructed error. Please see chaijs/chai/issues/45 to know more.
  96. constructorName = getFunctionName(errorLike);
  97. if (constructorName === '') {
  98. var newConstructorName = getFunctionName(new errorLike()); // eslint-disable-line new-cap
  99. constructorName = newConstructorName || constructorName;
  100. }
  101. }
  102. return constructorName;
  103. }
  104. /**
  105. * ### .getMessage(errorLike)
  106. *
  107. * Gets the error message from an error.
  108. * If `err` is a String itself, we return it.
  109. * If the error has no message, we return an empty string.
  110. *
  111. * @name getMessage
  112. * @param {Error|String} errorLike
  113. * @namespace Utils
  114. * @api public
  115. */
  116. function getMessage(errorLike) {
  117. var msg = '';
  118. if (errorLike && errorLike.message) {
  119. msg = errorLike.message;
  120. } else if (typeof errorLike === 'string') {
  121. msg = errorLike;
  122. }
  123. return msg;
  124. }
  125. module.exports = {
  126. compatibleInstance: compatibleInstance,
  127. compatibleConstructor: compatibleConstructor,
  128. compatibleMessage: compatibleMessage,
  129. getMessage: getMessage,
  130. getConstructorName: getConstructorName,
  131. };