If server send bad json, Websocket just close connection without any error,reason etc
Describe the bug
If server send bad json, Websocket just close connection without any error,reason etc in vanila websocket json throw error
Expected behavior
just report error, do not close connection but must log error or put try catch on JSON.parse(message.data)
Reproduction code
No response
Reproduction URL
No response
Version
7.4.0
Environment
No response
Additional context
No response
In RxJS, an 'error' indicates that an observable has finished. The WebSocketSubject does not provide a stream for non-critical (application level) errors. Instead, all errors are escalated and treated like an error event fired from the underlying WebSocket. There is a try/catch around serialization as suggested in the ticket description. The catch code path is what is responsible for calling error on the observable.
I believe this example is a non-issue. RxJS allows developers to provide their own de-serializer and serializer which can intercept JSON parsing errors.
See: https://rxjs.dev/api/webSocket/WebSocketSubjectConfig#examples Partial example:
import { webSocket } from 'rxjs/webSocket';
const wsSubject = webSocket({
url: 'ws://localhost:8081',
//Apply any transformation [of](https://rxjs.dev/api/index/function/of) your choice.
deserializer({ data }) {
try {
return JSON.parse(data);
} catch (jsonParsingError) {
// the deserializer must return something! Return a valid value instead of emitting the error
return { error: jsonParsingError.toString() };
}
},
});
wsSubject.subscribe(console.log);
This issue does have a related bug: #5312