-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy patherror.h
More file actions
954 lines (842 loc) · 31.7 KB
/
Copy patherror.h
File metadata and controls
954 lines (842 loc) · 31.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
#ifndef SOURCEMETA_JSONSCHEMA_CLI_ERROR_H_
#define SOURCEMETA_JSONSCHEMA_CLI_ERROR_H_
#include <sourcemeta/blaze/configuration.h>
#include <sourcemeta/blaze/linter.h>
#include <sourcemeta/blaze/test.h>
#include <sourcemeta/codegen/ir.h>
#include <sourcemeta/core/error.h>
#include <sourcemeta/core/io.h>
#include <sourcemeta/core/json.h>
#include <sourcemeta/core/jsonpointer.h>
#include <sourcemeta/core/jsonschema.h>
#include <sourcemeta/core/options.h>
#include <filesystem> // std::filesystem
#include <functional> // std::function
#include <initializer_list> // std::initializer_list
#include <iostream> // std::cout, std::cerr
#include <stdexcept> // std::runtime_error
#include <string> // std::string
#include <system_error> // std::errc
#include <type_traits> // std::is_base_of_v
#include <vector> // std::vector
#include "exit_code.h"
namespace sourcemeta::jsonschema {
class PositionalArgumentError : public std::runtime_error {
public:
PositionalArgumentError(std::string message, std::string example)
: std::runtime_error{message}, example_{std::move(example)} {}
[[nodiscard]] auto example() const noexcept -> const std::string & {
return this->example_;
}
private:
std::string example_;
};
class InvalidOptionEnumerationValueError : public std::runtime_error {
public:
InvalidOptionEnumerationValueError(std::string message, std::string option,
std::initializer_list<std::string> values)
: std::runtime_error{message}, option_{std::move(option)},
values_{values} {}
[[nodiscard]] auto option() const noexcept -> const std::string & {
return this->option_;
}
[[nodiscard]] auto values() const noexcept
-> const std::vector<std::string> & {
return this->values_;
}
private:
std::string option_;
std::vector<std::string> values_;
};
class NotSchemaError : public std::runtime_error {
public:
NotSchemaError(std::filesystem::path path, std::string pointer = {})
: std::runtime_error{"The schema file you provided does not represent a "
"valid JSON "
"Schema"},
path_{std::move(path)}, pointer_{std::move(pointer)} {}
[[nodiscard]] auto path() const noexcept -> const std::filesystem::path & {
return this->path_;
}
[[nodiscard]] auto pointer() const noexcept -> const std::string & {
return this->pointer_;
}
private:
std::filesystem::path path_;
std::string pointer_;
};
class PathResolutionError : public std::runtime_error {
public:
PathResolutionError(std::filesystem::path path, std::string pointer)
: std::runtime_error{"The JSON Pointer does not resolve to a value in "
"the document"},
path_{std::move(path)}, pointer_{std::move(pointer)} {}
[[nodiscard]] auto path() const noexcept -> const std::filesystem::path & {
return this->path_;
}
[[nodiscard]] auto pointer() const noexcept -> const std::string & {
return this->pointer_;
}
private:
std::filesystem::path path_;
std::string pointer_;
};
class PathSchemaReferenceError : public std::runtime_error {
public:
PathSchemaReferenceError(std::filesystem::path path, std::string identifier,
sourcemeta::core::Pointer location,
std::string pointer, std::string message)
: std::runtime_error{std::move(message)}, path_{std::move(path)},
identifier_{std::move(identifier)}, location_{std::move(location)},
pointer_{std::move(pointer)} {}
[[nodiscard]] auto path() const noexcept -> const std::filesystem::path & {
return this->path_;
}
[[nodiscard]] auto identifier() const noexcept -> const std::string & {
return this->identifier_;
}
[[nodiscard]] auto location() const noexcept
-> const sourcemeta::core::Pointer & {
return this->location_;
}
[[nodiscard]] auto pointer() const noexcept -> const std::string & {
return this->pointer_;
}
private:
std::filesystem::path path_;
std::string identifier_;
sourcemeta::core::Pointer location_;
std::string pointer_;
};
class YAMLInputError : public std::runtime_error {
public:
YAMLInputError(std::string message, std::filesystem::path path)
: std::runtime_error{std::move(message)}, path_{std::move(path)} {}
[[nodiscard]] auto path() const noexcept -> const std::filesystem::path & {
return this->path_;
}
private:
std::filesystem::path path_;
};
class OptionConflictError : public std::runtime_error {
public:
OptionConflictError(std::string message)
: std::runtime_error{std::move(message)} {}
};
class StdinError : public std::runtime_error {
public:
explicit StdinError(std::string message)
: std::runtime_error{std::move(message)} {}
};
class InvalidLintRuleError : public std::runtime_error {
public:
InvalidLintRuleError(std::string message, std::string rule)
: std::runtime_error{std::move(message)}, rule_{std::move(rule)} {}
[[nodiscard]] auto rule() const noexcept -> const std::string & {
return this->rule_;
}
private:
std::string rule_;
};
class DuplicateLintRuleError : public std::runtime_error {
public:
DuplicateLintRuleError(std::string rule)
: std::runtime_error{"A lint rule with this name already exists"},
rule_{std::move(rule)} {}
[[nodiscard]] auto rule() const noexcept -> const std::string & {
return this->rule_;
}
private:
std::string rule_;
};
class InvalidIncludeIdentifier : public std::runtime_error {
public:
InvalidIncludeIdentifier(std::string identifier)
: std::runtime_error{"The include identifier is not a valid C/C++ "
"identifier"},
identifier_{std::move(identifier)} {}
[[nodiscard]] auto identifier() const noexcept -> const std::string & {
return this->identifier_;
}
private:
std::string identifier_;
};
class LintAutoFixError : public std::runtime_error {
public:
LintAutoFixError(std::string message, std::filesystem::path path,
sourcemeta::core::Pointer location)
: std::runtime_error{std::move(message)}, path_{std::move(path)},
location_{std::move(location)} {}
[[nodiscard]] auto path() const noexcept -> const std::filesystem::path & {
return this->path_;
}
[[nodiscard]] auto location() const noexcept
-> const sourcemeta::core::Pointer & {
return this->location_;
}
private:
std::filesystem::path path_;
sourcemeta::core::Pointer location_;
};
class UnknownCommandError : public std::runtime_error {
public:
UnknownCommandError(std::string command)
: std::runtime_error{"Unknown command"}, command_{std::move(command)} {}
[[nodiscard]] auto command() const noexcept -> const std::string & {
return this->command_;
}
private:
std::string command_;
};
class ConfigurationNotFoundError : public std::runtime_error {
public:
ConfigurationNotFoundError(std::filesystem::path path)
: std::runtime_error{"Could not find a jsonschema.json configuration "
"file"},
path_{std::move(path)} {}
[[nodiscard]] auto path() const noexcept -> const std::filesystem::path & {
return this->path_;
}
private:
std::filesystem::path path_;
};
class LockNotFoundError : public std::runtime_error {
public:
LockNotFoundError(std::filesystem::path path)
: std::runtime_error{"Lock file not found"}, path_{std::move(path)} {}
[[nodiscard]] auto path() const noexcept -> const std::filesystem::path & {
return this->path_;
}
private:
std::filesystem::path path_;
};
class LockParseError : public std::runtime_error {
public:
LockParseError(std::filesystem::path path)
: std::runtime_error{"Lock file is corrupted"}, path_{std::move(path)} {}
[[nodiscard]] auto path() const noexcept -> const std::filesystem::path & {
return this->path_;
}
private:
std::filesystem::path path_;
};
class InstallError : public std::runtime_error {
public:
InstallError(std::string message, std::string uri)
: std::runtime_error{std::move(message)}, uri_{std::move(uri)} {}
[[nodiscard]] auto uri() const noexcept -> const std::string & {
return this->uri_;
}
private:
std::string uri_;
};
class ConfigurationResolveFileNotFoundError : public std::runtime_error {
public:
ConfigurationResolveFileNotFoundError(
std::filesystem::path configuration_path,
sourcemeta::core::Pointer location, std::filesystem::path resolve_path,
std::uint64_t line, std::uint64_t column)
: std::runtime_error{"The resolve target does not exist on the "
"filesystem"},
configuration_path_{std::move(configuration_path)},
location_{std::move(location)}, resolve_path_{std::move(resolve_path)},
line_{line}, column_{column} {}
[[nodiscard]] auto path() const noexcept -> const std::filesystem::path & {
return this->configuration_path_;
}
[[nodiscard]] auto location() const noexcept
-> const sourcemeta::core::Pointer & {
return this->location_;
}
[[nodiscard]] auto resolve_path() const noexcept
-> const std::filesystem::path & {
return this->resolve_path_;
}
[[nodiscard]] auto line() const noexcept -> std::uint64_t {
return this->line_;
}
[[nodiscard]] auto column() const noexcept -> std::uint64_t {
return this->column_;
}
private:
std::filesystem::path configuration_path_;
sourcemeta::core::Pointer location_;
std::filesystem::path resolve_path_;
std::uint64_t line_;
std::uint64_t column_;
};
class Fail : public std::runtime_error {
public:
Fail(int exit_code) : std::runtime_error{"Fail"}, exit_code_{exit_code} {}
[[nodiscard]] auto exit_code() const noexcept -> int {
return this->exit_code_;
}
private:
int exit_code_;
};
inline auto stdin_path() -> std::filesystem::path {
#ifdef _WIN32
return std::filesystem::path{"<stdin>"};
#else
return std::filesystem::path{"/dev/stdin"};
#endif
}
inline auto stdin_path_string(const std::filesystem::path &p) -> std::string {
#ifdef _WIN32
if (p.string() == "<stdin>") {
return "<stdin>";
}
#else
if (p == std::filesystem::path{"/dev/stdin"}) {
return "/dev/stdin";
}
#endif
return sourcemeta::core::weakly_canonical(p).string();
}
template <typename Exception>
inline auto print_exception(const bool is_json, const Exception &exception)
-> void {
auto error_json{sourcemeta::core::JSON::make_object()};
if constexpr (std::is_base_of_v<std::filesystem::filesystem_error,
Exception>) {
if (exception.code() == std::errc::no_such_file_or_directory) {
if (is_json) {
error_json.assign("error",
sourcemeta::core::JSON{exception.code().message()});
} else {
std::cerr << "error: " << exception.code().message() << "\n";
}
} else if (exception.code() == std::errc::is_a_directory) {
if (is_json) {
error_json.assign(
"error",
sourcemeta::core::JSON{
"The input was supposed to be a file but it is a directory"});
} else {
std::cerr << "error: The input was supposed to be a file but it is a "
"directory\n";
}
} else {
if (is_json) {
error_json.assign("error", sourcemeta::core::JSON{exception.what()});
} else {
std::cerr << "error: " << exception.what() << "\n";
}
}
} else {
if (is_json) {
error_json.assign("error", sourcemeta::core::JSON{exception.what()});
} else {
std::cerr << "error: " << exception.what() << "\n";
}
}
if constexpr (requires(const Exception ¤t) { current.identifier(); }) {
if (is_json) {
error_json.assign("identifier",
sourcemeta::core::JSON{exception.identifier()});
} else {
std::cerr << " at identifier " << exception.identifier() << "\n";
}
}
if constexpr (requires(const Exception ¤t) { current.value(); }) {
if (is_json) {
error_json.assign("value", sourcemeta::core::JSON{exception.value()});
} else {
std::cerr << " at value " << exception.value() << "\n";
}
}
if constexpr (requires(const Exception ¤t) {
{
current.keyword()
} -> std::convertible_to<std::string_view>;
}) {
if (is_json) {
error_json.assign(
"keyword", sourcemeta::core::JSON{std::string{exception.keyword()}});
} else {
std::cerr << " at keyword " << exception.keyword() << "\n";
}
}
if constexpr (requires(const Exception ¤t) {
{
current.resolve_path()
} -> std::convertible_to<std::filesystem::path>;
}) {
const auto &resolve_path_value{exception.resolve_path()};
if (is_json) {
error_json.assign("resolvePath",
sourcemeta::core::JSON{resolve_path_value.string()});
} else {
std::cerr << " at resolve path " << resolve_path_value.string() << "\n";
}
}
if constexpr (requires(const Exception ¤t) { current.line(); }) {
if (is_json) {
error_json.assign("line", sourcemeta::core::JSON{static_cast<std::size_t>(
exception.line())});
} else {
std::cerr << " at line " << exception.line() << "\n";
}
}
if constexpr (requires(const Exception ¤t) { current.column(); }) {
if (is_json) {
error_json.assign(
"column",
sourcemeta::core::JSON{static_cast<std::size_t>(exception.column())});
} else {
std::cerr << " at column " << exception.column() << "\n";
}
}
if constexpr (requires(const Exception ¤t) { current.regex(); }) {
if (is_json) {
error_json.assign("regex", sourcemeta::core::JSON{exception.regex()});
} else {
std::cerr << " at regex " << exception.regex() << "\n";
}
}
if constexpr (requires(const Exception ¤t) {
{
current.path()
} -> std::convertible_to<std::filesystem::path>;
}) {
const auto &error_path{exception.path()};
const auto error_path_string{stdin_path_string(error_path)};
if (is_json) {
error_json.assign("filePath", sourcemeta::core::JSON{error_path_string});
} else {
std::cerr << " at file path " << error_path_string << "\n";
}
} else if constexpr (requires(const Exception ¤t) {
{
current.path1()
} -> std::convertible_to<std::filesystem::path>;
}) {
if (is_json) {
error_json.assign(
"filePath",
sourcemeta::core::JSON{
sourcemeta::core::weakly_canonical(exception.path1()).string()});
} else {
std::cerr
<< " at file path "
<< sourcemeta::core::weakly_canonical(exception.path1()).string()
<< "\n";
}
}
if constexpr (requires(const Exception ¤t) {
{ current.pointer() } -> std::convertible_to<std::string>;
}) {
if (!exception.pointer().empty()) {
if (is_json) {
error_json.assign("pointer",
sourcemeta::core::JSON{exception.pointer()});
} else {
std::cerr << " at path " << exception.pointer() << "\n";
}
}
}
if constexpr (requires(const Exception ¤t) { current.location(); }) {
if (is_json) {
error_json.assign("location",
sourcemeta::core::JSON{
sourcemeta::core::to_string(exception.location())});
} else {
std::cerr << " at location \""
<< sourcemeta::core::to_string(exception.location()) << "\"\n";
}
}
if constexpr (requires(const Exception ¤t) {
current.base().recompose();
}) {
if (is_json) {
error_json.assign("baseURI",
sourcemeta::core::JSON{exception.base().recompose()});
} else {
std::cerr << " at base uri " << exception.base().recompose() << "\n";
}
}
if constexpr (requires(const Exception ¤t) { current.uri(); }) {
if (is_json) {
error_json.assign("uri", sourcemeta::core::JSON{exception.uri()});
} else {
std::cerr << " at uri " << exception.uri() << "\n";
}
}
if constexpr (requires(const Exception ¤t) {
{ current.rule() } -> std::convertible_to<std::string>;
}) {
if (is_json) {
error_json.assign("rule", sourcemeta::core::JSON{exception.rule()});
} else {
std::cerr << " at rule " << exception.rule() << "\n";
}
}
if constexpr (requires(const Exception ¤t) {
{ current.command() } -> std::convertible_to<std::string>;
}) {
if (is_json) {
error_json.assign("command", sourcemeta::core::JSON{exception.command()});
} else {
std::cerr << " at command " << exception.command() << "\n";
}
}
if constexpr (requires(const Exception ¤t) {
{ current.option() } -> std::convertible_to<std::string_view>;
}) {
if (is_json) {
error_json.assign(
"option", sourcemeta::core::JSON{std::string{exception.option()}});
} else {
std::cerr << " at option " << exception.option() << "\n";
}
}
if constexpr (requires(const Exception ¤t) {
{
current.values()
} -> std::convertible_to<const std::vector<std::string> &>;
}) {
if (is_json) {
auto values_array{sourcemeta::core::JSON::make_array()};
for (const auto &value : exception.values()) {
values_array.push_back(sourcemeta::core::JSON{value});
}
error_json.assign("values", std::move(values_array));
} else {
std::cerr << " with values\n";
for (const auto &value : exception.values()) {
std::cerr << " - " << value << "\n";
}
}
}
if (is_json) {
sourcemeta::core::prettify(error_json, std::cout);
std::cout << "\n";
}
}
inline auto try_catch(const sourcemeta::core::Options &options,
const std::function<int()> &callback) noexcept -> int {
try {
return callback();
} catch (const Fail &error) {
return error.exit_code();
} catch (const InstallError &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_OTHER_INPUT_ERROR;
} catch (const ConfigurationNotFoundError &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
if (!is_json) {
std::cerr << "\nLearn more here: "
"https://github.com/sourcemeta/jsonschema/blob/main/"
"docs/install.markdown\n";
}
return EXIT_OTHER_INPUT_ERROR;
} catch (const LockNotFoundError &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_OTHER_INPUT_ERROR;
} catch (const LockParseError &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_OTHER_INPUT_ERROR;
} catch (const PathResolutionError &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_INVALID_CLI_ARGUMENTS;
} catch (const NotSchemaError &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_SCHEMA_INPUT_ERROR;
} catch (const YAMLInputError &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_NOT_SUPPORTED;
} catch (const InvalidLintRuleError &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_INVALID_CLI_ARGUMENTS;
} catch (const sourcemeta::core::FileError<DuplicateLintRuleError> &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_OTHER_INPUT_ERROR;
} catch (const sourcemeta::core::FileError<
sourcemeta::blaze::LinterInvalidNameError> &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_OTHER_INPUT_ERROR;
} catch (const sourcemeta::core::FileError<
sourcemeta::blaze::LinterInvalidNamePatternError> &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_OTHER_INPUT_ERROR;
} catch (const sourcemeta::core::FileError<
sourcemeta::blaze::LinterMissingNameError> &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_OTHER_INPUT_ERROR;
} catch (const InvalidIncludeIdentifier &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_INVALID_CLI_ARGUMENTS;
} catch (const LintAutoFixError &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
if (!is_json) {
std::cerr << "\n";
std::cerr << "This is an unexpected error, as making the auto-fix "
"functionality work in all\n";
std::cerr << "cases is tricky. We are working hard to improve the "
"auto-fixing functionality\n";
std::cerr << "to handle all possible edge cases, but for now, try again "
"without `--fix/-f`\n";
std::cerr << "and apply the suggestions by hand.\n\n";
std::cerr << "Also consider consider reporting this problematic case to "
"the issue tracker,\n";
std::cerr << "so we can add it to the test suite and fix it:\n\n";
std::cerr << "https://github.com/sourcemeta/jsonschema/issues\n";
}
return EXIT_UNEXPECTED_ERROR;
} catch (const sourcemeta::core::FileError<sourcemeta::blaze::TestParseError>
&error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
if (!is_json) {
std::cerr << "\n";
std::cerr << "Learn more here: "
"https://github.com/sourcemeta/jsonschema/blob/main/"
"docs/test.markdown\n";
}
return EXIT_OTHER_INPUT_ERROR;
} catch (const sourcemeta::core::FileError<
sourcemeta::blaze::CompilerReferenceTargetNotSchemaError> &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
if (!is_json) {
std::cerr << "\n";
if (!error.location().empty() && error.location().back().is_property() &&
error.location().back().to_property() == "$defs") {
std::cerr << "Maybe you meant to use `definitions` instead of `$defs` "
"in this dialect?\n";
} else {
std::cerr << "Are you sure the reported location is a valid JSON "
"Schema keyword in this dialect?\n";
}
}
return EXIT_SCHEMA_INPUT_ERROR;
} catch (const sourcemeta::core::FileError<
sourcemeta::blaze::CompilerInvalidEntryPoint> &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
if (!is_json) {
std::cerr
<< "\nUse the `inspect` command to find valid schema locations\n";
}
return EXIT_SCHEMA_INPUT_ERROR;
} catch (const sourcemeta::core::FileError<
sourcemeta::blaze::CompilerInvalidRegexError> &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
if (!is_json) {
std::cerr << "\nDetailed regex error messages are not yet supported\n"
"Try tools like https://regex101.com to debug further\n";
}
return EXIT_SCHEMA_INPUT_ERROR;
} catch (const PathSchemaReferenceError &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_SCHEMA_INPUT_ERROR;
} catch (
const sourcemeta::core::FileError<sourcemeta::core::SchemaReferenceError>
&error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_SCHEMA_INPUT_ERROR;
} catch (const ConfigurationResolveFileNotFoundError &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_OTHER_INPUT_ERROR;
} catch (const sourcemeta::core::FileError<
sourcemeta::blaze::ConfigurationParseError> &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_OTHER_INPUT_ERROR;
} catch (const sourcemeta::core::FileError<
sourcemeta::core::SchemaRelativeMetaschemaResolutionError> &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_SCHEMA_INPUT_ERROR;
} catch (
const sourcemeta::core::FileError<sourcemeta::core::SchemaResolutionError>
&error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
if (!is_json) {
if (error.identifier().starts_with("file://")) {
std::cerr << "\nThis is likely because the file does not exist\n";
} else {
std::cerr
<< "\nThis is likely because you forgot to import such schema "
"using `--resolve/-r`\n";
}
}
return EXIT_SCHEMA_INPUT_ERROR;
} catch (const sourcemeta::core::FileError<
sourcemeta::core::SchemaUnknownBaseDialectError> &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
if (!is_json) {
std::cerr << "\nAre you sure the input is a valid JSON Schema and its "
"base dialect is known?\n";
std::cerr
<< "If the input does not declare the `$schema` keyword, you might "
"want to\n";
std::cerr << "explicitly declare a default dialect using "
"`--default-dialect/-d`\n";
}
return EXIT_SCHEMA_INPUT_ERROR;
} catch (const sourcemeta::core::FileError<
sourcemeta::core::SchemaUnknownDialectError> &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
if (!is_json) {
std::cerr << "\nAre you sure the input is a valid JSON Schema and its "
"dialect is known?\n";
std::cerr
<< "If the input does not declare the `$schema` keyword, you might "
"want to\n";
std::cerr << "explicitly declare a default dialect using "
"`--default-dialect/-d`\n";
}
return EXIT_SCHEMA_INPUT_ERROR;
} catch (
const sourcemeta::core::FileError<sourcemeta::core::SchemaKeywordError>
&error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
if (!is_json) {
std::cerr << "\nAre you sure the input is a valid JSON Schema and it is "
"valid according to its meta-schema?\n";
}
return EXIT_SCHEMA_INPUT_ERROR;
} catch (const sourcemeta::core::FileError<sourcemeta::core::SchemaFrameError>
&error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_SCHEMA_INPUT_ERROR;
} catch (const sourcemeta::core::FileError<
sourcemeta::core::SchemaReferenceObjectResourceError> &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_SCHEMA_INPUT_ERROR;
} catch (
const sourcemeta::core::FileError<sourcemeta::core::SchemaError> &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_SCHEMA_INPUT_ERROR;
} catch (
const sourcemeta::core::FileError<sourcemeta::core::SchemaVocabularyError>
&error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_SCHEMA_INPUT_ERROR;
} catch (const sourcemeta::core::FileError<
sourcemeta::codegen::UnsupportedKeywordError> &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_NOT_SUPPORTED;
} catch (const sourcemeta::core::FileError<
sourcemeta::codegen::UnsupportedKeywordValueError> &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_NOT_SUPPORTED;
} catch (const sourcemeta::core::FileError<
sourcemeta::codegen::UnexpectedSchemaError> &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_NOT_SUPPORTED;
} catch (const sourcemeta::core::JSONFileParseError &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_OTHER_INPUT_ERROR;
} catch (const sourcemeta::core::JSONParseError &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_OTHER_INPUT_ERROR;
// Command line parsing handling
} catch (const StdinError &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_INVALID_CLI_ARGUMENTS;
} catch (const OptionConflictError &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_INVALID_CLI_ARGUMENTS;
} catch (const InvalidOptionEnumerationValueError &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
if (!is_json) {
std::cerr << "\nRun the `help` command for usage information\n";
}
return EXIT_INVALID_CLI_ARGUMENTS;
} catch (const PositionalArgumentError &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
if (!is_json) {
std::cerr << "\nFor example: " << error.example() << "\n";
}
return EXIT_INVALID_CLI_ARGUMENTS;
} catch (const UnknownCommandError &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
if (!is_json) {
std::cerr << "\nRun the `help` command for usage information\n";
}
return EXIT_INVALID_CLI_ARGUMENTS;
} catch (const sourcemeta::core::OptionsUnexpectedValueFlagError &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
if (!is_json) {
std::cerr << "\nRun the `help` command for usage information\n";
}
return EXIT_INVALID_CLI_ARGUMENTS;
} catch (const sourcemeta::core::OptionsMissingOptionValueError &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
if (!is_json) {
std::cerr << "\nRun the `help` command for usage information\n";
}
return EXIT_INVALID_CLI_ARGUMENTS;
} catch (const sourcemeta::core::OptionsUnknownOptionError &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
if (!is_json) {
std::cerr << "\nRun the `help` command for usage information\n";
}
return EXIT_INVALID_CLI_ARGUMENTS;
// Standard library handlers
} catch (const std::filesystem::filesystem_error &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_OTHER_INPUT_ERROR;
} catch (const std::runtime_error &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_UNEXPECTED_ERROR;
} catch (const std::exception &error) {
const auto is_json{options.contains("json")};
if (is_json) {
auto error_json{sourcemeta::core::JSON::make_object()};
error_json.assign("error", sourcemeta::core::JSON{error.what()});
sourcemeta::core::prettify(error_json, std::cout);
std::cout << "\n";
} else {
std::cerr << "unexpected error: " << error.what()
<< "\nPlease report it at "
<< "https://github.com/sourcemeta/jsonschema\n";
}
return EXIT_UNEXPECTED_ERROR;
}
}
} // namespace sourcemeta::jsonschema
#endif