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

Show the path of the currently parsed value when an error occurs #835

Closed
santilin opened this issue Nov 20, 2017 · 1 comment
Closed

Show the path of the currently parsed value when an error occurs #835

santilin opened this issue Nov 20, 2017 · 1 comment

Comments

@santilin
Copy link

santilin commented Nov 20, 2017

Feature Request
It would be nice to get the path of the json file when an error occurs. Currently, only the type of the error is printed, with no way to know where in the json tree it happend, so it is very difficult to find the error.

For parse errors, I have implemented a way to show the place of the error:

	try {
		return json::parse(f);
	} catch( nlohmann::detail::parse_error &e ) {
		Xtring err = e.what();
		std::cout << err << std::endl;
		if (e.byte > 1 )
			f.seekg(e.byte - 2);
		else 
			f.seekg(0);
		char text[121];
		f.read(text, 120);
		std::cout << text << std::endl;
		exit(1);
	}
    }

And for type errors, I have tweaked the code for every JSON_THROW so that I get a dump of the value being parsed. But as I can not get the parent nor the name, only the value, I don't know where the error in the parse tree es:

        JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()) + j.dump() ));
@nlohmann
Copy link
Owner

  • I understand your usecase, but I am hesitant into serializing whole JSON values into exceptions.
  • About the parse errors: you perfectly showed that such a feature is possible without a library change, and following the “not pay for what you don't use” principle, I don't think that we should add this to the library - especially printing to an output stream.

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

No branches or pull requests

2 participants