Skip to content

Commit 56e348d

Browse files
committed
fix: comments
1 parent 3887429 commit 56e348d

File tree

8 files changed

+35
-13
lines changed

8 files changed

+35
-13
lines changed

src/bson.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export {
5050
Decimal128
5151
};
5252
export { BSONValue } from './bson_value';
53-
export { BSONError } from './error';
53+
export { BSONError, BSONVersionError } from './error';
5454
export { BSONType } from './constants';
5555
export { EJSON } from './extended_json';
5656

src/error.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,14 @@ export class BSONError extends Error {
4343
);
4444
}
4545
}
46+
47+
/** @public */
48+
export class BSONVersionError extends BSONError {
49+
get name(): 'BSONVersionError' {
50+
return 'BSONVersionError';
51+
}
52+
53+
constructor() {
54+
super('Unsupported BSON version, bson types must be from bson 5.0 or later');
55+
}
56+
}

src/extended_json.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
import { DBRef, isDBRefLike } from './db_ref';
1212
import { Decimal128 } from './decimal128';
1313
import { Double } from './double';
14-
import { BSONError } from './error';
14+
import { BSONError, BSONVersionError } from './error';
1515
import { Int32 } from './int_32';
1616
import { Long } from './long';
1717
import { MaxKey } from './max_key';
@@ -318,7 +318,7 @@ function serializeDocument(doc: any, options: EJSONSerializeOptions) {
318318
typeof doc._bsontype === 'string' &&
319319
doc[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION
320320
) {
321-
throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
321+
throw new BSONVersionError();
322322
} else if (isBSONType(doc)) {
323323
// the "document" is really just a BSON type object
324324
// eslint-disable-next-line @typescript-eslint/no-explicit-any

src/parser/calculate_size.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Binary } from '../binary';
22
import type { Document } from '../bson';
3-
import { BSONError } from '../error';
3+
import { BSONVersionError } from '../error';
44
import * as constants from '../constants';
55
import { ByteUtils } from '../utils/byte_utils';
66
import { isAnyArrayBuffer, isDate, isRegExp } from './utils';
@@ -83,7 +83,7 @@ function calculateElement(
8383
typeof value._bsontype === 'string' &&
8484
value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION
8585
) {
86-
throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
86+
throw new BSONVersionError();
8787
} else if (
8888
value == null ||
8989
value['_bsontype'] === 'MinKey' ||

src/parser/serializer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as constants from '../constants';
55
import type { DBRefLike } from '../db_ref';
66
import type { Decimal128 } from '../decimal128';
77
import type { Double } from '../double';
8-
import { BSONError } from '../error';
8+
import { BSONError, BSONVersionError } from '../error';
99
import type { Int32 } from '../int_32';
1010
import { Long } from '../long';
1111
import type { MinKey } from '../min_key';
@@ -710,7 +710,7 @@ export function serializeInto(
710710
typeof value === 'object' &&
711711
value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION
712712
) {
713-
throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
713+
throw new BSONVersionError();
714714
} else if (value._bsontype === 'ObjectId') {
715715
index = serializeObjectId(buffer, key, value, index);
716716
} else if (value._bsontype === 'Decimal128') {
@@ -820,7 +820,7 @@ export function serializeInto(
820820
typeof value === 'object' &&
821821
value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION
822822
) {
823-
throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
823+
throw new BSONVersionError();
824824
} else if (value._bsontype === 'ObjectId') {
825825
index = serializeObjectId(buffer, key, value, index);
826826
} else if (type === 'object' && value._bsontype === 'Decimal128') {
@@ -930,7 +930,7 @@ export function serializeInto(
930930
typeof value === 'object' &&
931931
value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION
932932
) {
933-
throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
933+
throw new BSONVersionError();
934934
} else if (value._bsontype === 'ObjectId') {
935935
index = serializeObjectId(buffer, key, value, index);
936936
} else if (type === 'object' && value._bsontype === 'Decimal128') {

test/node/cross_compat.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,17 @@ const BSONTypeClasses = {
6464
};
6565

6666
describe('Prevent previous major versions from working with BSON v5 serialize and stringify', function () {
67-
for (const [typeName, typeMaker] of Object.entries(BSONTypeClasses)) {
67+
for (const [typeName, typeFactory] of Object.entries(BSONTypeClasses)) {
6868
it(`serialize throws if ${typeName} is missing a version symbol`, () => {
69-
const type = typeMaker();
69+
const type = typeFactory();
7070
Object.defineProperty(type, Symbol.for('@@mdb.bson.version'), { value: null }); // set an own property that overrides the getter
7171
expect(() => BSON.serialize({ type })).to.throw(/Unsupported BSON version/);
7272
expect(() => BSON.serialize({ a: [type] })).to.throw(/Unsupported BSON version/);
7373
expect(() => BSON.serialize(new Map([['type', type]]))).to.throw(/Unsupported BSON version/);
7474
});
7575

7676
it(`stringify throws if ${typeName} is missing a version symbol`, () => {
77-
const type = typeMaker();
77+
const type = typeFactory();
7878
Object.defineProperty(type, Symbol.for('@@mdb.bson.version'), { value: null }); // set an own property that overrides the getter
7979
expect(() => EJSON.stringify({ type })).to.throw(/Unsupported BSON version/);
8080
expect(() => EJSON.stringify({ a: [type] })).to.throw(/Unsupported BSON version/);

test/node/error.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect } from 'chai';
22
import { loadESModuleBSON } from '../load_bson';
33

4-
import { __isWeb__, BSONError } from '../register-bson';
4+
import { __isWeb__, BSONError, BSONVersionError } from '../register-bson';
55

66
const instanceOfChecksWork = !__isWeb__;
77

@@ -82,4 +82,14 @@ describe('BSONError', function () {
8282
expect(bsonErr.name).equals('BSONError');
8383
expect(bsonErr.message).equals('This is a BSONError message');
8484
});
85+
86+
describe('class BSONVersionError', () => {
87+
it('is a BSONError instance', () => {
88+
expect(BSONError.isBSONError(new BSONVersionError())).to.be.true;
89+
});
90+
91+
it('has a name property equal to "BSONVersionError"', () => {
92+
expect(new BSONVersionError()).to.have.property('name', 'BSONVersionError');
93+
});
94+
});
8595
});

test/node/exports.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const EXPECTED_EXPORTS = [
88

99
'BSONType',
1010
'BSONValue',
11+
'BSONVersionError',
1112
'EJSON',
1213
'Code',
1314
'BSONSymbol',

0 commit comments

Comments
 (0)