File tree Expand file tree Collapse file tree 2 files changed +30
-6
lines changed Expand file tree Collapse file tree 2 files changed +30
-6
lines changed Original file line number Diff line number Diff line change @@ -374,6 +374,23 @@ describe('Observable.webSocket', () => {
374
374
375
375
subject . unsubscribe ( ) ;
376
376
} ) ;
377
+
378
+ it ( 'should handle constructor errors' , ( ) => {
379
+ const subject = Observable . webSocket ( < any > {
380
+ url : 'bad_url' ,
381
+ WebSocketCtor : ( url : string , protocol ?: string | string [ ] ) : WebSocket => {
382
+ throw new Error ( `connection refused` ) ;
383
+ }
384
+ } ) ;
385
+
386
+ subject . subscribe ( ( x : any ) => {
387
+ expect ( x ) . to . equal ( 'this should not happen' ) ;
388
+ } , ( err : any ) => {
389
+ expect ( err ) . to . be . an ( 'error' , 'connection refused' ) ;
390
+ } ) ;
391
+
392
+ subject . unsubscribe ( ) ;
393
+ } ) ;
377
394
} ) ;
378
395
379
396
describe ( 'multiplex' , ( ) => {
Original file line number Diff line number Diff line change @@ -114,19 +114,26 @@ export class WebSocketSubject<T> extends AnonymousSubject<T> {
114
114
115
115
private _connectSocket ( ) {
116
116
const { WebSocketCtor } = this ;
117
- const socket = this . protocol ?
118
- new WebSocketCtor ( this . url , this . protocol ) :
119
- new WebSocketCtor ( this . url ) ;
120
- this . socket = socket ;
117
+ const observer = this . _output ;
118
+
119
+ let socket : WebSocket = null ;
120
+ try {
121
+ socket = this . protocol ?
122
+ new WebSocketCtor ( this . url , this . protocol ) :
123
+ new WebSocketCtor ( this . url ) ;
124
+ this . socket = socket ;
125
+ } catch ( e ) {
126
+ observer . error ( e ) ;
127
+ return ;
128
+ }
129
+
121
130
const subscription = new Subscription ( ( ) => {
122
131
this . socket = null ;
123
132
if ( socket && socket . readyState === 1 ) {
124
133
socket . close ( ) ;
125
134
}
126
135
} ) ;
127
136
128
- const observer = this . _output ;
129
-
130
137
socket . onopen = ( e : Event ) => {
131
138
const openObserver = this . openObserver ;
132
139
if ( openObserver ) {
You can’t perform that action at this time.
0 commit comments