@@ -14,29 +14,53 @@ import {
14
14
MongoSystemError ,
15
15
NODE_IS_RECOVERING_ERROR_MESSAGE
16
16
} from '../../src/error' ;
17
+ import * as importsFromErrorSrc from '../../src/error' ;
17
18
import {
18
19
MongoError ,
19
20
MongoNetworkError ,
20
21
MongoParseError ,
21
22
MongoServerError ,
22
- MongoWriteConcernError
23
+ MongoWriteConcernError ,
24
+ TopologyDescription
23
25
} from '../../src/index' ;
24
- import * as EverythingFromDriver from '../../src/index' ;
26
+ import * as importsFromEntryPoint from '../../src/index' ;
25
27
import { Topology } from '../../src/sdam/topology' ;
26
- import { isHello , ns } from '../../src/utils' ;
28
+ import { isHello , ns , setDifference } from '../../src/utils' ;
27
29
import { ReplSetFixture } from '../tools/common' ;
28
30
import { cleanup } from '../tools/mongodb-mock/index' ;
29
31
import { getSymbolFrom } from '../tools/utils' ;
30
32
31
33
describe ( 'MongoErrors' , ( ) => {
32
- let errorClasses = Object . fromEntries (
33
- Object . entries ( EverythingFromDriver ) . filter ( ( [ key ] ) => key . endsWith ( 'Error' ) )
34
+ let errorClassesFromEntryPoint = Object . fromEntries (
35
+ Object . entries ( importsFromEntryPoint ) . filter (
36
+ ( [ key , value ] ) => key . endsWith ( 'Error' ) && value . toString ( ) . startsWith ( 'class' )
37
+ )
34
38
) as any ;
35
- errorClasses = { ...errorClasses , MongoPoolClosedError, MongoWaitQueueTimeoutError } ;
39
+ errorClassesFromEntryPoint = {
40
+ ...errorClassesFromEntryPoint ,
41
+ MongoPoolClosedError,
42
+ MongoWaitQueueTimeoutError
43
+ } ;
44
+
45
+ const errorClassesFromErrorSrc = Object . fromEntries (
46
+ Object . entries ( importsFromErrorSrc ) . filter (
47
+ ( [ key , value ] ) => key . endsWith ( 'Error' ) && value . toString ( ) . startsWith ( 'class' )
48
+ )
49
+ ) ;
50
+
51
+ it ( 'all defined errors should be public' , ( ) => {
52
+ expect (
53
+ setDifference ( Object . keys ( errorClassesFromEntryPoint ) , Object . keys ( errorClassesFromErrorSrc ) )
54
+ ) . to . have . property ( 'size' , 3 ) ;
55
+
56
+ expect (
57
+ setDifference ( Object . keys ( errorClassesFromErrorSrc ) , Object . keys ( errorClassesFromEntryPoint ) )
58
+ ) . to . have . property ( 'size' , 0 ) ;
59
+ } ) ;
36
60
37
- for ( const [ errorName , errorClass ] of Object . entries ( errorClasses ) ) {
38
- describe ( errorName , ( ) => {
39
- it ( `name should be read-only` , ( ) => {
61
+ describe ( 'error names should be read-only' , ( ) => {
62
+ for ( const [ errorName , errorClass ] of Object . entries ( errorClassesFromEntryPoint ) ) {
63
+ it ( `${ errorName } should be read-only` , ( ) => {
40
64
// Dynamically create error class with message
41
65
const error = new ( errorClass as any ) ( 'generated by test' , { } ) ;
42
66
// expect name property to be class name
@@ -48,8 +72,8 @@ describe('MongoErrors', () => {
48
72
} catch ( err ) { }
49
73
expect ( error ) . to . have . property ( 'name' , errorName ) ;
50
74
} ) ;
51
- } ) ;
52
- }
75
+ }
76
+ } ) ;
53
77
54
78
describe ( 'MongoError#constructor' , ( ) => {
55
79
it ( 'should accept a string' , function ( ) {
@@ -98,7 +122,7 @@ describe('MongoErrors', () => {
98
122
error : {
99
123
code : 123
100
124
}
101
- } as any as EverythingFromDriver . TopologyDescription ;
125
+ } as any as TopologyDescription ;
102
126
103
127
const error = new MongoSystemError ( 'something went wrong' , topologyDescription ) ;
104
128
expect ( error ) . to . haveOwnProperty ( 'code' , 123 ) ;
@@ -107,7 +131,16 @@ describe('MongoErrors', () => {
107
131
108
132
context ( 'when the topology description does not contain a code' , ( ) => {
109
133
it ( 'contains the code as a top level property' , ( ) => {
110
- const topologyDescription = { } as any as EverythingFromDriver . TopologyDescription ;
134
+ const topologyDescription = { error : { } } as any as TopologyDescription ;
135
+
136
+ const error = new MongoSystemError ( 'something went wrong' , topologyDescription ) ;
137
+ expect ( error ) . to . haveOwnProperty ( 'code' , undefined ) ;
138
+ } ) ;
139
+ } ) ;
140
+
141
+ context ( 'when the topology description does not contain an error property' , ( ) => {
142
+ it ( 'contains the code as a top level property' , ( ) => {
143
+ const topologyDescription = { } as any as TopologyDescription ;
111
144
112
145
const error = new MongoSystemError ( 'something went wrong' , topologyDescription ) ;
113
146
expect ( error ) . to . haveOwnProperty ( 'code' , undefined ) ;
0 commit comments