Exception.js.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: Exception.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: Exception.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>// Licensed Materials - Property of IBM
  20. //
  21. // IBM Watson Analytics
  22. //
  23. // (C) Copyright IBM Corp. 2015
  24. //
  25. // US Government Users Restricted Rights - Use, duplication or
  26. // disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  27. module.exports = ( function( Error, decl )
  28. {
  29. "use strict";
  30. /**
  31. * Interprets a line of a stack trace and tries to extract information from it.
  32. * @param {string} _line The line to interpret
  33. * @memberof module:barejs.Exception~
  34. * @private
  35. */
  36. /*istanbul ignore next: NodeJS has the Error.captureStackTrace method natively so this function will not be hit*/
  37. function splitStackLine( _line )
  38. {
  39. /*
  40. Expected input looks like:
  41. myFunction@http://my.domain.com:8080/my/site/script.js:120:8 (Firefox, Safari)
  42. @http://my.domain.com:8080/my/site/script.js:120:8 (Firefox)
  43. http://my.domain.com:8080/my/site/script.js:120:8 (Safari)
  44. */
  45. if ( !_line )
  46. return null;
  47. var line = String( _line );
  48. var result = new Array( 4 );
  49. // Try to find a name (can be empty string)
  50. var match = line.match( /^([^@]*)@/ );
  51. result[0] = null;
  52. if ( match )
  53. {
  54. result[0] = match[1] || null;
  55. // Chop off the &lt;functionName>@ part
  56. line = line.substr( match[0].length );
  57. }
  58. // Try to find line and column number
  59. match = line.match( /(:[0-9]+)?:([0-9]+)$/ );
  60. if ( match )
  61. {
  62. if ( match[1] )
  63. {
  64. result[2] = parseInt( match[1].substr( 1 ), 10 );
  65. result[3] = parseInt( match[2], 10 ) - 1;
  66. }
  67. else
  68. {
  69. result[2] = parseInt( match[2], 10 );
  70. result[3] = null;
  71. }
  72. // Chop off the :LineNumber:ColumnNumber part
  73. line = line.substr( 0, line.length - match[0].length );
  74. }
  75. // The result should be the filename
  76. result[1] = line;
  77. return result;
  78. }
  79. /*istanbul ignore next: NodeJS has the Error.captureStackTrace method natively so this function will not be hit*/
  80. function processStack( _obj, _ctor, _err )
  81. {
  82. var fileName, lineNumber, columnNumber; // Additional metadata
  83. var line, match; // used to parse the stack entries
  84. var stack = _err.stack.split( "\n" ); // Split version of _err.stack
  85. // Initialize fileName etc. to null
  86. fileName = lineNumber = columnNumber = null;
  87. if ( _ctor.name )
  88. {
  89. for ( line = 0; line &lt; stack.length; ++line )
  90. {
  91. if ( stack[line].startsWith( _ctor.name + "@" ) )
  92. break;
  93. }
  94. // Never remove the last entry
  95. if ( line >= ( stack.length - 1 ) )
  96. line = -1;
  97. }
  98. else
  99. {
  100. // We know for a fact the lowest entry can be ignored, unless it's the only entry
  101. line = ( stack.length === 1 ) ? -1 : 0;
  102. }
  103. // If we get here and line >= 0, we can remove those entries
  104. if ( line >= 0 )
  105. {
  106. stack = stack.slice( line + 1 );
  107. match = splitStackLine( stack[0] );
  108. if ( match )
  109. {
  110. fileName = match[1];
  111. lineNumber = match[2];
  112. columnNumber = match[3];
  113. }
  114. }
  115. decl.defineProperty( _obj, "stack", { configurable: true, value: stack.join( "\n" ) } );
  116. if ( fileName )
  117. {
  118. decl.defineProperties( _obj,
  119. {
  120. fileName: { configurable: true, value: fileName },
  121. lineNumber: { configurable: true, value: lineNumber },
  122. columnNumber: { configurable: true, value: columnNumber }
  123. } );
  124. }
  125. }
  126. /*istanbul ignore next: NodeJS has the Error.captureStackTrace method natively so this function will not be hit*/
  127. function captureStackTrace( _obj, _ctor, _err )
  128. {
  129. if ( "stack" in _err )
  130. processStack( _obj, _ctor, _err );
  131. // if ( "opera#sourceLoc" in _err ) // Opera support?
  132. }
  133. /**
  134. * The Exception constructor will set up the exception with a name and stack property. You can pass the "creator function"
  135. * as second argument, this is the topmost function that will be ignored from the stack. It defaults to the constructed
  136. * object's constructor, which ensures the "Exception" and parent constructors never show up in the stack.
  137. * @class module:barejs.Exception
  138. * @param {string} _message The message that describes the exception.
  139. * @param {function} [_creatorFn] Optional: function to exclude from the call stack.
  140. * Defaults to the this.constructor function.
  141. *
  142. * @classdesc Exception creates a normalized base class for creating custom Error (Exception) classes to throw.
  143. * It handles determining the stack trace for the created exception in a cross browser way.
  144. * This class relies on the constructor property being set correctly, otherwise sub-class constructors
  145. * may show up in the stack trace. Using {@link module:barejs.decl#declareClass decl.declareClass}
  146. * to define base classes ensures the constructor property is set correctly.
  147. *
  148. * Sub classes should also set the name property on the prototype to the name of the exception, for example:
  149. *
  150. * function MyCustomError( _message, _myAdditionalData )
  151. * {
  152. * Exception.call( this, _message );
  153. *
  154. * this.myAdditionalData = _myAdditionalData;
  155. * }
  156. *
  157. * decl.declareClass( MyCustomError, Exception,
  158. * {
  159. * // Setting the name ensures our custom error looks and behaves as expected
  160. * // Avoid using MyCustomError.name (named function name) as the name may get
  161. * // mangled by a minifier/uglifier.
  162. * name: "MyCustomError",
  163. *
  164. * myAdditionalData: null
  165. * } );
  166. *
  167. */
  168. function Exception( _message/*, _creatorFn*/ )
  169. {
  170. if ( !this || !( this instanceof Exception ) )
  171. throw new TypeError( "Invalid context for Exception. Did you forget the new keyword?" );
  172. var fn = arguments[1] || this.constructor;
  173. if ( typeof fn !== "function" )
  174. throw new TypeError( "_creatorFn must be omitted, null or a function." );
  175. /*istanbul ignore else: NodeJS has the Error.captureStackTrace method natively*/
  176. if ( Error.captureStackTrace )
  177. Error.captureStackTrace( this, fn );
  178. else
  179. captureStackTrace( this, fn, new Error() );
  180. decl.defineProperty( this, "message", { configurable: true, writable: true, value: _message } );
  181. }
  182. return decl.declareClass( Exception, Error,
  183. /** @lends module:barejs.Exception# */
  184. {
  185. /**
  186. * The name of the Exception type. Base classes are supposed to set the correct name on the prototype too.
  187. * It is recommended not to use the constructor function's name for this, as that might get obfuscated by
  188. * a minifier, or the name property may not be supported at all.
  189. * @member {string}
  190. */
  191. name: { configurable: true, writable: true, value: "Exception" }
  192. } );
  193. // End of module
  194. }( Error, require( "./decl" ) ) );
  195. </code></pre>
  196. </article>
  197. </section>
  198. </div>
  199. <nav>
  200. <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-barejs.html">barejs</a></li><li><a href="module-barejs_polyfill.html">barejs/polyfill</a></li><li><a href="module-barejs_polyfill_Intl.html">barejs/polyfill/Intl</a></li></ul><h3>Classes</h3><ul><li><a href="module-barejs.decl.html">decl</a></li><li><a href="module-barejs.decl-Enum.html">Enum</a></li><li><a href="module-barejs.decl-Interface.html">Interface</a></li><li><a href="module-barejs.decl-SpecialType.html">SpecialType</a></li><li><a href="module-barejs.Destroyable.html">Destroyable</a></li><li><a href="module-barejs.EventArgs.html">EventArgs</a></li><li><a href="module-barejs.Evented.html">Evented</a></li><li><a href="module-barejs.Evented-EventedHandle.html">EventedHandle</a></li><li><a href="module-barejs.Exception.html">Exception</a></li><li><a href="module-barejs_polyfill.Array.html">Array</a></li><li><a href="module-barejs_polyfill.Date.html">Date</a></li><li><a href="module-barejs_polyfill.EntryStore.html">EntryStore</a></li><li><a href="module-barejs_polyfill.EntryStore.Iterator.html">Iterator</a></li><li><a href="module-barejs_polyfill.Function.html">Function</a></li><li><a href="module-barejs_polyfill.Map.html">Map</a></li><li><a href="module-barejs_polyfill.Map-MapIterator.html">MapIterator</a></li><li><a href="module-barejs_polyfill.Math.html">Math</a></li><li><a href="module-barejs_polyfill.Number.html">Number</a></li><li><a href="module-barejs_polyfill.Object.html">Object</a></li><li><a href="module-barejs_polyfill.Promise.html">Promise</a></li><li><a href="module-barejs_polyfill.Set.html">Set</a></li><li><a href="module-barejs_polyfill.Set-SetIterator.html">SetIterator</a></li><li><a href="module-barejs_polyfill.String.html">String</a></li><li><a href="module-barejs_polyfill.Symbol.html">Symbol</a></li><li><a href="module-barejs_polyfill.WeakMap.html">WeakMap</a></li><li><a href="module-barejs_polyfill.WeakSet.html">WeakSet</a></li><li><a href="module-barejs_polyfill_Intl.DateTimeFormat.html">DateTimeFormat</a></li><li><a href="module-barejs_polyfill_Intl.DateTimeFormat-DateTimeFormatOptions.html">DateTimeFormatOptions</a></li><li><a href="module-barejs_polyfill_Intl.NumberFormat.html">NumberFormat</a></li><li><a href="module-barejs_polyfill_Intl.NumberFormat-NumberFormatOptions.html">NumberFormatOptions</a></li><li><a href="module-barejs_polyfill_Intl-Format.html">Format</a></li></ul>
  201. </nav>
  202. <br class="clear">
  203. <footer>
  204. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Wed Oct 03 2018 15:59:33 GMT+0200 (W. Europe Daylight Time)
  205. </footer>
  206. <script> prettyPrint(); </script>
  207. <script src="scripts/linenumber.js"> </script>
  208. </body>
  209. </html>