@@ -167,6 +167,19 @@ export function serializeToString(data: any): string {
167
167
) ;
168
168
}
169
169
170
+ function safeToString ( val : any ) : string {
171
+ try {
172
+ return String ( val ) ;
173
+ } catch ( err ) {
174
+ if ( typeof val === 'object' ) {
175
+ // An object with no prototype and no `[Symbol.toPrimitive]()`, `toString()`, and `valueOf()` methods would throw.
176
+ // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#string_coercion
177
+ return '[ object Object ] ';
178
+ }
179
+ throw err ;
180
+ }
181
+ }
182
+
170
183
// based on https://github.com/tmpfs/format-util/blob/0e62d430efb0a1c51448709abd3e2406c14d8401/format.js#L1
171
184
// based on https://developer.mozilla.org/en-US/docs/Web/API/console#Using_string_substitutions
172
185
// Implements s, d, i and f placeholders
@@ -176,7 +189,7 @@ export function formatConsoleArgumentsToSingleString(
176
189
) : string {
177
190
const args = inputArgs . slice ( ) ;
178
191
179
- let formatted : string = String ( maybeMessage ) ;
192
+ let formatted : string = safeToString ( maybeMessage ) ;
180
193
181
194
// If the first argument is a string, check for substitutions.
182
195
if ( typeof maybeMessage === 'string' ) {
@@ -211,14 +224,14 @@ export function formatConsoleArgumentsToSingleString(
211
224
// Arguments that remain after formatting.
212
225
if ( args . length ) {
213
226
for ( let i = 0 ; i < args . length ; i ++ ) {
214
- formatted += ' ' + String ( args [ i ] ) ;
227
+ formatted += ' ' + safeToString ( args [ i ] ) ;
215
228
}
216
229
}
217
230
218
231
// Update escaped %% values.
219
232
formatted = formatted . replace ( / % { 2 , 2 } / g, '%' ) ;
220
233
221
- return String ( formatted ) ;
234
+ return String ( ) ;
222
235
}
223
236
224
237
export function isSynchronousXHRSupported ( ) : boolean {
0 commit comments