@@ -45,8 +45,9 @@ export class InterceptorSource extends NetworkSource {
4545 * @fixme Incorrect disciminated union when merging multiple
4646 * events map in `BatchInterceptor` (results in `Listener<unknown>`).
4747 */
48- this . #interceptor. on ( 'request' , this . #onRequest. bind ( this ) as any )
49- this . #interceptor. on ( 'response' , this . #onResponse. bind ( this ) as any )
48+ this . #interceptor
49+ . on ( 'request' , this . #onRequest. bind ( this ) as any )
50+ . on ( 'response' , this . #onResponse. bind ( this ) as any )
5051
5152 this . #interceptor. on (
5253 'connection' ,
@@ -163,6 +164,30 @@ class InterceptorWebSocketNetworkFrame extends WebSocketNetworkFrame {
163164 } )
164165 }
165166
167+ public errorWith ( reason ?: unknown ) : void {
168+ if ( reason instanceof Error ) {
169+ const { client } = this . data . connection
170+
171+ /**
172+ * Use `client.errorWith(reason)` in the future.
173+ * @see https://github.com/mswjs/interceptors/issues/747
174+ */
175+ const errorEvent = new Event ( 'error' )
176+ Object . defineProperty ( errorEvent , 'cause' , {
177+ enumerable : true ,
178+ configurable : false ,
179+ value : reason ,
180+ } )
181+
182+ /**
183+ * @fixme So this all is fine but it doesn't fire at the right time.
184+ * WS interceptor does NOT await listeners.
185+ */
186+ client . socket . dispatchEvent ( errorEvent )
187+ client . close ( 1011 , reason . message )
188+ }
189+ }
190+
166191 public passthrough ( ) {
167192 this . data . connection . server . connect ( )
168193 }
0 commit comments