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

Issue #4561 - use diagnostic positions in exceptions #4583

Conversation

hnampally
Copy link
Contributor

@hnampally hnampally commented Jan 5, 2025

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.

  • Changes are described in the pull request, or an existing issue is referenced.
  • The test suite compiles and runs without error.
  • Code coverage is 100%. Test cases can be added by editing the test suite.
  • The source code is amalgamated; that is, after making changes to the sources in the include/nlohmann directory, run make amalgamate to create the single-header files single_include/nlohmann/json.hpp and single_include/nlohmann/json_fwd.hpp. The whole process is described here.

@hnampally hnampally requested a review from nlohmann as a code owner January 5, 2025 06:14
@hnampally hnampally changed the title Issue4561 use diagnostic positions in exceptions Issue #4561 use diagnostic positions in exceptions Jan 5, 2025
@hnampally hnampally changed the title Issue #4561 use diagnostic positions in exceptions Issue #4561 - use diagnostic positions in exceptions Jan 5, 2025
@coveralls
Copy link

coveralls commented Jan 5, 2025

Coverage Status

coverage: 99.607% (-0.03%) from 99.639%
when pulling 52f0703 on hnampally:issue4561-use-diagnostic-positions-in-exceptions
into 60c4875 on nlohmann:develop.

Copy link
Owner

@nlohmann nlohmann left a 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 #ifdefs to the other test files to accommodate for the different exception outputs.

docs/examples/diagnostic_positions.cpp Outdated Show resolved Hide resolved
docs/examples/diagnostics_extended.cpp Outdated Show resolved Hide resolved
include/nlohmann/detail/exceptions.hpp Outdated Show resolved Hide resolved
Signed-off-by: Harinath Nampally <[email protected]>
@github-actions github-actions bot added L tests and removed M labels Jan 6, 2025
Copy link

github-actions bot commented Jan 6, 2025

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated. @hnampally
Please read and follow the Contribution Guidelines.

@hnampally hnampally force-pushed the issue4561-use-diagnostic-positions-in-exceptions branch from 5aa9a0f to 777bae3 Compare January 6, 2025 01:17
@hnampally hnampally force-pushed the issue4561-use-diagnostic-positions-in-exceptions branch from fab4d58 to c1d35e3 Compare January 6, 2025 01:58
@@ -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";
}
}
Copy link
Owner

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

Copy link
Owner

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';
}
}
}
Copy link
Owner

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
Copy link
Owner

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
Copy link
Owner

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

Copy link
Contributor

@gregmarr gregmarr Jan 6, 2025

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)
Copy link
Owner

Choose a reason for hiding this comment

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

Due to

#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.

Copy link
Contributor Author

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))
Copy link
Contributor

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
}

Copy link
Contributor Author

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, ") ");
Copy link
Contributor

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());
Copy link
Contributor

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?

@hnampally
Copy link
Contributor Author

hnampally commented Jan 6, 2025

I am closing this PR and creating a new one.

@hnampally hnampally closed this Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants