@@ -8,6 +8,7 @@ import { OnConnect } from '../../src/decorators/OnConnect';
88import { ConnectedSocket } from '../../src/decorators/ConnectedSocket' ;
99import { waitForEvent } from '../utilities/waitForEvent' ;
1010import { EmitOnFail , OnMessage } from '../../src' ;
11+ import { waitForTime } from '../utilities/waitForTime' ;
1112
1213describe ( 'EmitOnFail' , ( ) => {
1314 const PORT = 8080 ;
@@ -124,4 +125,83 @@ describe('EmitOnFail', () => {
124125 expect ( errors [ 0 ] ) . toEqual ( 'error string' ) ;
125126 expect ( errors . length ) . toEqual ( 1 ) ;
126127 } ) ;
128+
129+ it ( 'Emit defined event on failing with specific error type' , async ( ) => {
130+ @SocketController ( '/string' )
131+ @Service ( )
132+ class TestController {
133+ @OnConnect ( )
134+ connected ( @ConnectedSocket ( ) socket : Socket ) {
135+ socket . emit ( 'connected' ) ;
136+ }
137+
138+ @OnMessage ( 'request' )
139+ @EmitOnFail ( 'fail1' , { errorType : RangeError } )
140+ @EmitOnFail ( 'fail2' , { errorType : TypeError } )
141+ @EmitOnFail ( 'fail3' )
142+ async testEvent ( ) {
143+ throw new RangeError ( 'range error' ) ;
144+ }
145+
146+ @OnMessage ( 'request2' )
147+ @EmitOnFail ( 'fail1' , { errorType : RangeError } )
148+ @EmitOnFail ( 'fail2' , { errorType : TypeError } )
149+ @EmitOnFail ( 'fail3' )
150+ async testEvent2 ( ) {
151+ throw new TypeError ( 'type error' ) ;
152+ }
153+
154+ @OnMessage ( 'request3' )
155+ @EmitOnFail ( 'fail1' , { errorType : RangeError } )
156+ @EmitOnFail ( 'fail2' , { errorType : TypeError } )
157+ @EmitOnFail ( 'fail3' )
158+ async testEvent3 ( ) {
159+ throw new Error ( 'test error' ) ;
160+ }
161+
162+ @OnMessage ( 'request4' )
163+ @EmitOnFail ( 'fail1' , { errorType : Error } )
164+ @EmitOnFail ( 'fail2' , { errorType : TypeError } )
165+ @EmitOnFail ( 'fail3' )
166+ async testEvent4 ( ) {
167+ throw new TypeError ( 'type error 2' ) ;
168+ }
169+ }
170+
171+ socketControllers = new SocketControllers ( {
172+ io : wsApp ,
173+ container : Container ,
174+ controllers : [ TestController ] ,
175+ } ) ;
176+ wsClient = io ( PATH_FOR_CLIENT + '/string' , { reconnection : false , timeout : 5000 , forceNew : true } ) ;
177+
178+ const errors = { fail1 : [ ] , fail2 : [ ] , fail3 : [ ] } ;
179+
180+ wsClient . on ( 'fail1' , data => {
181+ errors . fail1 . push ( data ) ;
182+ } ) ;
183+
184+ wsClient . on ( 'fail2' , data => {
185+ errors . fail2 . push ( data ) ;
186+ } ) ;
187+
188+ wsClient . on ( 'fail3' , data => {
189+ errors . fail3 . push ( data ) ;
190+ } ) ;
191+
192+ await waitForEvent ( wsClient , 'connected' ) ;
193+
194+ wsClient . emit ( 'request' ) ;
195+ wsClient . emit ( 'request2' ) ;
196+ wsClient . emit ( 'request3' ) ;
197+ wsClient . emit ( 'request4' ) ;
198+
199+ await waitForTime ( 1000 ) ;
200+
201+ expect ( errors ) . toEqual ( {
202+ fail1 : [ 'range error' , 'type error 2' ] ,
203+ fail2 : [ 'type error' ] ,
204+ fail3 : [ 'test error' ] ,
205+ } ) ;
206+ } ) ;
127207} ) ;
0 commit comments