You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/* Convert BigInt values into strings and keep other types unchanged */
134
+
typeof value ==='bigint'
135
+
?value.toString()
136
+
: value,
137
+
2
138
+
)
139
+
);
128
140
});
129
141
} catch (error) {
130
142
console.error(error);
@@ -345,6 +357,31 @@ const logger = pino();
345
357
const client = new PubSubApiClient(logger);
346
358
```
347
359
360
+
## Common Issues
361
+
362
+
### TypeError: Do not know how to serialize a BigInt
363
+
364
+
If you attempt to call `JSON.stringify` on an event you will likely see the following error:
365
+
366
+
> TypeError: Do not know how to serialize a BigInt
367
+
368
+
This happens when an integer value stored in an event field exceeds the range of the `Number` JS type (this typically happens with `commitNumber` values). In this case, we use a `BigInt` type to safely store the integer value. However, the `BigInt` type is not yet supported in standard JSON representation (see step 10 in the [BigInt TC39 spec](https://tc39.es/proposal-bigint/#sec-serializejsonproperty)) so this triggers a `TypeError`.
369
+
370
+
To avoid this error, use a replacer function to safely escape BigInt values so that they can be serialized as a string (or any other format of your choice) in JSON:
371
+
372
+
```js
373
+
// Safely log event as a JSON string
374
+
console.log(
375
+
JSON.stringify(
376
+
event,
377
+
(key, value) =>
378
+
/* Convert BigInt values into strings and keep other types unchanged */
379
+
typeof value === 'bigint' ? value.toString() : value,
0 commit comments