Skip to content

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

Description

@santilin

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() ));

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions