Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(WebSocketSubject): pass constructor errors onto observable #1895

Merged
merged 2 commits into from Aug 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions spec/observables/dom/webSocket-spec.ts
Expand Up @@ -374,6 +374,23 @@ describe('Observable.webSocket', () => {

subject.unsubscribe();
});

it('should handle constructor errors', () => {
const subject = Observable.webSocket(<any>{
url: 'bad_url',
WebSocketCtor: (url: string, protocol?: string | string[]): WebSocket => {
throw new Error(`connection refused`);
}
});

subject.subscribe((x: any) => {
expect(x).to.equal('this should not happen');
}, (err: any) => {
expect(err).to.be.an('error', 'connection refused');
});

subject.unsubscribe();
});
});

describe('multiplex', () => {
Expand Down
19 changes: 13 additions & 6 deletions src/observable/dom/WebSocketSubject.ts
Expand Up @@ -114,19 +114,26 @@ export class WebSocketSubject<T> extends AnonymousSubject<T> {

private _connectSocket() {
const { WebSocketCtor } = this;
const socket = this.protocol ?
new WebSocketCtor(this.url, this.protocol) :
new WebSocketCtor(this.url);
this.socket = socket;
const observer = this._output;

let socket: WebSocket = null;
try {
socket = this.protocol ?
new WebSocketCtor(this.url, this.protocol) :
new WebSocketCtor(this.url);
this.socket = socket;
} catch (e) {
observer.error(e);
return;
}

const subscription = new Subscription(() => {
this.socket = null;
if (socket && socket.readyState === 1) {
socket.close();
}
});

const observer = this._output;

socket.onopen = (e: Event) => {
const openObserver = this.openObserver;
if (openObserver) {
Expand Down