Skip to content

Commit

Permalink
[yaml] Update exception types to match propsed new style guide (Robot…
Browse files Browse the repository at this point in the history
  • Loading branch information
jwnimmer-tri authored Jul 12, 2021
1 parent 0b6ac77 commit 066f500
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 40 deletions.
4 changes: 2 additions & 2 deletions common/yaml/test/yaml_performance_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ class YamlPerformanceTest : public ::testing::Test {
static YAML::Node Load(const std::string& contents) {
const YAML::Node loaded = YAML::Load(contents);
if (loaded.Type() != YAML::NodeType::Map) {
throw std::runtime_error("Bad contents parse " + contents);
throw std::invalid_argument("Bad contents parse " + contents);
}
const YAML::Node doc = loaded["doc"];
if (doc.Type() != YAML::NodeType::Map) {
throw std::runtime_error("Bad doc parse " + contents);
throw std::invalid_argument("Bad doc parse " + contents);
}
return doc;
}
Expand Down
39 changes: 2 additions & 37 deletions common/yaml/test/yaml_read_archive_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ class YamlReadArchiveTest
static YAML::Node Load(const std::string& contents) {
const YAML::Node loaded = YAML::Load(contents);
if (loaded.Type() != YAML::NodeType::Map) {
throw std::runtime_error("Bad contents parse " + contents);
throw std::invalid_argument("Bad contents parse " + contents);
}
const YAML::Node doc = loaded["doc"];
if (doc.Type() != YAML::NodeType::Map) {
throw std::runtime_error("Bad doc parse " + contents);
throw std::invalid_argument("Bad doc parse " + contents);
}
return doc;
}
Expand Down Expand Up @@ -339,7 +339,6 @@ _template: &template 99.0
<< : *template
bar: 2.0
)""")),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{value\\}\\)"
" has invalid merge key type \\(Scalar\\) within entry"
" for std::map<[^ ]*> value\\.");
Expand All @@ -353,7 +352,6 @@ _template: &template
<< : *template
bar: 2.0
)""")),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{value\\}\\)"
" has invalid merge key type \\(Null\\) within entry"
" for std::map<[^ ]*> value\\.");
Expand Down Expand Up @@ -639,7 +637,6 @@ _template: &template 99.0
<< : *template
outer_value: 1.0
)""")),
std::runtime_error,
"YAML node of type Map"
" \\(with size 2 and keys \\{inner_struct, outer_value\\}\\)"
" has invalid merge key type \\(Scalar\\) within entry"
Expand All @@ -654,7 +651,6 @@ _template: &template
<< : *template
outer_value: 1.0
)""")),
std::runtime_error,
"YAML node of type Map"
" \\(with size 2 and keys \\{inner_struct, outer_value\\}\\)"
" has invalid merge key type \\(Null\\) within entry"
Expand All @@ -670,7 +666,6 @@ _template: &template
<< : *template
outer_value: 1.0
)""")),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{outer_value\\}\\)"
" has invalid merge key type \\(Sequence-of-non-Map\\) within entry"
" for <root>\\.");
Expand All @@ -693,7 +688,6 @@ TEST_P(YamlReadArchiveTest, VisitScalarFoundNothing) {
} else if (GetParam().allow_cpp_with_no_yaml) {
DRAKE_EXPECT_THROWS_MESSAGE(
AcceptIntoDummy<OuterStruct>(node),
std::runtime_error,
"YAML node of type Map"
" \\(with size 1 and keys \\{inner_value_TYPO\\}\\)"
" key inner_value_TYPO did not match any visited value entry for <root>"
Expand All @@ -703,7 +697,6 @@ TEST_P(YamlReadArchiveTest, VisitScalarFoundNothing) {
} else {
DRAKE_EXPECT_THROWS_MESSAGE(
AcceptIntoDummy<OuterStruct>(node),
std::runtime_error,
"YAML node of type Map"
" \\(with size 1 and keys \\{inner_value_TYPO\\}\\)"
" is missing entry for double inner_value"
Expand All @@ -722,7 +715,6 @@ TEST_P(YamlReadArchiveTest, VisitScalarFoundArray) {
inner_struct:
inner_value: [2.0, 3.0]
)""")),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{inner_value\\}\\)"
" has non-Scalar \\(Sequence\\) entry for double inner_value"
" while accepting YAML node of type Map"
Expand All @@ -740,7 +732,6 @@ TEST_P(YamlReadArchiveTest, VisitScalarFoundStruct) {
inner_value:
key: 2.0
)""")),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{inner_value\\}\\)"
" has non-Scalar \\(Map\\) entry for double inner_value"
" while accepting YAML node of type Map"
Expand All @@ -752,7 +743,6 @@ TEST_P(YamlReadArchiveTest, VisitScalarFoundStruct) {
TEST_P(YamlReadArchiveTest, VisitArrayFoundNothing) {
DRAKE_EXPECT_THROWS_MESSAGE(
AcceptIntoDummy<ArrayStruct>(LoadSingleValue("")),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{value\\}\\)"
" has non-Sequence \\(Null\\) entry for std::array<.*> value\\.");
}
Expand All @@ -761,7 +751,6 @@ TEST_P(YamlReadArchiveTest, VisitArrayFoundNothing) {
TEST_P(YamlReadArchiveTest, VisitArrayFoundScalar) {
DRAKE_EXPECT_THROWS_MESSAGE(
AcceptIntoDummy<ArrayStruct>(LoadSingleValue("1.0")),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{value\\}\\)"
" has non-Sequence \\(Scalar\\) entry for std::array<.*> value\\.");
}
Expand All @@ -774,7 +763,6 @@ TEST_P(YamlReadArchiveTest, VisitArrayFoundStruct) {
value:
inner_value: 1.0
)""")),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{value\\}\\)"
" has non-Sequence \\(Map\\) entry for std::array<.*> value\\.");
}
Expand All @@ -783,7 +771,6 @@ TEST_P(YamlReadArchiveTest, VisitArrayFoundStruct) {
TEST_P(YamlReadArchiveTest, VisitVectorFoundNothing) {
DRAKE_EXPECT_THROWS_MESSAGE(
AcceptIntoDummy<VectorStruct>(LoadSingleValue("")),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{value\\}\\)"
" has non-Sequence \\(Null\\) entry for std::vector<.*> value\\.");
}
Expand All @@ -792,7 +779,6 @@ TEST_P(YamlReadArchiveTest, VisitVectorFoundNothing) {
TEST_P(YamlReadArchiveTest, VisitVectorFoundScalar) {
DRAKE_EXPECT_THROWS_MESSAGE(
AcceptIntoDummy<VectorStruct>(LoadSingleValue("1.0")),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{value\\}\\)"
" has non-Sequence \\(Scalar\\) entry for std::vector<.*> value\\.");
}
Expand All @@ -805,7 +791,6 @@ TEST_P(YamlReadArchiveTest, VisitVectorFoundStruct) {
value:
inner_value: 1.0
)""")),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{value\\}\\)"
" has non-Sequence \\(Map\\) entry for std::vector<.*> value\\.");
}
Expand All @@ -814,7 +799,6 @@ TEST_P(YamlReadArchiveTest, VisitVectorFoundStruct) {
TEST_P(YamlReadArchiveTest, VisitOptionalScalarFoundSequence) {
DRAKE_EXPECT_THROWS_MESSAGE(
AcceptIntoDummy<OptionalStruct>(LoadSingleValue("[1.0]")),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{value\\}\\)"
" has non-Scalar \\(Sequence\\) entry for std::optional<double>"
" value\\.");
Expand All @@ -825,7 +809,6 @@ TEST_P(YamlReadArchiveTest, VisitVariantFoundNoTag) {
DRAKE_EXPECT_THROWS_MESSAGE(
AcceptIntoDummy<VariantWrappingStruct>(
Load("doc:\n inner:\n value:")),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{value\\}\\)"
" has non-Scalar \\(Null\\) entry for std::string value"
" while accepting YAML node of type Map \\(with size 1 and keys"
Expand All @@ -839,7 +822,6 @@ TEST_P(YamlReadArchiveTest, VisitVariantFoundNoTag) {
DRAKE_EXPECT_THROWS_MESSAGE(
AcceptIntoDummy<VariantWrappingStruct>(
Load("doc:\n inner:\n value: [foo, bar]")),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{value\\}\\)"
" has non-Scalar \\(Sequence\\) entry for std::string value"
" while accepting YAML node of type Map \\(with size 1 and keys"
Expand All @@ -848,7 +830,6 @@ TEST_P(YamlReadArchiveTest, VisitVariantFoundNoTag) {
DRAKE_EXPECT_THROWS_MESSAGE(
AcceptIntoDummy<VariantWrappingStruct>(
Load("doc:\n inner:\n value: {foo: bar}")),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{value\\}\\)"
" has non-Scalar \\(Map\\) entry for std::string value\\"
" while accepting YAML node of type Map \\(with size 1 and keys"
Expand All @@ -859,7 +840,6 @@ TEST_P(YamlReadArchiveTest, VisitVariantFoundNoTag) {
TEST_P(YamlReadArchiveTest, VisitVariantFoundUnknownTag) {
DRAKE_EXPECT_THROWS_MESSAGE(
AcceptIntoDummy<VariantStruct>(Load("doc:\n value: !UnknownTag foo")),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{value\\}\\) "
"has unsupported type tag !UnknownTag "
"while selecting a variant<> entry for "
Expand All @@ -872,22 +852,18 @@ TEST_P(YamlReadArchiveTest, VisitEigenFoundNothing) {
const std::string value;
DRAKE_EXPECT_THROWS_MESSAGE(
AcceptIntoDummy<EigenVecStruct>(LoadSingleValue(value)),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{value\\}\\)"
" has non-Sequence \\(Null\\) entry for Eigen::VectorXd value\\.");
DRAKE_EXPECT_THROWS_MESSAGE(
AcceptIntoDummy<EigenVec3Struct>(LoadSingleValue(value)),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{value\\}\\)"
" has non-Sequence \\(Null\\) entry for Eigen::Vector3d value\\.");
DRAKE_EXPECT_THROWS_MESSAGE(
AcceptIntoDummy<EigenMatrixStruct>(LoadSingleValue(value)),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{value\\}\\)"
" has non-Sequence \\(Null\\) entry for Eigen::MatrixXd value\\.");
DRAKE_EXPECT_THROWS_MESSAGE(
AcceptIntoDummy<EigenMatrix34Struct>(LoadSingleValue(value)),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{value\\}\\)"
" has non-Sequence \\(Null\\) entry for Eigen::Matrix.*3,4.* value\\.");
}
Expand All @@ -897,22 +873,18 @@ TEST_P(YamlReadArchiveTest, VisitEigenFoundScalar) {
const std::string value{"1.0"};
DRAKE_EXPECT_THROWS_MESSAGE(
AcceptIntoDummy<EigenVecStruct>(LoadSingleValue(value)),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{value\\}\\)"
" has non-Sequence \\(Scalar\\) entry for Eigen::VectorXd value\\.");
DRAKE_EXPECT_THROWS_MESSAGE(
AcceptIntoDummy<EigenVec3Struct>(LoadSingleValue(value)),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{value\\}\\)"
" has non-Sequence \\(Scalar\\) entry for Eigen::Vector3d value\\.");
DRAKE_EXPECT_THROWS_MESSAGE(
AcceptIntoDummy<EigenMatrixStruct>(LoadSingleValue(value)),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{value\\}\\)"
" has non-Sequence \\(Scalar\\) entry for Eigen::MatrixXd value\\.");
DRAKE_EXPECT_THROWS_MESSAGE(
AcceptIntoDummy<EigenMatrix34Struct>(LoadSingleValue(value)),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{value\\}\\)"
" has non-Sequence \\(Scalar\\) entry for Eigen::Matrix.* value\\.");
}
Expand All @@ -922,13 +894,11 @@ TEST_P(YamlReadArchiveTest, VisitEigenMatrixFoundOneDimensional) {
const std::string value{"[1.0, 2.0, 3.0, 4.0]"};
DRAKE_EXPECT_THROWS_MESSAGE(
AcceptIntoDummy<EigenMatrixStruct>(LoadSingleValue(value)),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{value\\}\\)"
" is Sequence-of-Scalar \\(not Sequence-of-Sequence\\)"
" entry for Eigen::MatrixXd value\\.");
DRAKE_EXPECT_THROWS_MESSAGE(
AcceptIntoDummy<EigenMatrix34Struct>(LoadSingleValue(value)),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{value\\}\\)"
" is Sequence-of-Scalar \\(not Sequence-of-Sequence\\)"
" entry for Eigen::Matrix.* value\\.");
Expand All @@ -945,12 +915,10 @@ TEST_P(YamlReadArchiveTest, VisitEigenMatrixFoundNonSquare) {
)""");
DRAKE_EXPECT_THROWS_MESSAGE(
AcceptIntoDummy<EigenMatrixStruct>(Load(doc)),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{value\\}\\)"
" has inconsistent cols dimensions entry for Eigen::MatrixXd value\\.");
DRAKE_EXPECT_THROWS_MESSAGE(
AcceptIntoDummy<EigenMatrix34Struct>(Load(doc)),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{value\\}\\)"
" has inconsistent cols dimensions entry for Eigen::Matrix.* value\\.");
}
Expand All @@ -968,7 +936,6 @@ TEST_P(YamlReadArchiveTest, VisitStructFoundNothing) {
} else {
DRAKE_EXPECT_THROWS_MESSAGE(
AcceptIntoDummy<OuterStruct>(node),
std::runtime_error,
"YAML node of type Map \\(with size 1 and keys \\{outer_value\\}\\)"
" is missing entry for [^ ]*InnerStruct inner_struct\\.");
}
Expand All @@ -982,7 +949,6 @@ TEST_P(YamlReadArchiveTest, VisitStructFoundScalar) {
outer_value: 1.0
inner_struct: 2.0
)""")),
std::runtime_error,
"YAML node of type Map"
" \\(with size 2 and keys \\{inner_struct, outer_value\\}\\)"
" has non-Map \\(Scalar\\) entry for [^ ]*InnerStruct inner_struct\\.");
Expand All @@ -996,7 +962,6 @@ TEST_P(YamlReadArchiveTest, VisitStructFoundArray) {
outer_value: 1.0
inner_struct: [2.0, 3.0]
)""")),
std::runtime_error,
"YAML node of type Map"
" \\(with size 2 and keys \\{inner_struct, outer_value\\}\\)"
" has non-Map \\(Sequence\\) entry for [^ ]*InnerStruct inner_struct\\.");
Expand Down
2 changes: 1 addition & 1 deletion common/yaml/yaml_write_archive.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ class YamlWriteArchive final {
|| (full_name == "int")) {
// TODO(jwnimmer-tri) Add support for well-known YAML primitive types
// within variants (when placed other than at the 0'th index).
throw std::runtime_error(fmt::format(
throw std::invalid_argument(fmt::format(
"Cannot YamlWriteArchive the variant type {} with a non-zero index",
full_name));
}
Expand Down

0 comments on commit 066f500

Please sign in to comment.