Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance missing_key error message #1148

Open
pfirsich opened this issue Jul 14, 2024 · 2 comments
Open

Enhance missing_key error message #1148

pfirsich opened this issue Jul 14, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@pfirsich
Copy link

pfirsich commented Jul 14, 2024

Currently the error message for missing keys is simply "missing_key" and a location pointing to the end of the object. For some applications I would feel uncomfortable showing that error to a user that is supposed to manually edit JSON files.

Is there a way to add a more descriptive error that mentions the missing key? I know that the error context does not have much space for information like that. Maybe includer_error could be abused for this?

@stephenberry
Copy link
Owner

Thanks for pointing this out.

The plan is to make the glz::context more generic, which would allow custom error messages, and I'll also take missing keys into account. We should be able to report at least one missing key. I'd like to report all missing keys, but want to be careful about avoiding dynamic allocations in the error handling code.

@stephenberry
Copy link
Owner

I think we want to report all keys that are missing. This would be easiest to handle as a std::vector<std::string> of all missing keys. However, I don't generally want this within glz::context and affect performance even when error_on_missing_keys is false.

I'm thinking of having a static thread_local function that provides the missing keys. An idea for how it would be used is below:

auto ec = glz::read_json(obj, buffer);
if (ec == glz::error_code::missing_key) {
   const std::vector<std::string>& missing_keys = glz::missing_keys();
   for (auto& key : missing_keys) {
      // do something with the reported missing keys
   }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants