rxjs icon indicating copy to clipboard operation
rxjs copied to clipboard

If server send bad json, Websocket just close connection without any error,reason etc

Open drcika opened this issue 3 years ago • 2 comments

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

drcika avatar Feb 24 '22 16:02 drcika

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);

bever1337 avatar Sep 30 '22 17:09 bever1337

This issue does have a related bug: #5312

bever1337 avatar Sep 30 '22 18:09 bever1337