API Docs for: 0.0.3
Show:

File: lib/cda.js

  1. /*
  2. * Copyright 2013 Joel latino
  3. * Contact: jlatino@sapo.pt ~ http://about.me/latinojoel/ ~ http://latinojoel.github.io/cdajs ~ http://joel-latino.blogspot.com
  4. * Twitter: @latinojoel
  5. *
  6. * Include this in your web-pages for debug and development purposes only. For production purposes,
  7. * consider using the minified/obfuscated versions in the /min directory.
  8. *
  9. * This program is free software: you can redistribute it and/or modify it under the terms of the
  10. * GNU Lesser General Public License as published by the Free Software Foundation,
  11. * either version 3 of the License, or (at your option) any later version. This
  12. * program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
  13. * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  14. * Public License for more details. You should have received a copy of the GNU General Public
  15. * License along with this program.
  16. * If not, see <http://www.gnu.org/licenses/>.
  17. */
  18.  
  19. "use strict"; // js-hint is fuck'd up ^^
  20.  
  21. /**
  22. * This is <b>CDAjs</b> - a stand-alone javascript library for working with Pentaho plugin
  23. * "Community Data Access" CDA (<i>Community Data Access</i>) is a open source plugin for
  24. * <b>Pentaho Business Analysis</b> suite, designed to allow greater flexibility for data sources.
  25. * Powered by <a href="http://www.webdetails.pt/ctools/cda.html" target="_blank">WebDetails</a>.
  26. * <br/><br/> <b>CDAjs</b> is cross-browser and <a href="http://nodejs.org"target="_blank">Node.js</a>
  27. * compatible and enables web-browser-based analytical Pentaho Business Intelligence applications.
  28. *
  29. * @module CDAjs
  30. * @title CDAjs
  31. */
  32. (function (window) {
  33.  
  34.  
  35. /**
  36. * Get's a XML Http Request
  37. *
  38. * @return {Object} a XML Http Request object.
  39. */
  40.  
  41. function _Xhr() {
  42. if (window.XMLHttpRequest) {
  43. return new window.XMLHttpRequest();
  44. } else if (window.ActiveXObject) {
  45. return new window.ActiveXObject("MSXML2.XMLHTTP.3.0");
  46. } else if (typeof (require) === "function") {
  47. var xhr;
  48. (xhr = function () {
  49. this.readyState = 0;
  50. this.status = -1;
  51. this.statusText = "Not sent";
  52. this.responseText = null;
  53. }).prototype = {
  54. changeStatus: function (statusCode, readyState) {
  55. this.status = statusCode;
  56. this.statusText = statusCode;
  57. this.readyState = readyState;
  58. if (_isFun(this.onreadystatechange)) {
  59. this.onreadystatechange.call(this, this);
  60. }
  61. },
  62. open: function (method, url, async, username, password) {
  63. if (async !== true) {
  64. throw "Synchronous mode not supported in this environment.";
  65. }
  66. var options = require("url").parse(url);
  67. if (options.host.length > options.hostname.length) {
  68. // for some reason, host includes the port, this confuses http.request
  69. // so, overwrite host with hostname (which does not include the port)
  70. // and kill hostname so that we end up with only host.
  71. options.host = options.hostname;
  72. delete options.hostname;
  73. }
  74. if (!options.path && options.pathname) {
  75. // old versions of node may not give the path, so we need to create it ourselves.
  76. options.path = options.pathname + (options.search || "");
  77. }
  78. options.method = CDA.DEFAULT_OPTIONS.METHOD; // method;
  79. options.headers = {};
  80. if (username) options.headers.Authorization = "Basic " + (new Buffer(username + ":" + (password || ""))).toString("base64");
  81. this.options = options;
  82. this.changeStatus(-1, 1);
  83. },
  84. send: function (data) {
  85. var me = this,
  86. options = me.options,
  87. client;
  88. if (data) {
  89. options.headers["Content-Length"] = Buffer.byteLength(data);
  90. }
  91. switch (options.protocol) {
  92. case "http:":
  93. client = require("http");
  94. if (!options.port) options.port = "80";
  95. break;
  96. case "https:":
  97. client = require("https");
  98. if (!options.port) options.port = "443";
  99. break;
  100. default:
  101. throw "Unsupported protocol " + options.protocol;
  102. }
  103. me.responseText = "";
  104. var request = client.request(options, function (response) {
  105. response.setEncoding("utf8");
  106. me.changeStatus(-1, 2);
  107. response.on("data", function (chunk) {
  108. me.responseText += chunk;
  109. me.changeStatus(response.statusCode, 3);
  110. });
  111. response.on("error", function (error) {
  112. me.changeStatus(response.statusCode, 4);
  113. });
  114. response.on("end", function () {
  115. me.changeStatus(response.statusCode, 4);
  116. });
  117. });
  118. request.on("error", function (e) {
  119. me.responseText = e;
  120. me.changeStatus(500, 4);
  121. });
  122. if (data) request.write(data);
  123. request.end();
  124. },
  125. setRequestHeader: function (name, value) {
  126. this.options.headers[name] = value;
  127. }
  128. };
  129. return new xhr();
  130. } else {
  131. CDA.Exception.newError("ERROR_INSTANTIATING_XMLHTTPREQUEST", "ajax", null)._throw();
  132. }
  133. }
  134.  
  135.  
  136. /**
  137. * Convert parameters for HTTP request.
  138. *
  139. * @param params a parameters to convert.
  140. * @return {string} a string with parameters converted for HTTP request.
  141. */
  142.  
  143. function _convertParams(params) {
  144. var form = "?";
  145. var isFirst = true;
  146. for (var key in params) {
  147. isFirst ? isFirst = false : form += "&";
  148. var value = typeof params[key] == 'function' ? params[key]() : params[key];
  149. form += key + "=" + value;
  150. }
  151. return form;
  152. }
  153.  
  154.  
  155. /**
  156. * Checks argument is undefined.
  157. *
  158. * @param arg an argument.
  159. * @return {boolean} <code>true</code> if argument is undefined, <code>false</code> if
  160. * argument isn't undefined.
  161. */
  162.  
  163. function _isUnd(arg) {
  164. return typeof (arg) === "undefined";
  165. }
  166.  
  167.  
  168. /**
  169. * Checks argument is function.
  170. *
  171. * @param arg an argument.
  172. * @return {boolean} <code>true</code> if argument is function, <code>false</code> if argument
  173. * isn't function.
  174. */
  175.  
  176. function _isFun(arg) {
  177. return typeof (arg) === "function";
  178. }
  179.  
  180.  
  181. /**
  182. * Function responsable for invoke an endpoint.
  183. *
  184. * @param callback a callback function executed when HTTP request to endpoint gives success.
  185. * @param options a options for invoke endpoint (can be HTTP parameters, ...).
  186. * @param endpoint an endpoint name.
  187. * @param self a self instance of CDAjs.
  188. */
  189.  
  190. function _invokeEndpoint(callback, options, endpoint, self) {
  191. var xhr = _Xhr();
  192. options.method = options.method || CDA.DEFAULT_OPTIONS.METHOD;
  193.  
  194. // Parameters for Pentaho Authentication
  195. if((options.username || self.options.username) !== undefined && (options.password || self.options.password) !== undefined){
  196. options.params["userid"] = options.username || self.options.username;
  197. options.params["password"] = options.password || self.options.password;
  198. }
  199.  
  200. if(Object.getOwnPropertyNames(options.params).length>0){
  201. options.form = _convertParams(options.params);
  202. }
  203.  
  204. var finalUrl = (options.url || self.options.url) + endpoint + (options.method.toUpperCase() === "GET" ? (options.form || "") : "");
  205. xhr.open(options.method, finalUrl, options.async || CDA.DEFAULT_OPTIONS.ASYNC, options.username, options.password);
  206. xhr.timeout = options.timeout || CDA.DEFAULT_OPTIONS.REQUEST_TIMEOUT;
  207. xhr.setRequestHeader("Accept", options["Accept"] || CDA.DEFAULT_OPTIONS.ACCEPT);
  208. xhr.setRequestHeader("Content-Type", options["Content-Type"] || CDA.DEFAULT_OPTIONS.CONTENT_TYPE);
  209.  
  210. if (options.method.toUpperCase() === "GET") {
  211. xhr.send();
  212. } else {
  213. xhr.send(options.form || "");
  214. }
  215.  
  216. xhr.onreadystatechange = function () {
  217. switch (xhr.readyState) {
  218. case 0:
  219. if (_isFun(options.aborted)) {
  220. options.aborted(xhr);
  221. }
  222. break;
  223. case 4:
  224. if (xhr.status === 200) {
  225. var response = "";
  226. try {
  227. response = JSON.parse(xhr.responseText);
  228. } catch (e) {
  229. response = xhr.responseText;
  230. console.log("Can not convert to JSON after invoke " + endpoint + " endpoint.");
  231. }
  232. callback(xhr.responseText == "" ? "" : response);
  233. } else {
  234. var err = CDA.Exception.newError("HTTP_ERROR", "xhr", {
  235. request: options,
  236. status: self.status,
  237. statusText: self.statusText
  238. });
  239. console.log("Error: " + err);
  240. // When I have an error in HTTP, this allows better debugging
  241. // So made an extra call instead of _newError inside func call
  242. if (!_isUnd(options.error) && _isFun(options.error)) {
  243. options.error(err);
  244. }
  245. }
  246. break;
  247. }
  248. };
  249. }
  250.  
  251.  
  252. if (!window.JSON) {
  253. window.JSON = {
  254. parse: function (sJSON) {
  255. return eval("(" + sJSON + ")");
  256. },
  257. stringify: function (vContent) {
  258. if (vContent instanceof Object) {
  259. var sOutput = "";
  260. if (vContent.constructor === Array) {
  261. for (var nId = 0; nId < vContent.length; sOutput += this.stringify(vContent[nId]) + ",", nId++);
  262. return "[" + sOutput.substr(0, sOutput.length - 1) + "]";
  263. }
  264. if (vContent.toString !== Object.prototype.toString) {
  265. return "\"" + vContent.toString().replace(/"/g, "\\$&") + "\"";
  266. }
  267. for (var sProp in vContent) {
  268. sOutput += "\"" + sProp.replace(/"/g, "\\$&") + "\":" + this.stringify(vContent[sProp]) + ",";
  269. }
  270. return "{" + sOutput.substr(0, sOutput.length - 1) + "}";
  271. }
  272. return typeof vContent === "string" ? "\"" + vContent.replace(/"/g, "\\$&") + "\"" : String(vContent);
  273. }
  274. };
  275. }
  276.  
  277.  
  278. /**
  279. * The CDA class provides a javascript API to communicate Pentaho Community Data Access plugin
  280. * (CDA) over HTTP.
  281. *
  282. * @class CDA
  283. * @constructor
  284. * @param options {Object} standard options
  285. */
  286.  
  287. function CDA(options) {
  288. this.options = options || CDA.Exception.newError("ERROR_CDA_OPTIONS", "CDA.constructor", null)._throw();
  289. this.options.url = options.url || CDA.Exception.newError("ERROR_CDA_URL", "CDA.constructor", null)._throw();
  290. if(typeof exports === "undefined"){
  291. this.options.username = options.username;
  292. this.options.password = options.password;
  293. }else{
  294. this.options.username = options.username || CDA.Exception.newError("ERROR_CDA_USERNAME", "CDA.constructor", null)._throw();
  295. this.options.password = options.password || CDA.Exception.newError("ERROR_CDA_PASSWORD", "CDA.constructor", null)._throw();
  296. }
  297. this.options.params = options.params || {};
  298. }
  299.  
  300.  
  301. /**
  302. * These are the default options used for new CDA instances in case no custom properties are set.
  303. * It sets the following properties:
  304. * <ul>
  305. * <li><code>REQUEST_TIMEOUT</code> int: 60000 - number of milliseconds before a request to the
  306. * Pentaho server will timeout </li>
  307. * <li><code>ASYNC</code> boolean: true - determines whether synchronous or asynchronous
  308. * communication with the Pentaho server will be used.</li>
  309. * <li><code>ACCEPT</code> string: text/xml, application/json - determines the accept header on
  310. * http request with the Pentaho server will be used.</li>
  311. * <li><code>CONTENT_TYPE</code> string: application/json - determines the content-type header
  312. * on http request with the Pentaho server will be used.</li>
  313. * <li><code>METHOD</code> string: GET - determines the method on http request with the Pentaho
  314. * server will be used.</li>
  315. * </ul>
  316. *
  317. * @property DEFAULT_OPTIONS
  318. * @static
  319. * @type object
  320. */
  321. CDA.DEFAULT_OPTIONS = {
  322. REQUEST_TIMEOUT: 60000,
  323. ASYNC: true,
  324. ACCEPT: "text/xml, application/json",
  325. CONTENT_TYPE: "application/json",
  326. METHOD: "GET"
  327. };
  328.  
  329. /**
  330. * Invokes the <code>/cda/doQuery</code> endpoint of CDA.
  331. *
  332. * @method doQuery
  333. * @param {function()} callback Function to be called for cda query done.
  334. * @param {Object} options Options to use on http request.
  335. */
  336. CDA.prototype.doQuery = function (callback, options) {
  337. var endpoint = "doQuery";
  338. if (_isUnd(options)) {
  339. options = this.options;
  340. }
  341. // Is mandatory use dataAccessId
  342. if (_isUnd(options.params) || _isUnd(options.params.dataAccessId)) {
  343. CDA.Exception.newError("ERROR_MISSING_DATA_ACCESS_ID", endpoint, null)._throw();
  344. }
  345.  
  346. // invokes endpoint
  347. _invokeEndpoint(callback, options, endpoint, this);
  348. };
  349.  
  350.  
  351. /**
  352. * Invokes the <code>/cda/listQueries</code> endpoint of CDA.
  353. *
  354. * @method listQueries
  355. * @param {function()} callback Function to be called for cda query done.
  356. * @param {Object} options Options to use on http request.
  357. */
  358. CDA.prototype.listQueries = function (callback, options) {
  359. var endpoint = "listQueries";
  360. if (_isUnd(options)) {
  361. options = this.options;
  362. }
  363. // Is mandatory use params
  364. if (_isUnd(options.params)) {
  365. CDA.Exception.newError("ERROR_MISSING_PARAMS", endpoint, null)._throw();
  366. }
  367. // Is mandatory use param path
  368. if (_isUnd(options.params.path)) {
  369. CDA.Exception.newError("ERROR_MISSING_PARAMS_PATH", endpoint, null)._throw();
  370. }
  371.  
  372. // invokes endpoint
  373. _invokeEndpoint(callback, options, endpoint, this);
  374. };
  375.  
  376.  
  377. /**
  378. * Invokes the <code>/cda/getCdaList</code> endpoint of CDA.
  379. *
  380. * @method getCdaList
  381. * @param {function()} callback Function to be called for cda query done.
  382. * @param {Object} options Options to use on http request.
  383. */
  384. CDA.prototype.getCdaList = function (callback, options) {
  385. var endpoint = "getCdaList";
  386. if (_isUnd(options)) {
  387. options = this.options;
  388. }
  389.  
  390. // invokes endpoint
  391. _invokeEndpoint(callback, options, endpoint, this);
  392. };
  393.  
  394.  
  395. /**
  396. * Invokes the <code>/cda/listDataAccessTypes</code> endpoint of CDA.
  397. *
  398. * @method listDataAccessTypes
  399. * @param {function()} callback Function to be called for cda query done.
  400. * @param {Object} options Options to use on http request.
  401. */
  402. CDA.prototype.listDataAccessTypes = function (callback, options) {
  403. var endpoint = "listDataAccessTypes";
  404. if (_isUnd(options)) {
  405. options = this.options;
  406. }
  407.  
  408. // invokes endpoint
  409. _invokeEndpoint(callback, options, endpoint, this);
  410. };
  411.  
  412.  
  413. /**
  414. * Invokes the <code>/cda/listParameters</code> endpoint of CDA.
  415. *
  416. * @method listParameters
  417. * @param {function()} callback Function to be called for cda query done.
  418. * @param {Object} options Options to use on http request.
  419. */
  420. CDA.prototype.listParameters = function (callback, options) {
  421. var endpoint = "listParameters";
  422. if (_isUnd(options)) {
  423. options = this.options;
  424. }
  425. // Is mandatory use params
  426. if (_isUnd(options.params)) {
  427. CDA.Exception.newError("ERROR_MISSING_PARAMS", endpoint, null)._throw();
  428. }
  429. // Is mandatory use param path
  430. if (_isUnd(options.params.path)) {
  431. CDA.Exception.newError("ERROR_MISSING_PARAMS_PATH", endpoint, null)._throw();
  432. }
  433. // Is mandatory use param dataAccessId
  434. if (_isUnd(options.params.dataAccessId)) {
  435. CDA.Exception.newError("ERROR_MISSING_PARAMS_DATA_ACCESS_ID", endpoint, null)._throw();
  436. }
  437.  
  438. // invokes endpoint
  439. _invokeEndpoint(callback, options, endpoint, this);
  440. };
  441.  
  442.  
  443. /**
  444. * Invokes the <code>/cda/clearCache</code> endpoint of CDA.
  445. *
  446. * @method clearCache
  447. * @param {function()} callback Function to be called for cda query done.
  448. * @param {Object} options Options to use on http request.
  449. */
  450. CDA.prototype.clearCache = function (callback, options) {
  451. var endpoint = "clearCache";
  452. if (_isUnd(options)) {
  453. options = this.options;
  454. }
  455.  
  456. // invokes endpoint
  457. _invokeEndpoint(callback, options, endpoint, this);
  458. };
  459.  
  460.  
  461. /**
  462. * Invokes the <code>/cda/getCdaFile</code> endpoint of CDA.
  463. *
  464. * @method getCdaFile
  465. * @param {function()} callback Function to be called for cda query done.
  466. * @param {Object} options Options to use on http request.
  467. */
  468. CDA.prototype.getCdaFile = function (callback, options) {
  469. var endpoint = "getCdaFile";
  470. if (_isUnd(options)) {
  471. options = this.options;
  472. }
  473.  
  474. // invokes endpoint
  475. _invokeEndpoint(callback, options, endpoint, this);
  476. };
  477.  
  478.  
  479. /**
  480. * <p>
  481. * This class is used to indicate an runtime errors occurring in any of the methods of the CDAjs
  482. * classes.
  483. * </p>
  484. * <p>
  485. * You do not need to instantiate objects of this class yourself. Rather, instances of this class
  486. * are created and thrown at runtime whenever an error occurs.
  487. * </p>
  488. * <p>
  489. * To handle CDAjs errors, you can use a <code>try...catch</code> block like this:
  490. * </p>
  491. *
  492. * <pre>
  493. * try {
  494. * ...general cdajs work...
  495. * } catch (exception) {
  496. * if (exception instanceof CDA.Exception) {
  497. * ...use exception.code, exception.message and exception.data to handle the exception.
  498. * } else {
  499. * ...handle other errors...
  500. * }
  501. * }
  502. * </pre>
  503. *
  504. * @class CDA.Exception
  505. * @constructor
  506. */
  507. CDA.Exception = function (type, code, message, helpfile, source, data, args) {
  508. this.type = type;
  509. this.code = code;
  510. this.message = message;
  511. this.source = source;
  512. this.helpfile = helpfile;
  513. this.data = data;
  514. this.args = args;
  515. return this;
  516. };
  517.  
  518.  
  519. /**
  520. * Create an exception.
  521. *
  522. * @param codeName an exception code name.
  523. * @param source an exception source
  524. * @param data an exception data to specify
  525. * @return {Object|CDA.Exception}
  526. */
  527. CDA.Exception.newError = function (codeName, source, data) {
  528. return new this.newDetailError(CDA.Exception[codeName + "_CODE"], CDA.Exception[codeName + "_MSG"], CDA.Exception[codeName + "_HELP"], source, data);
  529. };
  530.  
  531.  
  532. /**
  533. * Create an exception.
  534. *
  535. * @param code an exception code name.
  536. * @param message an exception message.
  537. * @param help an exception information help.
  538. * @param source an exception source
  539. * @param data an exception data to specify
  540. * @returns {Object|CDA.Exception}
  541. */
  542. CDA.Exception.newDetailError = function (code, message, help, source, data) {
  543. return new CDA.Exception(CDA.Exception.TYPE_ERROR, code, message, help, source, data);
  544. };
  545.  
  546.  
  547. var CDAWikiExceptionCodes = "https://github.com/latinojoel/cdajs/wiki/Exception-Codes";
  548.  
  549.  
  550. /**
  551. * Can appear as value for the type property of instances of the CDA.Exception class, and
  552. * indicates that this <code>CDA.Exception</code> signals an error.
  553. *
  554. * @property TYPE_ERROR
  555. * @static
  556. * @final
  557. * @type string
  558. * @default error
  559. */
  560. CDA.Exception.TYPE_ERROR = "error";
  561.  
  562.  
  563. /**
  564. * Can appear as value for the type property of instances of the CDA.Exception class, and
  565. * indicates that this <code>CDA.Exception</code> signals a warning.
  566. *
  567. * @property TYPE_WARNING
  568. * @static
  569. * @final
  570. * @type string
  571. * @default warning
  572. */
  573. CDA.Exception.TYPE_WARNING = "warning";
  574.  
  575.  
  576. /**
  577. * Exception code indicating that missing the CDA URL.
  578. *
  579. * @property ERROR_CDA_URL_CODE
  580. * @static
  581. * @final
  582. * @type {int}
  583. * @default -1
  584. */
  585. CDA.Exception.ERROR_CDA_URL_CODE = -1;
  586. CDA.Exception.ERROR_CDA_URL_MSG = "Missing CDA URL";
  587. CDA.Exception.ERROR_CDA_URL_HELP = "Please provide CDA url on the CDA object constructor (Please consult this page " + CDAWikiExceptionCodes + ")";
  588.  
  589.  
  590. /**
  591. * Exception code indicating that missing options in the constructor.
  592. *
  593. * @property ERROR_CDA_OPTIONS_CODE
  594. * @static
  595. * @final
  596. * @type {int}
  597. * @default -2
  598. */
  599. CDA.Exception.ERROR_CDA_OPTIONS_CODE = -2;
  600. CDA.Exception.ERROR_CDA_OPTIONS_MSG = "Missing CDA Options";
  601. CDA.Exception.ERROR_CDA_OPTIONS_HELP = "Please provide CDA options on the CDA object constructor (Please consult this page " + CDAWikiExceptionCodes + ")";
  602.  
  603.  
  604. /**
  605. * Exception code indicating a general XMLHttpRequest error. If this error occurs, the data object
  606. * of the exception will have these members:
  607. * <ul>
  608. * <li>request: the options that make up the original HTTP request</li>
  609. * <li>status: the HTTP status code</li>
  610. * <li>statusText: the HTTP status text</li>
  611. * </ul>
  612. *
  613. * @property HTTP_ERROR_CODE
  614. * @static
  615. * @final
  616. * @type {int}
  617. * @default -3
  618. */
  619. CDA.Exception.HTTP_ERROR_CODE = -3;
  620. CDA.Exception.HTTP_ERROR_MSG = "HTTP ERROR";
  621. CDA.Exception.HTTP_ERROR_HELP = "Generic HTTP error (Please consult this page " + CDAWikiExceptionCodes + ")";
  622.  
  623.  
  624. /**
  625. * Exception code indicating that missing username for Pentaho authentication.
  626. *
  627. * @property ERROR_CDA_USERNAME_CODE
  628. * @static
  629. * @final
  630. * @type {int}
  631. * @default -4
  632. */
  633. CDA.Exception.ERROR_CDA_USERNAME_CODE = -4;
  634. CDA.Exception.ERROR_CDA_USERNAME_MSG = "Missing CDA Username";
  635. CDA.Exception.ERROR_CDA_USERNAME_HELP = "Please provide CDA username on the CDA object constructor (Please consult this page " + CDAWikiExceptionCodes + ")";
  636.  
  637.  
  638. /**
  639. * Exception code indicating that missing password for Pentaho authentication.
  640. *
  641. * @property ERROR_CDA_PASSWORD_CODE
  642. * @static
  643. * @final
  644. * @type {int}
  645. * @default -5
  646. */
  647. CDA.Exception.ERROR_CDA_PASSWORD_CODE = -5;
  648. CDA.Exception.ERROR_CDA_PASSWORD_MSG = "Missing CDA Password";
  649. CDA.Exception.ERROR_CDA_PASSWORD_HELP = "Please provide CDA password on the CDA object constructor (Please consult this page " + CDAWikiExceptionCodes + ")";
  650.  
  651.  
  652. /**
  653. * Exception code indicating that missing data access id for CDA query.
  654. *
  655. * @property ERROR_MISSING_DATA_ACCESS_ID_CODE
  656. * @static
  657. * @final
  658. * @type {int}
  659. * @default -6
  660. */
  661. CDA.Exception.ERROR_MISSING_DATA_ACCESS_ID_CODE = -6;
  662. CDA.Exception.ERROR_MISSING_DATA_ACCESS_ID_MSG = "Missing CDA data access id";
  663. CDA.Exception.ERROR_MISSING_DATA_ACCESS_ID_HELP = "Please provide CDA data access id (Please consult this page " + CDAWikiExceptionCodes + ")";
  664.  
  665.  
  666. /**
  667. * Exception code indicating that missing params object for invoke CDA endpoint.
  668. *
  669. * @property ERROR_MISSING_PARAMS_CODE
  670. * @static
  671. * @final
  672. * @type {int}
  673. * @default -7
  674. */
  675. CDA.Exception.ERROR_MISSING_PARAMS_CODE = -7;
  676. CDA.Exception.ERROR_MISSING_PARAMS_MSG = "Missing object params";
  677. CDA.Exception.ERROR_MISSING_PARAMS_HELP = "Please provide params (Please consult this page " + CDAWikiExceptionCodes + ")";
  678.  
  679.  
  680. /**
  681. * Exception code indicating that missing path property on object params for invoke CDA endpoint.
  682. *
  683. * @property ERROR_MISSING_PARAMS_PATH_CODE
  684. * @static
  685. * @final
  686. * @type {int}
  687. * @default -9
  688. */
  689. CDA.Exception.ERROR_MISSING_PARAMS_PATH_CODE = -9;
  690. CDA.Exception.ERROR_MISSING_PARAMS_PATH_MSG = "Missing path property on object params";
  691. CDA.Exception.ERROR_MISSING_PARAMS_PATH_HELP = "Please provide path property on params (Please consult this page " + CDAWikiExceptionCodes + ")";
  692.  
  693.  
  694. CDA.Exception.prototype = {
  695.  
  696. /**
  697. * This propery indicates what kind of exception occurred. It can have one of the following
  698. * values:
  699. * <dl>
  700. * <dt><code>TYPE_WARNING</code></dt>
  701. * <dd>Indicates a warning</dd>
  702. * <dt><code>TYPE_ERROR</code></dt>
  703. * <dd>Indicates an error</dd>
  704. * </dl>
  705. *
  706. * @property type
  707. * @type {string}
  708. * @default null
  709. */
  710. type: null,
  711.  
  712.  
  713. /**
  714. * A code that can be used to identify this particular kind of exception.
  715. *
  716. * @property code
  717. * @type {int}
  718. * @default null
  719. */
  720. code: null,
  721. /**
  722. * A human readable message that describes the nature of the error or warning.
  723. *
  724. * @property message
  725. * @type {string}
  726. * @default null
  727. */
  728. message: null,
  729.  
  730.  
  731. /**
  732. * A name that indicates in what component (on the client or server side) this error or warning
  733. * occurred.
  734. *
  735. * @property source
  736. * @type {string}
  737. * @default null
  738. */
  739. source: null,
  740.  
  741.  
  742. /**
  743. * A path or url that points to a document that contains more information about this error.
  744. *
  745. * @property helpfile
  746. * @type {string}
  747. * @default null
  748. */
  749. helpfile: null,
  750.  
  751.  
  752. /**
  753. * Additional data captured when the exception was instantiated. The type of information stored
  754. * here is dependent upon the nature of the error.
  755. *
  756. * @property data
  757. * @type {string}
  758. * @default null
  759. */
  760. data: null,
  761. _throw: function () {
  762. throw this;
  763. },
  764.  
  765.  
  766. /**
  767. * A reference to the built-in <code>arguments</code> array of the function that is throwing
  768. * the exception This can be used to get a "stack trace"
  769. *
  770. * @property args
  771. * @type {array}
  772. */
  773. args: null,
  774.  
  775.  
  776. /**
  777. * Returns a string representing this exception
  778. *
  779. * @method toString
  780. * @return a string representing this exception
  781. */
  782. toString: function () {
  783. return this.type + " " + this.code + ": " + this.message + " (source: " + this.source + ")";
  784. },
  785.  
  786.  
  787. /**
  788. * Get a stack trace.
  789. *
  790. * @method getStackTrace
  791. * @return an array of objects describing the function on the stack
  792. */
  793. getStackTrace: function () {
  794. var stack = "";
  795. if (this.args) {
  796. var func = this.args.callee;
  797. while (func) {
  798. funcstring = String(func);
  799. func = func.caller;
  800. }
  801. }
  802. return stack;
  803. }
  804. };
  805.  
  806.  
  807. /*
  808. * Register CDAjs. In an amd (https://github.com/amdjs/amdjs-api/wiki/AMD) environment, use the
  809. * define function Otherwise, add it to the global window variable. For server side environemnts
  810. * that do not have a proper window object, simply create a global variable called window and
  811. * assign an object to it that you want to function as the CDA container.
  812. */
  813. if (typeof (define) === "function" && define.amd) {
  814. define(function () {
  815. return CDA;
  816. });
  817. } else window.CDA = CDA;
  818.  
  819. return CDA;
  820.  
  821. })(typeof exports === "undefined" ? window : exports);