1111
1212goog . provide ( 'jspb.asserts' ) ;
1313
14- /**
15- * Error object for failed assertions.
16- *
17- * @extends {Error }
18- * @final
19- */
20-
21- class JspbAssertionError extends Error {
22- /**
23- * @param {string } messagePattern The pattern that was used to form message.
24- * @param {!Array<*> } messageArgs The items to substitute into the pattern.
25- */
26- constructor ( messagePattern , messageArgs ) {
27- super ( subs ( messagePattern , messageArgs ) ) ;
28-
29- /**
30- * The message pattern used to format the error message. Error handlers can
31- * use this to uniquely identify the assertion.
32- * @type {string }
33- */
34- this . messagePattern = messagePattern ;
35- }
36- }
37-
38- jspb . asserts . JspbAssertionError = JspbAssertionError ;
39-
40- /**
41- * The default error handler.
42- * @param {!JspbAssertionError } e The exception to be handled.
43- * @return {void }
44- */
45- jspb . asserts . JSPB_DEFAULT_ERROR_HANDLER = function ( e ) {
46- throw e ;
47- }
48-
49-
50-
51- /**
52- * The handler responsible for throwing or logging assertion errors.
53- * @type {function(!JspbAssertionError) }
54- */
55- let errorHandler_ = jspb . asserts . JSPB_DEFAULT_ERROR_HANDLER ;
56-
57-
5814/**
5915 * Does simple python-style string substitution.
6016 * subs("foo%s hot%s", "bar", "dog") becomes "foobar hotdog".
@@ -85,7 +41,7 @@ function subs(pattern, subs) {
8541 * @param {?Array<*> } defaultArgs The substitution arguments for defaultMessage.
8642 * @param {string|undefined } givenMessage Message supplied by the caller.
8743 * @param {!Array<*> } givenArgs The substitution arguments for givenMessage.
88- * @throws {JspbAssertionError } When the value is not a number.
44+ * @throws {Error } When the value is not a number.
8945 */
9046function doAssertFailure ( defaultMessage , defaultArgs , givenMessage , givenArgs ) {
9147 let message = 'Assertion failed' ;
@@ -101,36 +57,22 @@ function doAssertFailure(defaultMessage, defaultArgs, givenMessage, givenArgs) {
10157 // a stack trace is added to var message above. With this, a stack trace is
10258 // not added until this line (it causes the extra garbage to be added after
10359 // the assertion message instead of in the middle of it).
104- const e = new JspbAssertionError ( '' + message , args || [ ] ) ;
105- errorHandler_ ( e ) ;
60+ throw new Error ( '' + message , args || [ ] ) ;
10661}
10762
108- /**
109- * Sets a custom error handler that can be used to customize the behavior of
110- * assertion failures, for example by turning all assertion failures into log
111- * messages.
112- * @param {function(!JspbAssertionError) } errorHandler
113- * @return {void }
114- */
115- jspb . asserts . setJspbErrorHandler = function ( errorHandler ) {
116- errorHandler_ = errorHandler ;
117- } ;
118-
119-
12063/**
12164 * Checks if the condition evaluates to true.
12265 * @template T
12366 * @param {T } condition The condition to check.
12467 * @param {string= } opt_message Error message in case of failure.
125- * @param {...* } var_args The items to substitute into the failure message.
68+ * @param {...* } args The items to substitute into the failure message.
12669 * @return {T } The value of the condition.
127- * @throws {JspbAssertionError } When the condition evaluates to false.
70+ * @throws {Error } When the condition evaluates to false.
12871 */
12972
130- jspb . asserts . jspbAssert = function ( condition , opt_message , var_args ) {
73+ jspb . asserts . assert = function ( condition , opt_message , ... args ) {
13174 if ( ! condition ) {
132- doAssertFailure (
133- '' , null , opt_message , Array . prototype . slice . call ( arguments , 2 ) ) ;
75+ doAssertFailure ( '' , null , opt_message , args ) ;
13476 }
13577 return condition ;
13678} ;
@@ -140,15 +82,15 @@ jspb.asserts.jspbAssert = function(condition, opt_message, var_args) {
14082 * Checks if the value is a string.
14183 * @param {* } value The value to check.
14284 * @param {string= } opt_message Error message in case of failure.
143- * @param {...* } var_args The items to substitute into the failure message.
85+ * @param {...* } args The items to substitute into the failure message.
14486 * @return {string } The value, guaranteed to be a string when asserts enabled.
145- * @throws {JspbAssertionError } When the value is not a string.
87+ * @throws {Error } When the value is not a string.
14688 */
147- jspb . asserts . jspbAssertString = function ( value , opt_message , var_args ) {
89+ jspb . asserts . assertString = function ( value , opt_message , ... args ) {
14890 if ( typeof value !== 'string' ) {
14991 doAssertFailure (
15092 'Expected string but got %s: %s.' , [ goog . typeOf ( value ) , value ] ,
151- opt_message , Array . prototype . slice . call ( arguments , 2 ) ) ;
93+ opt_message , args ) ;
15294 }
15395 return /** @type {string } */ ( value ) ;
15496} ;
@@ -158,15 +100,15 @@ jspb.asserts.jspbAssertString = function(value, opt_message, var_args) {
158100 * Checks if the value is an Array.
159101 * @param {* } value The value to check.
160102 * @param {string= } opt_message Error message in case of failure.
161- * @param {...* } var_args The items to substitute into the failure message.
103+ * @param {...* } args The items to substitute into the failure message.
162104 * @return {!Array<?> } The value, guaranteed to be a non-null array.
163- * @throws {JspbAssertionError } When the value is not an array.
105+ * @throws {Error } When the value is not an array.
164106 */
165- jspb . asserts . jspbAssertArray = function ( value , opt_message , var_args ) {
107+ jspb . asserts . assertArray = function ( value , opt_message , ... args ) {
166108 if ( ! Array . isArray ( value ) ) {
167109 doAssertFailure (
168110 'Expected array but got %s: %s.' , [ goog . typeOf ( value ) , value ] ,
169- opt_message , Array . prototype . slice . call ( arguments , 2 ) ) ;
111+ opt_message , args ) ;
170112 }
171113 return /** @type {!Array<?> } */ ( value ) ;
172114} ;
@@ -185,14 +127,14 @@ jspb.asserts.jspbAssertArray = function(value, opt_message, var_args) {
185127 * </pre>
186128 *
187129 * @param {string= } opt_message Error message in case of failure.
188- * @param {...* } var_args The items to substitute into the failure message.
130+ * @param {...* } args The items to substitute into the failure message.
189131 * @return {void }
190- * @throws {JspbAssertionError } Failure.
132+ * @throws {Error } Failure.
191133 */
192- jspb . asserts . jspbFail = function ( opt_message , var_args ) {
193- errorHandler_ ( new JspbAssertionError (
134+ jspb . asserts . fail = function ( opt_message , ... args ) {
135+ throw new Error (
194136 'Failure' + ( opt_message ? ': ' + opt_message : '' ) ,
195- Array . prototype . slice . call ( arguments , 1 ) ) ) ;
137+ args ) ;
196138} ;
197139
198140/**
@@ -205,17 +147,17 @@ jspb.asserts.jspbFail = function(opt_message, var_args) {
205147 * @param {? } value The value to check.
206148 * @param {function(new: T, ...) } type A user-defined constructor.
207149 * @param {string= } opt_message Error message in case of failure.
208- * @param {...* } var_args The items to substitute into the failure message.
209- * @throws {JspbAssertionError } When the value is not an instance of
150+ * @param {...* } args The items to substitute into the failure message.
151+ * @throws {Error } When the value is not an instance of
210152 * type.
211153 * @return {T }
212154 * @template T
213155 */
214- jspb . asserts . jspbAssertInstanceof = function ( value , type , opt_message , var_args ) {
156+ jspb . asserts . assertInstanceof = function ( value , type , opt_message , ... args ) {
215157 if ( ! ( value instanceof type ) ) {
216158 doAssertFailure (
217159 'Expected instanceof %s but got %s.' , [ getType ( type ) , getType ( value ) ] ,
218- opt_message , Array . prototype . slice . call ( arguments , 3 ) ) ;
160+ opt_message , args ) ;
219161 }
220162 return value ;
221163} ;
0 commit comments