Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <sourcemeta/core/json.h>
#include <sourcemeta/core/jsonpointer.h>
#include <sourcemeta/core/options.h>
#include <sourcemeta/core/yaml.h>

#include <concepts> // std::same_as, std::convertible_to
#include <cstdint> // std::uint64_t
Expand Down Expand Up @@ -1009,6 +1010,10 @@ inline auto try_catch(const sourcemeta::core::Options &options,
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_NOT_SUPPORTED;
} catch (const sourcemeta::core::YAMLFileParseError &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_OTHER_INPUT_ERROR;
} catch (const sourcemeta::core::JSONFileParseError &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
Expand Down
6 changes: 5 additions & 1 deletion src/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,11 @@ handle_json_entry(const std::filesystem::path &entry_path,
index, property_storage->back());
};
sourcemeta::core::JSON document{sourcemeta::core::JSON{nullptr}};
sourcemeta::core::parse_yaml(stream, document, callback);
try {
sourcemeta::core::parse_yaml(stream, document, callback);
} catch (const sourcemeta::core::YAMLParseError &error) {
throw sourcemeta::core::YAMLFileParseError{canonical, error};
}
documents.push_back({std::move(document), std::move(positions),
std::move(property_storage)});
line_offset += max_line > 0 ? max_line - 1 : 0;
Expand Down
6 changes: 5 additions & 1 deletion src/resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,11 @@ static inline auto http_fetch(const std::string &url,
sourcemeta::core::http_header_find(response.headers, "content-type")};
if (content_type.has_value() && sourcemeta::core::http_content_type_matches(
content_type.value(), "text/yaml")) {
return sourcemeta::core::parse_yaml(response.body);
try {
return sourcemeta::core::parse_yaml(response.body);
} catch (const sourcemeta::core::YAMLParseError &error) {
throw sourcemeta::core::YAMLFileParseError{url, error};
}
}

return sourcemeta::core::parse_json(response.body);
Expand Down
3 changes: 3 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ add_jsonschema_test_unix(inspect/pass_yaml)
add_jsonschema_test_unix(inspect/pass_json_output)
add_jsonschema_test_unix(inspect/fail_no_schema)
add_jsonschema_test_unix(inspect/fail_schema_invalid_json)
add_jsonschema_test_unix(inspect/fail_schema_invalid_yaml)
add_jsonschema_test_unix(inspect/fail_unknown_metaschema)
add_jsonschema_test_unix(inspect/fail_default_dialect_config_extension_mismatch)
add_jsonschema_test_unix(inspect/fail_relative_file_metaschema_ref)
Expand Down Expand Up @@ -640,6 +641,7 @@ add_jsonschema_test_unix(codegen/fail_no_schema)
add_jsonschema_test_unix(codegen/fail_missing_target)
add_jsonschema_test_unix(codegen/fail_invalid_target)
add_jsonschema_test_unix(codegen/fail_schema_invalid_json)
add_jsonschema_test_unix(codegen/fail_schema_invalid_yaml)
add_jsonschema_test_unix(codegen/fail_unknown_metaschema)
add_jsonschema_test_unix(codegen/fail_default_dialect_config_extension_mismatch)
add_jsonschema_test_unix(codegen/fail_unsupported_vocabulary)
Expand Down Expand Up @@ -783,6 +785,7 @@ add_jsonschema_test_unix(lint/fail_lint_rule_duplicate_builtin)
add_jsonschema_test_unix(lint/fail_lint_rule_duplicate_custom)
add_jsonschema_test_unix(lint/fail_lint_rule_invalid_json)
add_jsonschema_test_unix(lint/fail_lint_rule_unresolvable)
add_jsonschema_test_unix(lint/fail_lint_rule_invalid_yaml)
add_jsonschema_test_unix(lint/fail_lint_rule_property_names)
add_jsonschema_test_unix(lint/fail_lint_rule_property_names_json)
add_jsonschema_test_unix(lint/pass_lint_config_rule_no_violation)
Expand Down
45 changes: 45 additions & 0 deletions test/codegen/fail_schema_invalid_yaml.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/sh

