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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
add_executable(jsonschema_cli
main.cc configure.h.in command.h
utils.h error.h logger.h configuration.h input.h resolver.h
utils.h error.h exit_code.h logger.h configuration.h input.h resolver.h
command_fmt.cc
command_inspect.cc
command_bundle.cc
Expand Down
2 changes: 2 additions & 0 deletions src/command_bundle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ auto sourcemeta::jsonschema::bundle(const sourcemeta::core::Options &options)
} catch (const sourcemeta::core::SchemaUnknownBaseDialectError &) {
throw FileError<sourcemeta::core::SchemaUnknownBaseDialectError>(
schema_path);
} catch (const sourcemeta::core::SchemaUnknownDialectError &) {
throw FileError<sourcemeta::core::SchemaUnknownDialectError>(schema_path);
} catch (const sourcemeta::core::SchemaError &error) {
throw FileError<sourcemeta::core::SchemaError>(schema_path, error.what());
}
Expand Down
2 changes: 2 additions & 0 deletions src/command_canonicalize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ auto sourcemeta::jsonschema::canonicalize(
} catch (const sourcemeta::core::SchemaUnknownBaseDialectError &) {
throw FileError<sourcemeta::core::SchemaUnknownBaseDialectError>(
schema_path);
} catch (const sourcemeta::core::SchemaUnknownDialectError &) {
throw FileError<sourcemeta::core::SchemaUnknownDialectError>(schema_path);
} catch (const sourcemeta::core::SchemaError &error) {
throw FileError<sourcemeta::core::SchemaError>(schema_path, error.what());
}
Expand Down
7 changes: 6 additions & 1 deletion src/command_codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ auto sourcemeta::jsonschema::codegen(const sourcemeta::core::Options &options)
}

if (!options.contains("target")) {
throw std::runtime_error{
throw OptionConflictError{
"You must pass a target using the `--target/-t` option"};
}

Expand Down Expand Up @@ -63,6 +63,8 @@ auto sourcemeta::jsonschema::codegen(const sourcemeta::core::Options &options)
} catch (const sourcemeta::core::SchemaUnknownBaseDialectError &) {
throw FileError<sourcemeta::core::SchemaUnknownBaseDialectError>(
schema_path);
} catch (const sourcemeta::core::SchemaUnknownDialectError &) {
throw FileError<sourcemeta::core::SchemaUnknownDialectError>(schema_path);
} catch (const sourcemeta::core::SchemaError &error) {
throw FileError<sourcemeta::core::SchemaError>(schema_path, error.what());
} catch (const sourcemeta::core::SchemaVocabularyError &error) {
Expand All @@ -76,6 +78,9 @@ auto sourcemeta::jsonschema::codegen(const sourcemeta::core::Options &options)
throw FileError<sourcemeta::codegen::UnsupportedKeywordValueError>(
schema_path, error.json(), error.pointer(),
std::string{error.keyword()}, error.what());
} catch (const sourcemeta::codegen::UnexpectedSchemaError &error) {
throw FileError<sourcemeta::codegen::UnexpectedSchemaError>(
schema_path, error.json(), error.pointer(), error.what());
}

std::ostringstream output;
Expand Down
12 changes: 10 additions & 2 deletions src/command_compile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,14 @@ auto sourcemeta::jsonschema::compile(const sourcemeta::core::Options &options)
frame.analyse(bundled, sourcemeta::core::schema_walker, custom_resolver,
dialect, schema_default_id);

const auto entrypoint_uri{resolve_entrypoint(
frame, std::string{options.at("entrypoint").front()})};
std::string entrypoint_uri;
try {
entrypoint_uri = resolve_entrypoint(
frame, std::string{options.at("entrypoint").front()});
} catch (const sourcemeta::blaze::CompilerInvalidEntryPoint &error) {
throw FileError<sourcemeta::blaze::CompilerInvalidEntryPoint>(
schema_path, error);
}

schema_template = sourcemeta::blaze::compile(
bundled, sourcemeta::core::schema_walker, custom_resolver,
Expand Down Expand Up @@ -91,6 +97,8 @@ auto sourcemeta::jsonschema::compile(const sourcemeta::core::Options &options)
} catch (const sourcemeta::core::SchemaUnknownBaseDialectError &) {
throw FileError<sourcemeta::core::SchemaUnknownBaseDialectError>(
schema_path);
} catch (const sourcemeta::core::SchemaUnknownDialectError &) {
throw FileError<sourcemeta::core::SchemaUnknownDialectError>(schema_path);
} catch (const sourcemeta::core::SchemaError &error) {
throw FileError<sourcemeta::core::SchemaError>(schema_path, error.what());
}
Expand Down
7 changes: 4 additions & 3 deletions src/command_fmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ auto sourcemeta::jsonschema::fmt(const sourcemeta::core::Options &options)
} catch (const sourcemeta::core::SchemaUnknownBaseDialectError &) {
throw FileError<sourcemeta::core::SchemaUnknownBaseDialectError>(
entry.resolution_base);
} catch (const sourcemeta::core::SchemaUnknownDialectError &) {
throw FileError<sourcemeta::core::SchemaUnknownDialectError>(
entry.resolution_base);
} catch (const sourcemeta::core::SchemaError &error) {
throw FileError<sourcemeta::core::SchemaError>(entry.resolution_base,
error.what());
Expand Down Expand Up @@ -118,8 +121,6 @@ auto sourcemeta::jsonschema::fmt(const sourcemeta::core::Options &options)
<< "\n";
}

// Report a different exit code for formatting check failures, to
// distinguish them from other errors
throw Fail{2};
throw Fail{EXIT_EXPECTED_FAILURE};
}
}
2 changes: 2 additions & 0 deletions src/command_inspect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ auto sourcemeta::jsonschema::inspect(const sourcemeta::core::Options &options)
} catch (const sourcemeta::core::SchemaUnknownBaseDialectError &) {
throw FileError<sourcemeta::core::SchemaUnknownBaseDialectError>(
schema_path);
} catch (const sourcemeta::core::SchemaUnknownDialectError &) {
throw FileError<sourcemeta::core::SchemaUnknownDialectError>(schema_path);
} catch (const sourcemeta::core::SchemaError &error) {
throw FileError<sourcemeta::core::SchemaError>(schema_path, error.what());
}
Expand Down
29 changes: 16 additions & 13 deletions src/command_install.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include <cassert> // assert
#include <cstdint> // std::uint8_t
#include <cstdlib> // EXIT_FAILURE
#include <filesystem> // std::filesystem
#include <fstream> // std::ofstream
#include <iostream> // std::cerr, std::cout
Expand Down Expand Up @@ -130,11 +129,13 @@ auto output_json(sourcemeta::core::JSON &events_array) -> void {

enum class OrphanedBehavior : std::uint8_t { Delete, Error };

auto make_on_event(const sourcemeta::core::Options &options, bool &had_error,
const bool is_json, sourcemeta::core::JSON &events_array,
auto make_on_event(const sourcemeta::core::Options &options,
int &error_exit_code, const bool is_json,
sourcemeta::core::JSON &events_array,
const OrphanedBehavior orphaned_behavior)
-> sourcemeta::blaze::Configuration::FetchEvent::Callback {
return [&options, &had_error, is_json, &events_array, orphaned_behavior](
return [&options, &error_exit_code, is_json, &events_array,
orphaned_behavior](
const sourcemeta::blaze::Configuration::FetchEvent &event)
-> bool {
using Type = sourcemeta::blaze::Configuration::FetchEvent::Type;
Expand Down Expand Up @@ -217,7 +218,7 @@ auto make_on_event(const sourcemeta::core::Options &options, bool &had_error,

break;
case Type::Untracked:
had_error = true;
error_exit_code = sourcemeta::jsonschema::EXIT_EXPECTED_FAILURE;
if (is_json) {
emit_json(events_array, "untracked", "uri", event.uri);
} else {
Expand All @@ -235,12 +236,14 @@ auto make_on_event(const sourcemeta::core::Options &options, bool &had_error,
if (orphaned_behavior == OrphanedBehavior::Delete) {
std::filesystem::remove(event.path);
} else {
had_error = true;
error_exit_code = sourcemeta::jsonschema::EXIT_EXPECTED_FAILURE;
}

break;
case Type::Error:
had_error = true;
error_exit_code = orphaned_behavior == OrphanedBehavior::Error
? sourcemeta::jsonschema::EXIT_EXPECTED_FAILURE
: sourcemeta::jsonschema::EXIT_OTHER_INPUT_ERROR;
if (is_json) {
auto json_event{sourcemeta::core::JSON::make_object()};
json_event.assign("type", sourcemeta::core::JSON{"error"});
Expand Down Expand Up @@ -433,10 +436,10 @@ auto sourcemeta::jsonschema::install(const sourcemeta::core::Options &options)
return dependency_resolve(options, configuration, identifier);
}};

bool had_error{false};
const auto on_event{make_on_event(options, had_error, is_json, events_array,
is_frozen ? OrphanedBehavior::Error
: OrphanedBehavior::Delete)};
int error_exit_code{0};
const auto on_event{make_on_event(
options, error_exit_code, is_json, events_array,
is_frozen ? OrphanedBehavior::Error : OrphanedBehavior::Delete)};

if (is_frozen) {
configuration.fetch(lock, fetcher, resolver, configuration_reader, writer,
Expand All @@ -454,8 +457,8 @@ auto sourcemeta::jsonschema::install(const sourcemeta::core::Options &options)
output_json(events_array);
}

if (had_error) {
throw Fail{EXIT_FAILURE};
if (error_exit_code != 0) {
throw Fail{error_exit_code};
}

if (!is_frozen) {
Expand Down
35 changes: 22 additions & 13 deletions src/command_lint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <sourcemeta/blaze/compiler.h>
#include <sourcemeta/blaze/linter.h>

#include <cstdlib> // EXIT_FAILURE
#include <cstdlib> // EXIT_SUCCESS
#include <filesystem> // std::filesystem::current_path
#include <fstream> // std::ofstream, std::ifstream
#include <iostream> // std::cerr, std::cout
Expand Down Expand Up @@ -174,6 +174,9 @@ static auto load_rule(sourcemeta::core::SchemaTransformer &bundle,
} catch (const sourcemeta::core::SchemaUnknownBaseDialectError &) {
throw sourcemeta::jsonschema::FileError<
sourcemeta::core::SchemaUnknownBaseDialectError>(rule_path);
} catch (const sourcemeta::core::SchemaUnknownDialectError &) {
throw sourcemeta::jsonschema::FileError<
sourcemeta::core::SchemaUnknownDialectError>(rule_path);
} catch (const sourcemeta::core::SchemaResolutionError &error) {
throw sourcemeta::jsonschema::FileError<
sourcemeta::core::SchemaResolutionError>(rule_path, error);
Expand Down Expand Up @@ -353,7 +356,7 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
}
scores.emplace_back(apply_result.second);
if (!apply_result.first) {
return 2;
return EXIT_EXPECTED_FAILURE;
}

return EXIT_SUCCESS;
Expand Down Expand Up @@ -407,6 +410,13 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)

throw FileError<sourcemeta::core::SchemaUnknownBaseDialectError>(
entry.resolution_base);
} catch (const sourcemeta::core::SchemaUnknownDialectError &) {
if (printed_progress) {
std::cerr << "\n";
}

throw FileError<sourcemeta::core::SchemaUnknownDialectError>(
entry.resolution_base);
} catch (const sourcemeta::core::SchemaResolutionError &error) {
if (printed_progress) {
std::cerr << "\n";
Expand All @@ -423,7 +433,8 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
}
});

if (wrapper_result == EXIT_SUCCESS || wrapper_result == 2) {
if (wrapper_result == EXIT_SUCCESS ||
wrapper_result == EXIT_EXPECTED_FAILURE) {
if (wrapper_result != EXIT_SUCCESS) {
result = false;
}
Expand Down Expand Up @@ -452,8 +463,7 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
output << "\n";
}
} else {
// Exception was caught - exit immediately with error code 1
throw Fail{EXIT_FAILURE};
throw Fail{wrapper_result};
}
}
} else {
Expand Down Expand Up @@ -482,7 +492,7 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
return EXIT_SUCCESS;
} else {
// Return 2 for logical lint failures
return 2;
return EXIT_EXPECTED_FAILURE;
}
} catch (
const sourcemeta::blaze::CompilerReferenceTargetNotSchemaError
Expand All @@ -499,18 +509,19 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
} catch (const sourcemeta::core::SchemaUnknownBaseDialectError &) {
throw FileError<sourcemeta::core::SchemaUnknownBaseDialectError>(
entry.resolution_base);
} catch (const sourcemeta::core::SchemaUnknownDialectError &) {
throw FileError<sourcemeta::core::SchemaUnknownDialectError>(
entry.resolution_base);
} catch (const sourcemeta::core::SchemaResolutionError &error) {
throw FileError<sourcemeta::core::SchemaResolutionError>(
entry.resolution_base, error);
}
});

if (wrapper_result == 2) {
// Logical lint failure
if (wrapper_result == EXIT_EXPECTED_FAILURE) {
result = false;
} else if (wrapper_result != EXIT_SUCCESS) {
// Exception was caught - exit immediately with error code 1
throw Fail{EXIT_FAILURE};
throw Fail{wrapper_result};
}
}
}
Expand Down Expand Up @@ -541,8 +552,6 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
}

