3

I've created a simple rx operator that converts a stream of strings to a stream of jsons and it works fine. However, I would like to be able to raise a custom exception and I am not sure how to call the on_error method of the subscription

The operator is called convertStringToJson and a working sample can be found here: https://github.com/cipriancaba/rxcpp-examples/blob/master/src/SimpleOperators.cpp

function<observable<json>(observable<string>)> SimpleOperators::convertFromStringToJson() {
  return [](observable<string> $str) {
    return $str |
      Rx::map([](const string s) {
        return json::parse(s);
      });
  };
}
Ciprian
  • 507
  • 4
  • 12

1 Answers1

4

rxcpp will work if you use try/catch to translate the exception.

However, the intended pattern is to use on_error_resume_next() to translate the exception.

This is the code:

function<observable<json>(observable<string>)> SimpleOperators::convertFromStringToJson() {
  return [](observable<string> $str) {
    return $str |
      Rx::map([](const string& s) {
          return json::parse(s);
      }) |
      Rx::on_error_resume_next([](std::exception_ptr){
        return Rx::error<json>(runtime_error("custom exception"));
      });
  };
}

I opened a pull request on github with this code.

https://github.com/cipriancaba/rxcpp-examples/pull/1

Kirk Shoop
  • 1,154
  • 7
  • 12