set -o errexit
set -o nounset

TMP="$(mktemp -d)"
clean() { rm -rf "$TMP"; }
trap clean EXIT

cat << 'EOF' > "$TMP/schema.yaml"
type: string
---
type: integer
EOF

"$1" codegen "$TMP/schema.yaml" --target typescript 2> "$TMP/stderr.txt" \
&& EXIT_CODE="$?" || EXIT_CODE="$?"
# Other input error
test "$EXIT_CODE" = "6"

cat << EOF > "$TMP/expected.txt"
error: Unexpected content after document
at line 3
at column 1
at file path $(realpath "$TMP")/schema.yaml
EOF

diff "$TMP/stderr.txt" "$TMP/expected.txt"

# JSON error
"$1" codegen "$TMP/schema.yaml" --target typescript --json > "$TMP/stdout.txt" \
&& EXIT_CODE="$?" || EXIT_CODE="$?"
# Other input error
test "$EXIT_CODE" = "6"

cat << EOF > "$TMP/expected.txt"
{
"error": "Unexpected content after document",
"line": 3,
"column": 1,
"filePath": "$(realpath "$TMP")/schema.yaml"
}
EOF

diff "$TMP/stdout.txt" "$TMP/expected.txt"
45 changes: 45 additions & 0 deletions test/inspect/fail_schema_invalid_yaml.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/sh

set -o errexit
set -o nounset

TMP="$(mktemp -d)"
clean() { rm -rf "$TMP"; }
trap clean EXIT

cat << 'EOF' > "$TMP/schema.yaml"
type: string
---
type: integer
EOF

"$1" inspect "$TMP/schema.yaml" 2> "$TMP/stderr.txt" \
&& EXIT_CODE="$?" || EXIT_CODE="$?"
# Other input error
test "$EXIT_CODE" = "6"

cat << EOF > "$TMP/expected.txt"
error: Unexpected content after document
at line 3
at column 1
at file path $(realpath "$TMP")/schema.yaml
EOF

diff "$TMP/stderr.txt" "$TMP/expected.txt"

# JSON error
"$1" inspect "$TMP/schema.yaml" --json > "$TMP/stdout.txt" \
&& EXIT_CODE="$?" || EXIT_CODE="$?"
# Other input error
test "$EXIT_CODE" = "6"

cat << EOF > "$TMP/expected.txt"
{
"error": "Unexpected content after document",
"line": 3,
"column": 1,
"filePath": "$(realpath "$TMP")/schema.yaml"
}
EOF

diff "$TMP/stdout.txt" "$TMP/expected.txt"
51 changes: 51 additions & 0 deletions test/lint/fail_lint_rule_invalid_yaml.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/sh

set -o errexit
set -o nounset

TMP="$(mktemp -d)"
clean() { rm -rf "$TMP"; }
trap clean EXIT

cat << 'EOF' > "$TMP/rule.yaml"
title: require_type
---
title: another
EOF

cat << 'EOF' > "$TMP/schema.json"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "string"
}
EOF

"$1" lint --rule "$TMP/rule.yaml" "$TMP/schema.json" \
> "$TMP/output.txt" 2>&1 && EXIT_CODE="$?" || EXIT_CODE="$?"
# Other input error
test "$EXIT_CODE" = "6"

cat << EOF > "$TMP/expected.txt"
error: Unexpected content after document
at line 3
at column 1
at file path $(realpath "$TMP")/rule.yaml
EOF

diff "$TMP/output.txt" "$TMP/expected.txt"

"$1" lint --rule "$TMP/rule.yaml" --json "$TMP/schema.json" \
> "$TMP/output_json.txt" 2>&1 && EXIT_CODE="$?" || EXIT_CODE="$?"
# Other input error
test "$EXIT_CODE" = "6"

cat << EOF > "$TMP/expected_json.txt"
{
"error": "Unexpected content after document",
"line": 3,
"column": 1,
"filePath": "$(realpath "$TMP")/rule.yaml"
}
EOF

diff "$TMP/output_json.txt" "$TMP/expected_json.txt"
Loading