-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Issue #4561 - use diagnostic positions in exceptions #4583
Issue #4561 - use diagnostic positions in exceptions #4583
Conversation
Signed-off-by: Harinath Nampally <[email protected]>
Signed-off-by: Harinath Nampally <[email protected]>
Signed-off-by: Harinath Nampally <[email protected]>
Signed-off-by: Harinath Nampally <[email protected]>
Signed-off-by: Harinath Nampally <[email protected]>
Signed-off-by: Harinath Nampally <[email protected]>
Signed-off-by: Harinath Nampally <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also check the failing test. Just as for JSON_DIAGNOSTICS
, you may need to add #ifdef
s to the other test files to accommodate for the different exception outputs.
Signed-off-by: Harinath Nampally <[email protected]>
🔴 Amalgamation check failed! 🔴The source code has not been amalgamated. @hnampally |
Signed-off-by: Harinath Nampally <[email protected]>
5aa9a0f
to
777bae3
Compare
Signed-off-by: Harinath Nampally <[email protected]>
fab4d58
to
c1d35e3
Compare
@@ -48,4 +48,4 @@ int main() | |||
std::cout << "1" << "\n"; | |||
std::cout << "Parsed string: \n"; | |||
std::cout << json_string.substr(j["address"]["housenumber"].start_pos(), j["address"]["housenumber"].end_pos() - j["address"]["housenumber"].start_pos()) << "\n\n"; | |||
} | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert this file.
@@ -47,4 +47,3 @@ Original string: | |||
1 | |||
Parsed string: | |||
1 | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert this file.
@@ -19,4 +19,4 @@ int main() | |||
{ | |||
std::cout << e.what() << '\n'; | |||
} | |||
} | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert this file.
@@ -1 +1 @@ | |||
[json.exception.type_error.302] (/address/housenumber) type must be number, but is string | |||
[json.exception.type_error.302] (/address/housenumber) type must be number, but is string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert this file.
str += ", byte " + std::to_string(leaf_element->start_pos()) + "-" + std::to_string(leaf_element->end_pos()); | ||
return concat('(', str, ") "); | ||
} | ||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic looks weird:
#if JSON_DIAGNOSTIC_POSITIONS
#endif
elif JSON_DIAGNOSTIC_POSITIONS
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, a line was lost, and it should still be between the #endif
and the #elif
:
return concat('(', str, ") ");
Actually, that should just be moved out of the if
.
@@ -60,14 +60,20 @@ TEST_CASE("JSON patch") | |||
json const doc2 = R"({ "q": { "bar": 2 } })"_json; | |||
|
|||
// because "a" does not exist. | |||
#if defined(JSON_DIAGNOSTIC_POSITIONS) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Due to
json/include/nlohmann/detail/abi_macros.hpp
Lines 29 to 31 in 60c4875
#ifndef JSON_DIAGNOSTIC_POSITIONS | |
#define JSON_DIAGNOSTIC_POSITIONS 0 | |
#endif |
JSON_DIAGNOSTIC_POSITIONS
will always be defined. Either to 0
or 1
. So checking for defined(JSON_DIAGNOSTIC_POSITIONS)
will always be true.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand, unfortunately my workspace got messed up and I was unable to test it thoroughly, I didn't intend to push these changes, anyway I will set up a fresh workspace, I wonder if its okay to close this PR and open a new PR with fresh branch?
@@ -131,7 +130,20 @@ class exception : public std::exception | |||
{ | |||
return concat(a, '/', detail::escape(b)); | |||
}); | |||
return concat('(', str, ") "); | |||
#if JSON_DIAGNOSTIC_POSITIONS | |||
if ((leaf_element->start_pos() != std::string::npos) && (leaf_element->end_pos() != std::string::npos)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line 134-139 appears to be similar to 140-146. Should we consider extracting it into a helper function for better code reusability? Like below..
template<typename BasicJsonType>
std::string get_byte_positions(const BasicJsonType* leaf_element)
{
#if JSON_DIAGNOSTIC_POSITIONS
if ((leaf_element->start_pos() != std::string::npos) && (leaf_element->end_pos() != std::string::npos))
{
return concat('(', "byte ", std::to_string(leaf_element->start_pos()), "-", std::to_string(leaf_element->end_pos()), ") ");
}
return "";
#else
static_cast<void>(leaf_element);
return "";
#endif
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the suggestion, yes I will do this, it looks like a good idea to me.
if ((leaf_element->start_pos() != std::string::npos) && (leaf_element->end_pos() != std::string::npos)) | ||
{ | ||
auto str = concat("byte ", std::to_string(leaf_element->start_pos()), "-", std::to_string(leaf_element->end_pos())); | ||
return concat('(', str, ") "); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't need two separate concat()
calls here.
#if JSON_DIAGNOSTIC_POSITIONS | ||
if ((leaf_element->start_pos() != std::string::npos) && (leaf_element->end_pos() != std::string::npos)) | ||
{ | ||
str += ", byte " + std::to_string(leaf_element->start_pos()) + "-" + std::to_string(leaf_element->end_pos()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should these be bytes
instead of byte
?
I am closing this PR and creating a new one. |
This PR is for #4561, updates the diagnostics function in the exceptions.hpp to use diagnostic positions in exceptions.
Pull request checklist
Read the Contribution Guidelines for detailed information.
include/nlohmann
directory, runmake amalgamate
to create the single-header filessingle_include/nlohmann/json.hpp
andsingle_include/nlohmann/json_fwd.hpp
. The whole process is described here.