Skip to content

Commit 4b2a006

Browse files
authored
Merge pull request #1303 from nlohmann/feature/binary_errors
Improve diagnostic messages for binary formats
2 parents dd67293 + dbb0b63 commit 4b2a006

7 files changed

Lines changed: 364 additions & 282 deletions

File tree

include/nlohmann/detail/input/binary_reader.hpp

Lines changed: 128 additions & 87 deletions
Large diffs are not rendered by default.

include/nlohmann/detail/output/binary_writer.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ class binary_writer
767767
}
768768
else
769769
{
770-
JSON_THROW(out_of_range::create(407, "number overflow serializing " + std::to_string(n)));
770+
JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(n) + " cannot be represented by UBJSON as it does not fit int64"));
771771
}
772772
}
773773

@@ -821,7 +821,7 @@ class binary_writer
821821
// LCOV_EXCL_START
822822
else
823823
{
824-
JSON_THROW(out_of_range::create(407, "number overflow serializing " + std::to_string(n)));
824+
JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(n) + " cannot be represented by UBJSON as it does not fit int64"));
825825
}
826826
// LCOV_EXCL_STOP
827827
}

single_include/nlohmann/json.hpp

Lines changed: 130 additions & 89 deletions
Large diffs are not rendered by default.

test/src/unit-cbor.cpp

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -831,14 +831,14 @@ TEST_CASE("CBOR")
831831
{
832832
CHECK_THROWS_AS(json::from_cbor(std::vector<uint8_t>({0xf9})), json::parse_error&);
833833
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0xf9})),
834-
"[json.exception.parse_error.110] parse error at byte 2: unexpected end of input");
834+
"[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR number: unexpected end of input");
835835
CHECK(json::from_cbor(std::vector<uint8_t>({0xf9}), true, false).is_discarded());
836836
}
837837
SECTION("only one byte follows")
838838
{
839839
CHECK_THROWS_AS(json::from_cbor(std::vector<uint8_t>({0xf9, 0x7c})), json::parse_error&);
840840
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0xf9, 0x7c})),
841-
"[json.exception.parse_error.110] parse error at byte 3: unexpected end of input");
841+
"[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR number: unexpected end of input");
842842
CHECK(json::from_cbor(std::vector<uint8_t>({0xf9, 0x7c}), true, false).is_discarded());
843843
}
844844
}
@@ -1314,7 +1314,7 @@ TEST_CASE("CBOR")
13141314
{
13151315
CHECK_THROWS_AS(json::from_cbor(std::vector<uint8_t>()), json::parse_error&);
13161316
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>()),
1317-
"[json.exception.parse_error.110] parse error at byte 1: unexpected end of input");
1317+
"[json.exception.parse_error.110] parse error at byte 1: syntax error while parsing CBOR value: unexpected end of input");
13181318
CHECK(json::from_cbor(std::vector<uint8_t>(), true, false).is_discarded());
13191319
}
13201320

@@ -1346,53 +1346,53 @@ TEST_CASE("CBOR")
13461346
CHECK_THROWS_AS(json::from_cbor(std::vector<uint8_t>({0xBF, 0x61, 0X61})), json::parse_error&);
13471347

