@@ -46,4 +46,71 @@ describe('isFastFailureConnectionError', function () {
4646 isFastFailureConnectionError ( new Error ( 'could not connect' ) ) ,
4747 ) . to . equal ( false ) ;
4848 } ) ;
49+
50+ describe ( 'isCompassSocketServiceError' , function ( ) {
51+ class CompassSocketServiceError extends Error {
52+ constructor (
53+ msg : string ,
54+ public code : number ,
55+ ) {
56+ super ( msg ) ;
57+ this . name = 'CompassSocketServiceError' ;
58+ }
59+ }
60+
61+ it ( 'returns true for UNAUTHORIZED (3000)' , function ( ) {
62+ const error = new CompassSocketServiceError ( 'Unauthorized' , 3000 ) ;
63+ expect ( isFastFailureConnectionError ( error ) ) . to . equal ( true ) ;
64+ } ) ;
65+
66+ it ( 'returns true for FORBIDDEN (3003)' , function ( ) {
67+ const error = new CompassSocketServiceError ( 'Forbidden' , 3003 ) ;
68+ expect ( isFastFailureConnectionError ( error ) ) . to . equal ( true ) ;
69+ } ) ;
70+
71+ it ( 'returns true for NOT_FOUND (4004)' , function ( ) {
72+ const error = new CompassSocketServiceError ( 'Not found' , 4004 ) ;
73+ expect ( isFastFailureConnectionError ( error ) ) . to . equal ( true ) ;
74+ } ) ;
75+
76+ it ( 'returns true for VIOLATED_POLICY (1008)' , function ( ) {
77+ const error = new CompassSocketServiceError ( 'Violated policy' , 1008 ) ;
78+ expect ( isFastFailureConnectionError ( error ) ) . to . equal ( true ) ;
79+ } ) ;
80+
81+ it ( 'returns true for DO_NOT_TRY_AGAIN (4101)' , function ( ) {
82+ const error = new CompassSocketServiceError ( 'Do not try again' , 4101 ) ;
83+ expect ( isFastFailureConnectionError ( error ) ) . to . equal ( true ) ;
84+ } ) ;
85+
86+ it ( 'returns false for CompassSocketServiceError with non-fail-fast code' , function ( ) {
87+ const error = new CompassSocketServiceError ( 'Some other error' , 9999 ) ;
88+ expect ( isFastFailureConnectionError ( error ) ) . to . equal ( false ) ;
89+ } ) ;
90+
91+ it ( 'returns true when CompassSocketServiceError is the cause of MongoNetworkError' , function ( ) {
92+ const cause = new CompassSocketServiceError ( 'Unauthorized' , 3000 ) ;
93+ const error = new MongoNetworkError ( 'Connection failed' ) ;
94+ ( error as any ) . cause = cause ;
95+ expect ( isFastFailureConnectionError ( error ) ) . to . equal ( true ) ;
96+ } ) ;
97+
98+ it ( 'returns true when CompassSocketServiceError is nested deeply' , function ( ) {
99+ const cause = new CompassSocketServiceError ( 'Forbidden' , 3003 ) ;
100+ const wrappedError = new Error ( 'Wrapped error' ) ;
101+ ( wrappedError as any ) . cause = cause ;
102+ const error = new MongoNetworkError ( 'Connection failed' ) ;
103+ ( error as any ) . cause = wrappedError ;
104+ expect ( isFastFailureConnectionError ( error ) ) . to . equal ( true ) ;
105+ } ) ;
106+
107+ it ( 'returns true when CompassSocketServiceError is in an AggregateError' , function ( ) {
108+ const cause = new CompassSocketServiceError ( 'Not found' , 4004 ) ;
109+ const aggregateError = new AggregateError (
110+ [ new Error ( 'Other error' ) , cause ] ,
111+ 'Multiple errors' ,
112+ ) ;
113+ expect ( isFastFailureConnectionError ( aggregateError ) ) . to . equal ( true ) ;
114+ } ) ;
115+ } ) ;
49116} ) ;
0 commit comments