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
Copy file name to clipboardExpand all lines: README.md
+23Lines changed: 23 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -285,6 +285,29 @@ Deserialize stream data as BSON documents.
285
285
286
286
**Returns**: <code>Number</code> - returns the next index in the buffer after deserialization **x** numbers of documents.
287
287
288
+
## Error Handling
289
+
290
+
It is our recommendation to use `BSONError.isBSONError()` checks on errors and to avoid relying on parsing `error.message` and `error.name` strings in your code. We guarantee `BSONError.isBSONError()` checks will pass according to semver guidelines, but errors may be sub-classed or their messages may change at any time, even patch releases, as we see fit to increase the helpfulness of the errors.
291
+
292
+
Any new errors we add to the driver will directly extend an existing error class and no existing error will be moved to a different parent class outside of a major release.
293
+
This means `BSONError.isBSONError()` will always be able to accurately capture the errors that our BSON library throws.
294
+
295
+
Hypothetical example: A collection in our Db has an issue with UTF-8 data:
Copy file name to clipboardExpand all lines: docs/upgrade-to-v5.md
+24Lines changed: 24 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -264,3 +264,27 @@ You can now find compiled bundles of the BSON library in 3 common formats in the
264
264
- ES Module - `lib/bson.mjs`
265
265
- Immediate Invoked Function Expression (IIFE) - `lib/bson.bundle.js`
266
266
- Typically used when trying to import JS on the web CDN style, but the ES Module (`.mjs`) bundle is fully browser compatible and should be preferred if it works in your use case.
267
+
268
+
### `BSONTypeError` removed and `BSONError` offers filtering functionality with `static isBSONError()`
269
+
270
+
`BSONTypeError` has been removed because it was not a subclass of BSONError so would not return true for an `instanceof` check against `BSONError`. To learn more about our expectations of error handling see [this section of the mongodb driver's readme](https://github.com/mongodb/node-mongodb-native/tree/main#error-handling).
271
+
272
+
273
+
A `BSONError` can be thrown from deep within a library that relies on BSON, having one error super class for the library helps with programmatic filtering of an error's origin.
274
+
Since BSON can be used in environments where instances may originate from across realms, `BSONError` has a static `isBSONError()` method that helps with determining if an object is a `BSONError` instance (much like [Array.isArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray)).
275
+
It is our recommendation to use `isBSONError()` checks on errors and to avoid relying on parsing `error.message` and `error.name` strings in your code. We guarantee `isBSONError()` checks will pass according to semver guidelines, but errors may be sub-classed or their messages may change at any time, even patch releases, as we see fit to increase the helpfulness of the errors.
276
+
277
+
Hypothetical example: A collection in our Db has an issue with UTF-8 data:
@@ -328,7 +328,7 @@ export class UUID extends Binary {
328
328
}elseif(typeofinput==='string'){
329
329
bytes=uuidHexStringToBuffer(input);
330
330
}else{
331
-
thrownewBSONTypeError(
331
+
thrownewBSONError(
332
332
'Argument passed in UUID constructor must be a UUID, a 16 byte Buffer or a 32/36 character hex string (dashes excluded/included, format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).'
0 commit comments