11
11
12
12
goog . provide ( 'jspb.asserts' ) ;
13
13
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
-
58
14
/**
59
15
* Does simple python-style string substitution.
60
16
* subs("foo%s hot%s", "bar", "dog") becomes "foobar hotdog".
@@ -85,7 +41,7 @@ function subs(pattern, subs) {
85
41
* @param {?Array<*> } defaultArgs The substitution arguments for defaultMessage.
86
42
* @param {string|undefined } givenMessage Message supplied by the caller.
87
43
* @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.
89
45
*/
90
46
function doAssertFailure ( defaultMessage , defaultArgs , givenMessage , givenArgs ) {
91
47
let message = 'Assertion failed' ;
@@ -101,36 +57,22 @@ function doAssertFailure(defaultMessage, defaultArgs, givenMessage, givenArgs) {
101
57
// a stack trace is added to var message above. With this, a stack trace is
102
58
// not added until this line (it causes the extra garbage to be added after
103
59
// 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 || [ ] ) ;
106
61
}
107
62
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
-
120
63
/**
121
64
* Checks if the condition evaluates to true.
122
65
* @template T
123
66
* @param {T } condition The condition to check.
124
67
* @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.
126
69
* @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.
128
71
*/
129
72
130
- jspb . asserts . jspbAssert = function ( condition , opt_message , var_args ) {
73
+ jspb . asserts . assert = function ( condition , opt_message , ... args ) {
131
74
if ( ! condition ) {
132
- doAssertFailure (
133
- '' , null , opt_message , Array . prototype . slice . call ( arguments , 2 ) ) ;
75
+ doAssertFailure ( '' , null , opt_message , args ) ;
134
76
}
135
77
return condition ;
136
78
} ;
@@ -140,15 +82,15 @@ jspb.asserts.jspbAssert = function(condition, opt_message, var_args) {
140
82
* Checks if the value is a string.
141
83
* @param {* } value The value to check.
142
84
* @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.
144
86
* @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.
146
88
*/
147
- jspb . asserts . jspbAssertString = function ( value , opt_message , var_args ) {
89
+ jspb . asserts . assertString = function ( value , opt_message , ... args ) {
148
90
if ( typeof value !== 'string' ) {
149
91
doAssertFailure (
150
92
'Expected string but got %s: %s.' , [ goog . typeOf ( value ) , value ] ,
151
- opt_message , Array . prototype . slice . call ( arguments , 2 ) ) ;
93
+ opt_message , args ) ;
152
94
}
153
95
return /** @type {string } */ ( value ) ;
154
96
} ;
@@ -158,15 +100,15 @@ jspb.asserts.jspbAssertString = function(value, opt_message, var_args) {
158
100
* Checks if the value is an Array.
159
101
* @param {* } value The value to check.
160
102
* @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.
162
104
* @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.
164
106
*/
165
- jspb . asserts . jspbAssertArray = function ( value , opt_message , var_args ) {
107
+ jspb . asserts . assertArray = function ( value , opt_message , ... args ) {
166
108
if ( ! Array . isArray ( value ) ) {
167
109
doAssertFailure (
168
110
'Expected array but got %s: %s.' , [ goog . typeOf ( value ) , value ] ,
169
- opt_message , Array . prototype . slice . call ( arguments , 2 ) ) ;
111
+ opt_message , args ) ;
170
112
}
171
113
return /** @type {!Array<?> } */ ( value ) ;
172
114
} ;
@@ -185,14 +127,14 @@ jspb.asserts.jspbAssertArray = function(value, opt_message, var_args) {
185
127
* </pre>
186
128
*
187
129
* @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.
189
131
* @return {void }
190
- * @throws {JspbAssertionError } Failure.
132
+ * @throws {Error } Failure.
191
133
*/
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 (
194
136
'Failure' + ( opt_message ? ': ' + opt_message : '' ) ,
195
- Array . prototype . slice . call ( arguments , 1 ) ) ) ;
137
+ args ) ;
196
138
} ;
197
139
198
140
/**
@@ -205,17 +147,17 @@ jspb.asserts.jspbFail = function(opt_message, var_args) {
205
147
* @param {? } value The value to check.
206
148
* @param {function(new: T, ...) } type A user-defined constructor.
207
149
* @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
210
152
* type.
211
153
* @return {T }
212
154
* @template T
213
155
*/
214
- jspb . asserts . jspbAssertInstanceof = function ( value , type , opt_message , var_args ) {
156
+ jspb . asserts . assertInstanceof = function ( value , type , opt_message , ... args ) {
215
157
if ( ! ( value instanceof type ) ) {
216
158
doAssertFailure (
217
159
'Expected instanceof %s but got %s.' , [ getType ( type ) , getType ( value ) ] ,
218
- opt_message , Array . prototype . slice . call ( arguments , 3 ) ) ;
160
+ opt_message , args ) ;
219
161
}
220
162
return value ;
221
163
} ;
0 commit comments