if (!result) {
// Report a different exit code for linting failures, to
// distinguish them from other errors
throw Fail{2};
throw Fail{EXIT_EXPECTED_FAILURE};
}
}
7 changes: 4 additions & 3 deletions src/command_metaschema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,13 @@ auto sourcemeta::jsonschema::metaschema(
} catch (const sourcemeta::core::SchemaResolutionError &error) {
throw FileError<sourcemeta::core::SchemaResolutionError>(
entry.resolution_base, error);
} catch (const sourcemeta::core::SchemaUnknownDialectError &) {
throw FileError<sourcemeta::core::SchemaUnknownDialectError>(
entry.resolution_base);
}
}

if (!result) {
// Report a different exit code for validation failures, to
// distinguish them from other errors
throw Fail{2};
throw Fail{EXIT_EXPECTED_FAILURE};
}
}
13 changes: 10 additions & 3 deletions src/command_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <sourcemeta/core/json.h>

#include <chrono> // std::chrono
#include <cstdlib> // EXIT_FAILURE
#include <iostream> // std::cerr, std::cout
#include <sstream> // std::ostringstream
#include <string> // std::string
Expand Down Expand Up @@ -67,6 +66,12 @@ auto parse_test_suite(const sourcemeta::jsonschema::InputJSON &entry,
}
throw sourcemeta::jsonschema::FileError<
sourcemeta::core::SchemaUnknownBaseDialectError>{entry.resolution_base};
} catch (const sourcemeta::core::SchemaUnknownDialectError &) {
if (!json_output) {
std::cout << entry.first << ":\n";
}
throw sourcemeta::jsonschema::FileError<
sourcemeta::core::SchemaUnknownDialectError>{entry.resolution_base};
} catch (...) {
if (!json_output) {
std::cout << entry.first << ":\n";
Expand Down Expand Up @@ -157,7 +162,8 @@ auto report_as_text(const sourcemeta::core::Options &options) -> void {
}

if (!result) {
throw sourcemeta::jsonschema::Fail{2};
throw sourcemeta::jsonschema::Fail{
sourcemeta::jsonschema::EXIT_EXPECTED_FAILURE};
}
}

Expand Down Expand Up @@ -313,7 +319,8 @@ auto report_as_ctrf(const sourcemeta::core::Options &options) -> void {
std::cout << "\n";

if (!result) {
throw sourcemeta::jsonschema::Fail{2};
throw sourcemeta::jsonschema::Fail{
sourcemeta::jsonschema::EXIT_EXPECTED_FAILURE};
}
}

Expand Down
Loading