API Docs for: 0.8.0
Show:

File: include/error/errors_over_time.js

  1. /*
  2. Copyright (C) 2016 PencilBlue, LLC
  3.  
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. 'use strict';
  18.  
  19. module.exports = function ErrorsOverTimeModule(/*pb*/) {
  20.  
  21. /**
  22. * Wraps up code that allows developers the ability to time box errors. In
  23. * some instances errors will occur. It is only when a certain number of those
  24. * errors occur within a given time span that it is recognized that recovery is
  25. * improbable. When the threshold is broken the code allows you to throw an
  26. * error that represents all others that contributed to the threshold breach.
  27. * @class ErrorsOverTime
  28. * @constructor
  29. * @param {Integer} errorSpan The upper bound on the number of errors that can
  30. * occur within the provided time frame before it is determined that recovery
  31. * is not possible.
  32. * @param {Integer} errorThreshold The upper bound on the amount of time, in
  33. * milliseconds, that errors can occur in before it is determined that recovery
  34. * is not possible.
  35. * @param {String} [prefix] The prefix to any error message that is generated
  36. * by the instance
  37. */
  38. function ErrorsOverTime(errorSpan, errorThreshold, prefix) {
  39.  
  40. /**
  41. * The upper bound on the number of errors that can occur within the provided
  42. * time frame before it is determined that recovery is not possible.
  43. * @property errorSpan
  44. * @type {Integer}
  45. */
  46. this.errorSpan = errorSpan;
  47.  
  48. /**
  49. * The upper bound on the amount of time, in milliseconds, that errors can
  50. * occur in before it is determined that recovery is not possible.
  51. * @property errorThreshold
  52. * @type {Integer}
  53. */
  54. this.errorThreshold = errorThreshold;
  55.  
  56. /**
  57. * The list of errors that will be used to determin if too many errors have
  58. * occurred within a given time frame.
  59. * @property errors
  60. * @type {Array}
  61. */
  62. this.errors = [];
  63.  
  64. /**
  65. * The total number of errors that have occurred
  66. * @property totalErrorCnt
  67. * @type {Integer}
  68. */
  69. this.totalErrorCnt = 0;
  70.  
  71. /**
  72. * The prefix to any error message that is generated by the instance
  73. * @property prefix
  74. * @type {String}
  75. */
  76. this.prefix = prefix;
  77. }
  78.  
  79. /**
  80. * Adds an error into the calculation to determine if too many errors have
  81. * occurred within a particular time span. If the threshold has been broken an
  82. * error is thrown.
  83. * @method throwIfOutOfBounds
  84. * @param {Error} The error that occurred
  85. * @param {String} prefix The error message text that will come first
  86. * @return {Boolean} TRUE if threshold is in tact, FALSE if not
  87. */
  88. ErrorsOverTime.prototype.throwIfOutOfBounds = function(err, prefix) {
  89. if (!this.errorOccurred(err)) {
  90. ErrorsOverTime.generateError(this.errors, prefix || this.prefix);
  91. }
  92. return true;
  93. };
  94.  
  95. /**
  96. * Adds an error into the calculation to determine if too many errors have
  97. * occurred within a particular time span.
  98. * @method errorOccurred
  99. * @param {Error} The error that occurred
  100. * @return {Boolean} TRUE if threshold is in tact, FALSE if not
  101. */
  102. ErrorsOverTime.prototype.errorOccurred = function(err) {
  103.  
  104. //add the error
  105. this.totalErrorCnt++;
  106. this.errors.push(err);
  107.  
  108. //shave off any errors that are outside of our span
  109. if (this.errors.length > this.errorSpan) {
  110. this.errors.splice(0, 1);
  111. }
  112. return this.isWithinLimits();
  113. };
  114.  
  115. /**
  116. * Determines if the errors that have occurred are within the acceptable tolerance.
  117. * @method isWithinLimits
  118. * @return {Boolean} TRUE if threshold in tact, FALSE if not
  119. */
  120. ErrorsOverTime.prototype.isWithinLimits = function() {
  121.  
  122. var withinLimits = true;
  123. if (this.errors.length >= this.errorSpan) {
  124. withinLimits = this.getRange() > this.errorThreshold;
  125. }
  126. return withinLimits;
  127. };
  128.  
  129. /**
  130. * Gets the time span over which the current set of errors has occurred
  131. * @method getRange
  132. * @return {Integer} The number of milliseconds over which the last "n" number
  133. * of errors have occurred where "n" is the value is between 0 and the value of
  134. * the errorSpan property inclusive.
  135. */
  136. ErrorsOverTime.prototype.getRange = function() {
  137. return this.errors[this.errors.length - 1] - this.errors[this.errors.length - this.errorSpan];
  138. };
  139.  
  140. /**
  141. * Generates and throws an Error that represents all of the errors that
  142. * triggered the threshold breach.
  143. * @static
  144. * @method generateError
  145. * @param {Array} errors The array of errors that will be represented by one
  146. * wrapper error
  147. * @param {String} prefix The error message text that will come first
  148. */
  149. ErrorsOverTime.generateError = function(errors, prefix) {
  150. throw ErrorsOverTime.createError(errors, prefix);
  151. };
  152.  
  153. /**
  154. * Creates an Error that represents all of the errors that triggered the
  155. * threshold breach.
  156. * @static
  157. * @method createError
  158. * @param {Array} errors The array of errors that will be represented by one
  159. * wrapper error
  160. * @param {String} prefix The error message text that will come first
  161. * @return {Error}
  162. */
  163. ErrorsOverTime.createError = function(errors, prefix) {
  164. if (!prefix) {
  165. prefix = '';
  166. }
  167.  
  168. var pattern = prefix + 'Threshold breached by:';
  169. errors.forEach(function(errItem) {
  170. pattern += '\n' + errItem.stack;
  171. });
  172. pattern += '\n----------------------------------------';
  173.  
  174. return new Error(pattern);
  175. };
  176.  
  177. return ErrorsOverTime;
  178. };
  179.