Check-70bec281.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /**
  2. * Cesium - https://github.com/AnalyticalGraphicsInc/cesium
  3. *
  4. * Copyright 2011-2017 Cesium Contributors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Columbus View (Pat. Pend.)
  19. *
  20. * Portions licensed separately.
  21. * See https://github.com/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md for full licensing details.
  22. */
  23. define(['exports', './when-8d13db60'], function (exports, when) { 'use strict';
  24. /**
  25. * Constructs an exception object that is thrown due to a developer error, e.g., invalid argument,
  26. * argument out of range, etc. This exception should only be thrown during development;
  27. * it usually indicates a bug in the calling code. This exception should never be
  28. * caught; instead the calling code should strive not to generate it.
  29. * <br /><br />
  30. * On the other hand, a {@link RuntimeError} indicates an exception that may
  31. * be thrown at runtime, e.g., out of memory, that the calling code should be prepared
  32. * to catch.
  33. *
  34. * @alias DeveloperError
  35. * @constructor
  36. * @extends Error
  37. *
  38. * @param {String} [message] The error message for this exception.
  39. *
  40. * @see RuntimeError
  41. */
  42. function DeveloperError(message) {
  43. /**
  44. * 'DeveloperError' indicating that this exception was thrown due to a developer error.
  45. * @type {String}
  46. * @readonly
  47. */
  48. this.name = 'DeveloperError';
  49. /**
  50. * The explanation for why this exception was thrown.
  51. * @type {String}
  52. * @readonly
  53. */
  54. this.message = message;
  55. //Browsers such as IE don't have a stack property until you actually throw the error.
  56. var stack;
  57. try {
  58. throw new Error();
  59. } catch (e) {
  60. stack = e.stack;
  61. }
  62. /**
  63. * The stack trace of this exception, if available.
  64. * @type {String}
  65. * @readonly
  66. */
  67. this.stack = stack;
  68. }
  69. if (when.defined(Object.create)) {
  70. DeveloperError.prototype = Object.create(Error.prototype);
  71. DeveloperError.prototype.constructor = DeveloperError;
  72. }
  73. DeveloperError.prototype.toString = function() {
  74. var str = this.name + ': ' + this.message;
  75. if (when.defined(this.stack)) {
  76. str += '\n' + this.stack.toString();
  77. }
  78. return str;
  79. };
  80. /**
  81. * @private
  82. */
  83. DeveloperError.throwInstantiationError = function() {
  84. throw new DeveloperError('This function defines an interface and should not be called directly.');
  85. };
  86. /**
  87. * Contains functions for checking that supplied arguments are of a specified type
  88. * or meet specified conditions
  89. * @private
  90. */
  91. var Check = {};
  92. /**
  93. * Contains type checking functions, all using the typeof operator
  94. */
  95. Check.typeOf = {};
  96. function getUndefinedErrorMessage(name) {
  97. return name + ' is required, actual value was undefined';
  98. }
  99. function getFailedTypeErrorMessage(actual, expected, name) {
  100. return 'Expected ' + name + ' to be typeof ' + expected + ', actual typeof was ' + actual;
  101. }
  102. /**
  103. * Throws if test is not defined
  104. *
  105. * @param {String} name The name of the variable being tested
  106. * @param {*} test The value that is to be checked
  107. * @exception {DeveloperError} test must be defined
  108. */
  109. Check.defined = function (name, test) {
  110. if (!when.defined(test)) {
  111. throw new DeveloperError(getUndefinedErrorMessage(name));
  112. }
  113. };
  114. /**
  115. * Throws if test is not typeof 'function'
  116. *
  117. * @param {String} name The name of the variable being tested
  118. * @param {*} test The value to test
  119. * @exception {DeveloperError} test must be typeof 'function'
  120. */
  121. Check.typeOf.func = function (name, test) {
  122. if (typeof test !== 'function') {
  123. throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'function', name));
  124. }
  125. };
  126. /**
  127. * Throws if test is not typeof 'string'
  128. *
  129. * @param {String} name The name of the variable being tested
  130. * @param {*} test The value to test
  131. * @exception {DeveloperError} test must be typeof 'string'
  132. */
  133. Check.typeOf.string = function (name, test) {
  134. if (typeof test !== 'string') {
  135. throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'string', name));
  136. }
  137. };
  138. /**
  139. * Throws if test is not typeof 'number'
  140. *
  141. * @param {String} name The name of the variable being tested
  142. * @param {*} test The value to test
  143. * @exception {DeveloperError} test must be typeof 'number'
  144. */
  145. Check.typeOf.number = function (name, test) {
  146. if (typeof test !== 'number') {
  147. throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'number', name));
  148. }
  149. };
  150. /**
  151. * Throws if test is not typeof 'number' and less than limit
  152. *
  153. * @param {String} name The name of the variable being tested
  154. * @param {*} test The value to test
  155. * @param {Number} limit The limit value to compare against
  156. * @exception {DeveloperError} test must be typeof 'number' and less than limit
  157. */
  158. Check.typeOf.number.lessThan = function (name, test, limit) {
  159. Check.typeOf.number(name, test);
  160. if (test >= limit) {
  161. throw new DeveloperError('Expected ' + name + ' to be less than ' + limit + ', actual value was ' + test);
  162. }
  163. };
  164. /**
  165. * Throws if test is not typeof 'number' and less than or equal to limit
  166. *
  167. * @param {String} name The name of the variable being tested
  168. * @param {*} test The value to test
  169. * @param {Number} limit The limit value to compare against
  170. * @exception {DeveloperError} test must be typeof 'number' and less than or equal to limit
  171. */
  172. Check.typeOf.number.lessThanOrEquals = function (name, test, limit) {
  173. Check.typeOf.number(name, test);
  174. if (test > limit) {
  175. throw new DeveloperError('Expected ' + name + ' to be less than or equal to ' + limit + ', actual value was ' + test);
  176. }
  177. };
  178. /**
  179. * Throws if test is not typeof 'number' and greater than limit
  180. *
  181. * @param {String} name The name of the variable being tested
  182. * @param {*} test The value to test
  183. * @param {Number} limit The limit value to compare against
  184. * @exception {DeveloperError} test must be typeof 'number' and greater than limit
  185. */
  186. Check.typeOf.number.greaterThan = function (name, test, limit) {
  187. Check.typeOf.number(name, test);
  188. if (test <= limit) {
  189. throw new DeveloperError('Expected ' + name + ' to be greater than ' + limit + ', actual value was ' + test);
  190. }
  191. };
  192. /**
  193. * Throws if test is not typeof 'number' and greater than or equal to limit
  194. *
  195. * @param {String} name The name of the variable being tested
  196. * @param {*} test The value to test
  197. * @param {Number} limit The limit value to compare against
  198. * @exception {DeveloperError} test must be typeof 'number' and greater than or equal to limit
  199. */
  200. Check.typeOf.number.greaterThanOrEquals = function (name, test, limit) {
  201. Check.typeOf.number(name, test);
  202. if (test < limit) {
  203. throw new DeveloperError('Expected ' + name + ' to be greater than or equal to' + limit + ', actual value was ' + test);
  204. }
  205. };
  206. /**
  207. * Throws if test is not typeof 'object'
  208. *
  209. * @param {String} name The name of the variable being tested
  210. * @param {*} test The value to test
  211. * @exception {DeveloperError} test must be typeof 'object'
  212. */
  213. Check.typeOf.object = function (name, test) {
  214. if (typeof test !== 'object') {
  215. throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'object', name));
  216. }
  217. };
  218. /**
  219. * Throws if test is not typeof 'boolean'
  220. *
  221. * @param {String} name The name of the variable being tested
  222. * @param {*} test The value to test
  223. * @exception {DeveloperError} test must be typeof 'boolean'
  224. */
  225. Check.typeOf.bool = function (name, test) {
  226. if (typeof test !== 'boolean') {
  227. throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'boolean', name));
  228. }
  229. };
  230. /**
  231. * Throws if test1 and test2 is not typeof 'number' and not equal in value
  232. *
  233. * @param {String} name1 The name of the first variable being tested
  234. * @param {String} name2 The name of the second variable being tested against
  235. * @param {*} test1 The value to test
  236. * @param {*} test2 The value to test against
  237. * @exception {DeveloperError} test1 and test2 should be type of 'number' and be equal in value
  238. */
  239. Check.typeOf.number.equals = function (name1, name2, test1, test2) {
  240. Check.typeOf.number(name1, test1);
  241. Check.typeOf.number(name2, test2);
  242. if (test1 !== test2) {
  243. throw new DeveloperError(name1 + ' must be equal to ' + name2 + ', the actual values are ' + test1 + ' and ' + test2);
  244. }
  245. };
  246. exports.Check = Check;
  247. exports.DeveloperError = DeveloperError;
  248. });