Skip to content

Support non-throwing-style error handling #615

Open
@bryceschober

Description

@bryceschober

Describe the proposed feature

At least in the context of CBOR and UBJSON support (all of json_ext/*?), the underlying basic_*_reader::read() and decode() functions support a non-throwing std::error_code interface, but then those get turned into exception objects that are either thrown or terminate in the implementations of decode_*().

Instead, providing an alternative interface that returns an std::expected-like object that could contain the value type or an error type that contains the underlying exception information (what()).

What other libraries (C++ or other) have this feature?

See getml/reflect-cpp#429 and reflect-cpp/include/rfl/Result.hpp

Include a code fragment with sample data that illustrates the use of this feature

Intead of the try { ... } catch(e) { ... } logic in getml/reflect-cpp#432, it could run something more like:

auto jsoncons_result = jsoncons::cbor::decode_cbor<jsoncons::json>(_bytes);
if( jsoncons_result ) {
  auto r = Reader();
  return Parser<T, Processors<Ps...>>::read(r, InputVarType{&jsoncons_result.value()});
} else {
  std::string msg("Could not parse CBOR: ");
  msg.append(jsoncons_result.error().what());
  return rfl::error(msg);
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions