Skip to content
This repository was archived by the owner on Jun 17, 2022. It is now read-only.

Conversation

@esotericnonsense
Copy link

Required in order to be able to distinguish between keys which do not exist
and those which exist but point to null values.

If anyone has a better recommendation for which specific exception to throw here, or perhaps even a better way to do it, I'm all ears.

lib/univalue.cpp Outdated
}

if (throw_if_not_present) {
throw int(-1);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in general we want to throw exceptions derived from std::exception. Univalue seems to use runtime_exception for other cases, so this may be sufficient.

Required in order to be able to distinguish between keys which do not exist
    and those which exist but point to null values.
@esotericnonsense
Copy link
Author

Changed to inherit from std::exception, also see bitcoin/bitcoin#12435

@eklitzke
Copy link

Which methods present you with missing fields? I'm curious to know where you need this. In any event, I don't think this is right. In C++ try/catch blocks are cheap, but throwing an exception is very expensive, because it requires unwinding the stack. The way I would be something like this:

const UniValue& find_value(const UniValue& obj, const std::string& name, int *status=nullptr);

Then in the body if status is not null I'd set a flag if the value is missing (e.g. something like *status = MISSING).

@esotericnonsense
Copy link
Author

It's in the pull request on the bitcoin repo. In the JSON-RPC 2.0 spec if the 'id' field is not present in a request, then the request should be treated as a 'notification' and not responded to.

The default-argument null pointer thing is clever. I'll change it over to use that instead.

@Sjors
Copy link
Member

Sjors commented Dec 6, 2018

Needs rebase.

@promag
Copy link

promag commented May 3, 2019

PR title is misleading, please update. I think the status could just be a bool?

@fanquake
Copy link
Member

Closing this for now, as the downstream work is not continuing.

@fanquake fanquake closed this Oct 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants