Skip to content

Commit 62fc287

Browse files
authored
Better distinguish between invalid or missing schema linter titles (#634)
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent e520223 commit 62fc287

3 files changed

Lines changed: 24 additions & 10 deletions

File tree

src/linter/include/sourcemeta/blaze/linter_error.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ namespace sourcemeta::blaze {
1818
#pragma warning(disable : 4251 4275)
1919
#endif
2020

21+
/// @ingroup linter
22+
/// An error that represents a missing rule name
23+
class SOURCEMETA_BLAZE_LINTER_EXPORT LinterMissingNameError
24+
: public std::exception {
25+
public:
26+
[[nodiscard]] auto what() const noexcept -> const char * override {
27+
return "The schema rule is missing a title";
28+
}
29+
};
30+
2131
/// @ingroup linter
2232
/// An error that represents an invalid schema rule name. The name must
2333
/// consist only of lowercase ASCII letters, digits, underscores, or slashes.

src/linter/schema.cc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,15 @@ static auto extract_description(const sourcemeta::core::JSON &schema)
4747
}
4848

4949
static auto extract_title(const sourcemeta::core::JSON &schema) -> std::string {
50-
if (!schema.defines("title") || !schema.at("title").is_string()) {
51-
throw LinterInvalidNameError(
52-
"", "The schema rule title is missing or not a string");
50+
if (!schema.defines("title")) {
51+
throw LinterMissingNameError{};
52+
}
53+
54+
if (!schema.at("title").is_string()) {
55+
std::ostringstream result;
56+
sourcemeta::core::stringify(schema.at("title"), result);
57+
throw LinterInvalidNameError(std::move(result).str(),
58+
"The schema rule title is not a string");
5359
}
5460

5561
auto title{schema.at("title").to_string()};

test/linter/linter_schema_test.cc

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ TEST(Linter, schema_rule_missing_title_throws) {
289289
rule_schema, sourcemeta::core::schema_walker,
290290
sourcemeta::core::schema_resolver,
291291
sourcemeta::blaze::default_schema_compiler),
292-
sourcemeta::blaze::LinterInvalidNameError);
292+
sourcemeta::blaze::LinterMissingNameError);
293293
}
294294

295295
TEST(Linter, schema_rule_non_string_title_throws) {
@@ -406,7 +406,7 @@ TEST(Linter, schema_rule_invalid_name_error_preserves_name) {
406406
}
407407
}
408408

409-
TEST(Linter, schema_rule_missing_title_error_preserves_empty_name) {
409+
TEST(Linter, schema_rule_missing_title_error_message) {
410410
const auto rule_schema{sourcemeta::core::parse_json(R"JSON({
411411
"$schema": "https://json-schema.org/draft/2020-12/schema",
412412
"type": "object"
@@ -418,11 +418,9 @@ TEST(Linter, schema_rule_missing_title_error_preserves_empty_name) {
418418
rule_schema, sourcemeta::core::schema_walker,
419419
sourcemeta::core::schema_resolver,
420420
sourcemeta::blaze::default_schema_compiler);
421-
FAIL() << "Expected LinterInvalidNameError";
422-
} catch (const sourcemeta::blaze::LinterInvalidNameError &error) {
423-
EXPECT_EQ(error.identifier(), "");
424-
EXPECT_STREQ(error.what(),
425-
"The schema rule title is missing or not a string");
421+
FAIL() << "Expected LinterMissingNameError";
422+
} catch (const sourcemeta::blaze::LinterMissingNameError &error) {
423+
EXPECT_STREQ(error.what(), "The schema rule is missing a title");
426424
}
427425
}
428426

0 commit comments

Comments
 (0)