13481348
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0x18})),
1349-
"[json.exception.parse_error.110] parse error at byte 2: unexpected end of input");
1349+
"[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR number: unexpected end of input");
13501350
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0x19})),
1351-
"[json.exception.parse_error.110] parse error at byte 2: unexpected end of input");
1351+
"[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR number: unexpected end of input");
13521352
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0x19, 0x00})),
1353-
"[json.exception.parse_error.110] parse error at byte 3: unexpected end of input");
1353+
"[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR number: unexpected end of input");
13541354
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0x1a})),
1355-
"[json.exception.parse_error.110] parse error at byte 2: unexpected end of input");
1355+
"[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR number: unexpected end of input");
13561356
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0x1a, 0x00})),
1357-
"[json.exception.parse_error.110] parse error at byte 3: unexpected end of input");
1357+
"[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR number: unexpected end of input");
13581358
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0x1a, 0x00, 0x00})),
1359-
"[json.exception.parse_error.110] parse error at byte 4: unexpected end of input");
1359+
"[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing CBOR number: unexpected end of input");
13601360
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0x1a, 0x00, 0x00, 0x00})),
1361-
"[json.exception.parse_error.110] parse error at byte 5: unexpected end of input");
1361+
"[json.exception.parse_error.110] parse error at byte 5: syntax error while parsing CBOR number: unexpected end of input");
13621362
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0x1b})),
1363-
"[json.exception.parse_error.110] parse error at byte 2: unexpected end of input");
1363+
"[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR number: unexpected end of input");
13641364
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0x1b, 0x00})),
1365-
"[json.exception.parse_error.110] parse error at byte 3: unexpected end of input");
1365+
"[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR number: unexpected end of input");
13661366
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00})),
1367-
"[json.exception.parse_error.110] parse error at byte 4: unexpected end of input");
1367+
"[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing CBOR number: unexpected end of input");
13681368
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00})),
1369-
"[json.exception.parse_error.110] parse error at byte 5: unexpected end of input");
1369+
"[json.exception.parse_error.110] parse error at byte 5: syntax error while parsing CBOR number: unexpected end of input");
13701370
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00})),
1371-
"[json.exception.parse_error.110] parse error at byte 6: unexpected end of input");
1371+
"[json.exception.parse_error.110] parse error at byte 6: syntax error while parsing CBOR number: unexpected end of input");
13721372
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00})),
1373-
"[json.exception.parse_error.110] parse error at byte 7: unexpected end of input");
1373+
"[json.exception.parse_error.110] parse error at byte 7: syntax error while parsing CBOR number: unexpected end of input");
13741374
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})),
1375-
"[json.exception.parse_error.110] parse error at byte 8: unexpected end of input");
1375+
"[json.exception.parse_error.110] parse error at byte 8: syntax error while parsing CBOR number: unexpected end of input");
13761376
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})),
1377-
"[json.exception.parse_error.110] parse error at byte 9: unexpected end of input");
1377+
"[json.exception.parse_error.110] parse error at byte 9: syntax error while parsing CBOR number: unexpected end of input");
13781378
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0x62})),
1379-
"[json.exception.parse_error.110] parse error at byte 2: unexpected end of input");
1379+
"[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR string: unexpected end of input");
13801380
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0x62, 0x60})),
1381-
"[json.exception.parse_error.110] parse error at byte 3: unexpected end of input");
1381+
"[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR string: unexpected end of input");
13821382
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0x7F})),
1383-
"[json.exception.parse_error.110] parse error at byte 2: unexpected end of input");
1383+
"[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR string: unexpected end of input");
13841384
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0x7F, 0x60})),
1385-
"[json.exception.parse_error.110] parse error at byte 3: unexpected end of input");
1385+
"[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR string: unexpected end of input");
13861386
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0x82, 0x01})),
1387-
"[json.exception.parse_error.110] parse error at byte 3: unexpected end of input");
1387+
"[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR value: unexpected end of input");
13881388
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0x9F, 0x01})),
1389-
"[json.exception.parse_error.110] parse error at byte 3: unexpected end of input");
1389+
"[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR value: unexpected end of input");
13901390
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0xBF, 0x61, 0x61, 0xF5})),
1391-
"[json.exception.parse_error.110] parse error at byte 5: unexpected end of input");
1391+
"[json.exception.parse_error.110] parse error at byte 5: syntax error while parsing CBOR string: unexpected end of input");
13921392
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0xA1, 0x61, 0x61})),
1393-
"[json.exception.parse_error.110] parse error at byte 4: unexpected end of input");
1393+
"[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing CBOR value: unexpected end of input");
13941394
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0xBF, 0x61, 0x61})),
1395-
"[json.exception.parse_error.110] parse error at byte 4: unexpected end of input");
1395+
"[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing CBOR value: unexpected end of input");
13961396

13971397
CHECK(json::from_cbor(std::vector<uint8_t>({0x18}), true, false).is_discarded());
13981398
CHECK(json::from_cbor(std::vector<uint8_t>({0x19}), true, false).is_discarded());
@@ -1426,12 +1426,12 @@ TEST_CASE("CBOR")
14261426
{
14271427
CHECK_THROWS_AS(json::from_cbor(std::vector<uint8_t>({0x1c})), json::parse_error&);
14281428
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0x1c})),
1429-
"[json.exception.parse_error.112] parse error at byte 1: error reading CBOR; last byte: 0x1C");
1429+
"[json.exception.parse_error.112] parse error at byte 1: syntax error while parsing CBOR value: invalid byte: 0x1C");
14301430
CHECK(json::from_cbor(std::vector<uint8_t>({0x1c}), true, false).is_discarded());
14311431

14321432
CHECK_THROWS_AS(json::from_cbor(std::vector<uint8_t>({0xf8})), json::parse_error&);
14331433
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0xf8})),
1434-
"[json.exception.parse_error.112] parse error at byte 1: error reading CBOR; last byte: 0xF8");
1434+
"[json.exception.parse_error.112] parse error at byte 1: syntax error while parsing CBOR value: invalid byte: 0xF8");
14351435
CHECK(json::from_cbor(std::vector<uint8_t>({0xf8}), true, false).is_discarded());
14361436
}
14371437

@@ -1491,7 +1491,7 @@ TEST_CASE("CBOR")
14911491
{
14921492
CHECK_THROWS_AS(json::from_cbor(std::vector<uint8_t>({0xa1, 0xff, 0x01})), json::parse_error&);
14931493
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0xa1, 0xff, 0x01})),
1494-
"[json.exception.parse_error.113] parse error at byte 2: expected a CBOR string; last byte: 0xFF");
1494+
"[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing CBOR string: expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0xFF");
14951495
CHECK(json::from_cbor(std::vector<uint8_t>({0xa1, 0xff, 0x01}), true, false).is_discarded());
14961496
}
14971497

@@ -1509,7 +1509,7 @@ TEST_CASE("CBOR")
15091509
{
15101510
CHECK_THROWS_AS(json::from_cbor(vec), json::parse_error&);
15111511
CHECK_THROWS_WITH(json::from_cbor(vec),
1512-
"[json.exception.parse_error.110] parse error at byte 2: expected end of input");
1512+
"[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR value: expected end of input; last byte: 0xF6");
15131513
CHECK(json::from_cbor(vec, true, false).is_discarded());
15141514
}
15151515
}

0 commit comments

Comments
 (0)