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(webSocket): manage serializer throwing exceptions #7114

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
46 changes: 46 additions & 0 deletions spec/observables/dom/webSocket-spec.ts
Expand Up @@ -416,6 +416,52 @@ describe('webSocket', () => {
subject.unsubscribe();
});

it('should take a serializer', () => {
const subject = webSocket<{ data: string}>({
url: 'ws://mysocket',
serializer: (e: any) => {
return e.data + '!';
}
});

subject.subscribe();

const socket = MockWebSocket.lastSocket;
socket.open();


['ahoy', 'yarr', 'shove off'].forEach(x => {
subject.next({ data: x });
expect(socket.lastMessageSent).to.equal(x + '!');
});

subject.unsubscribe();
});

it('if the serializer fails it should close the connection and rethrow the error', () => {
const subject = webSocket<string>({
url: 'ws://mysocket',
serializer: (e: any) => {
throw new Error('I am a bad error');
}
});

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

const socket = MockWebSocket.lastSocket;
socket.open();

expect(() => subject.next('weee!')).to.throw('I am a bad error');
voliva marked this conversation as resolved.
Show resolved Hide resolved
expect(socket.readyState).to.equal(WebSocketState.CLOSING);
expect(socket.closeCode).to.equal(1000);

subject.unsubscribe();
});

it('should accept a closingObserver', () => {
let calls = 0;
const subject = webSocket<string>(<any>{
Expand Down
23 changes: 15 additions & 8 deletions src/internal/observable/dom/WebSocketSubject.ts
Expand Up @@ -296,18 +296,25 @@ export class WebSocketSubject<T> extends AnonymousSubject<T> {

const queue = this.destination;

this.destination = Subscriber.create<T>(
(x) => {
this.destination = new Subscriber<T>({
voliva marked this conversation as resolved.
Show resolved Hide resolved
next: (x: T) => {
if (socket!.readyState === 1) {
try {
const { serializer } = this._config;
socket!.send(serializer!(x!));
} catch (e) {
this.destination!.error(e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say the actual bug was here. We clearly have a handle to the wrong thing. 🤔

} catch (e: any) {
if (e && e.code) {
voliva marked this conversation as resolved.
Show resolved Hide resolved
this.destination!.error(e);
} else {
this.destination!.error({
code: 1000,
});
throw e;
}
}
}
},
(err) => {
error: (err: any) => {
const { closingObserver } = this._config;
if (closingObserver) {
closingObserver.next(undefined);
Expand All @@ -319,15 +326,15 @@ export class WebSocketSubject<T> extends AnonymousSubject<T> {
}
this._resetState();
},
() => {
complete: () => {
const { closingObserver } = this._config;
if (closingObserver) {
closingObserver.next(undefined);
}
socket!.close();
this._resetState();
}
) as Subscriber<any>;
},
}) as Subscriber<any>;

if (queue && queue instanceof ReplaySubject) {
subscription.add((queue as ReplaySubject<T>).subscribe(this.destination));
Expand Down