-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use diagnostic positions in exceptions (#4585)
* add a ci step for Json_Diagnostic_Positions Signed-off-by: Harinath Nampally <harinath922@gmail.com> * Update ci.cmake to address review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * address review comment Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix typo in the comment Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix typos in ci.cmake Signed-off-by: Harinath Nampally <harinath922@gmail.com> * invoke the new ci step from ubuntu.yml Signed-off-by: Harinath Nampally <harinath922@gmail.com> * issue4561 - use diagnostic positions for exceptions Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_documentation check Signed-off-by: Harinath Nampally <harinath922@gmail.com> * address review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci check failures for unit-diagnostic-postions.cpp Signed-off-by: Harinath Nampally <harinath922@gmail.com> * improvements based on review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix const correctness string Signed-off-by: Harinath Nampally <harinath922@gmail.com> * further refinements based on reviews Signed-off-by: Harinath Nampally <harinath922@gmail.com> * add one more test case for full coverage Signed-off-by: Harinath Nampally <harinath922@gmail.com> * ci check fix - add const Signed-off-by: Harinath Nampally <harinath922@gmail.com> * add unit tests for json_diagnostic_postions only Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_diagnostics Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_build_documentation check Signed-off-by: Harinath Nampally <harinath922@gmail.com> --------- Signed-off-by: Harinath Nampally <harinath922@gmail.com>
- Loading branch information
Showing
13 changed files
with
261 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
docs/mkdocs/docs/examples/diagnostic_positions_exception.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <iostream> | ||
|
||
#define JSON_DIAGNOSTIC_POSITIONS 1 | ||
#include <nlohmann/json.hpp> | ||
|
||
using json = nlohmann::json; | ||
|
||
/* Demonstration of type error exception with diagnostic postions support enabled */ | ||
int main() | ||
{ | ||
//Invalid json string - housenumber type must be int instead of string | ||
const std::string json_invalid_string = R"( | ||
{ | ||
"address": { | ||
"street": "Fake Street", | ||
"housenumber": "1" | ||
} | ||
} | ||
)"; | ||
json j = json::parse(json_invalid_string); | ||
try | ||
{ | ||
int housenumber = j["address"]["housenumber"]; | ||
std::cout << housenumber; | ||
} | ||
catch (const json::exception& e) | ||
{ | ||
std::cout << e.what() << '\n'; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
docs/mkdocs/docs/examples/diagnostic_positions_exception.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[json.exception.type_error.302] (bytes 92-95) type must be number, but is string |
31 changes: 31 additions & 0 deletions
31
docs/mkdocs/docs/examples/diagnostics_extended_positions.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <iostream> | ||
|
||
#define JSON_DIAGNOSTICS 1 | ||
#define JSON_DIAGNOSTIC_POSITIONS 1 | ||
#include <nlohmann/json.hpp> | ||
|
||
using json = nlohmann::json; | ||
|
||
/* Demonstration of type error exception with diagnostic postions support enabled */ | ||
int main() | ||
{ | ||
//Invalid json string - housenumber type must be int instead of string | ||
const std::string json_invalid_string = R"( | ||
{ | ||
"address": { | ||
"street": "Fake Street", | ||
"housenumber": "1" | ||
} | ||
} | ||
)"; | ||
json j = json::parse(json_invalid_string); | ||
try | ||
{ | ||
int housenumber = j["address"]["housenumber"]; | ||
std::cout << housenumber; | ||
} | ||
catch (const json::exception& e) | ||
{ | ||
std::cout << e.what() << '\n'; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
docs/mkdocs/docs/examples/diagnostics_extended_positions.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[json.exception.type_error.302] (/address/housenumber) (bytes 92-95) type must be number, but is string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// __ _____ _____ _____ | ||
// __| | __| | | | JSON for Modern C++ (supporting code) | ||
// | | |__ | | | | | | version 3.11.3 | ||
// |_____|_____|_____|_|___| https://github.com/nlohmann/json | ||
// | ||
// SPDX-FileCopyrightText: 2013 - 2024 Niels Lohmann <https://nlohmann.me> | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include "doctest_compatibility.h" | ||
|
||
#ifdef JSON_DIAGNOSTICS | ||
#undef JSON_DIAGNOSTICS | ||
#endif | ||
|
||
#define JSON_DIAGNOSTICS 0 | ||
#define JSON_DIAGNOSTIC_POSITIONS 1 | ||
#include <nlohmann/json.hpp> | ||
|
||
using json = nlohmann::json; | ||
|
||
TEST_CASE("Better diagnostics with positions only") | ||
{ | ||
SECTION("invalid type") | ||
{ | ||
const std::string json_invalid_string = R"( | ||
{ | ||
"address": { | ||
"street": "Fake Street", | ||
"housenumber": "1" | ||
} | ||
} | ||
)"; | ||
json j = json::parse(json_invalid_string); | ||
CHECK_THROWS_WITH_AS(j.at("address").at("housenumber").get<int>(), | ||
"[json.exception.type_error.302] (bytes 108-111) type must be number, but is string", json::type_error); | ||
} | ||
|
||
SECTION("invalid type without positions") | ||
{ | ||
const json j = "foo"; | ||
CHECK_THROWS_WITH_AS(j.get<int>(), | ||
"[json.exception.type_error.302] type must be number, but is string", json::type_error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// __ _____ _____ _____ | ||
// __| | __| | | | JSON for Modern C++ (supporting code) | ||
// | | |__ | | | | | | version 3.11.3 | ||
// |_____|_____|_____|_|___| https://github.com/nlohmann/json | ||
// | ||
// SPDX-FileCopyrightText: 2013 - 2024 Niels Lohmann <https://nlohmann.me> | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include "doctest_compatibility.h" | ||
|
||
#define JSON_DIAGNOSTICS 1 | ||
#define JSON_DIAGNOSTIC_POSITIONS 1 | ||
#include <nlohmann/json.hpp> | ||
|
||
using json = nlohmann::json; | ||
|
||
TEST_CASE("Better diagnostics with positions") | ||
{ | ||
SECTION("invalid type") | ||
{ | ||
const std::string json_invalid_string = R"( | ||
{ | ||
"address": { | ||
"street": "Fake Street", | ||
"housenumber": "1" | ||
} | ||
} | ||
)"; | ||
json j = json::parse(json_invalid_string); | ||
CHECK_THROWS_WITH_AS(j.at("address").at("housenumber").get<int>(), | ||
"[json.exception.type_error.302] (/address/housenumber) (bytes 108-111) type must be number, but is string", json::type_error); | ||
} | ||
|
||
SECTION("invalid type without positions") | ||
{ | ||
const json j = "foo"; | ||
CHECK_THROWS_WITH_AS(j.get<int>(), | ||
"[json.exception.type_error.302] type must be number, but is string", json::type_error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters