Skip to content

Feature request: Allow exceptionless usage #26

@omartijn

Description

@omartijn

Since using c++20 and coroutines, I'm running more and more into issues because you cannot await a coroutine in a catch-clause. For my code I've now created a wrapper to deal with this, it catches the exception and returns an expected which will have an error on failure:

template <typename Type, unsigned Rapidjson_Parseflags = rapidjson::kParseDefaultFlags>
std::expected<Type, std::string> try_from_json(std::string_view json) noexcept
{
	try
	{
		return json_dto::from_json<Type, Rapidjson_Parseflags>(json_dto::make_string_ref(json.data(), json.size()));
	}
	catch (const json_dto::ex_t& e)
	{
		return tl::make_unexpected(std::string{ e.what() });
	}
}

plus a whole lot of other overloads, of course. It'd be nice to have support for something like this directly inside json_dto. I realize that std::expected is only introduced in c++23 (which would require bumping the c++ version), but we could potentially only do this if compiling with a recent enough c++-version, use a third-party library (something like tl::expected), or implement our own (like we do with json_dto::Optional)

Any ideas on this? I wouldn't mind doing the work if it's something you'd be interested in merging.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions