/
Exception.pm
962 lines (840 loc) Β· 27.4 KB
/
Exception.pm
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
955
956
957
958
959
960
961
962
my class Failure { ... }
my role X::Comp { ... }
my class X::ControlFlow { ... }
my class Exception {
has $!ex;
method backtrace() { Backtrace.new(self) }
multi method Str(Exception:D:) {
self.?message.Str // 'Something went wrong'
}
multi method gist(Exception:D:) {
my $str = try self.?message;
return "Error while creating error string: $!" if $!;
$str ~= "\n";
try $str ~= self.backtrace;
return "$str\nError while creating backtrace: $!.message()\n$!.backtrace.full();" if $!;
return $str;
}
method throw() is hidden_from_backtrace {
nqp::bindattr(self, Exception, '$!ex', pir::new__Ps('Exception'))
unless nqp::defined($!ex);
pir::setattribute__vPsP($!ex, 'payload', nqp::p6decont(self));
my $msg := self.?message;
pir::setattribute__0PsP($!ex, 'message', nqp::unbox_s($msg.Str))
if $msg.defined;
nqp::throw($!ex)
}
method rethrow() is hidden_from_backtrace {
pir::setattribute__vPsP($!ex, 'payload', nqp::p6decont(self));
nqp::rethrow($!ex)
}
method resumable() {
nqp::p6bool(nqp::istrue(nqp::atkey($!ex, 'resume')));
}
method resume() {
my Mu $resume := nqp::atkey($!ex, 'resume');
if $resume {
$resume();
}
else {
die "Exception is not resumable";
}
}
method fail(Exception:D:) {
try self.throw;
my $fail := Failure.new($!);
my Mu $return := pir::find_caller_lex__Ps('RETURN');
$return($fail) unless nqp::isnull($return);
$fail
}
}
my class X::AdHoc is Exception {
has $.payload;
method message() { $.payload.Str }
method Numeric() { $.payload.Numeric }
}
my class X::Method::NotFound is Exception {
has $.method;
has $.typename;
has Bool $.private = False;
method message() {
$.private
?? "No such private method '$.method' for invocant of type '$.typename'"
!! "No such method '$.method' for invocant of type '$.typename'";
}
}
my class X::Method::InvalidQualifier is Exception {
has $.method;
has $.invocant;
has $.qualifier-type;
method message() {
"Cannot dispatch to method $.method on {$.qualifier-type.^name} "
~ "because it is not inherited or done by {$.invocant.^name}";
}
}
sub EXCEPTION(|) {
my Mu $parrot_ex := nqp::shift(pir::perl6_current_args_rpa__P());
my Mu $payload := nqp::atkey($parrot_ex, 'payload');
if nqp::p6bool(nqp::istype($payload, Exception)) {
nqp::bindattr($payload, Exception, '$!ex', $parrot_ex);
$payload;
} else {
my int $type = nqp::atkey_i($parrot_ex, 'type');
my $ex;
if $type == pir::const::EXCEPTION_METHOD_NOT_FOUND &&
nqp::p6box_s(nqp::atkey_s($parrot_ex, 'message'))
~~ /"Method '" (.+?) "' not found for invocant of class '" (.+)\'$/ {
$ex := X::Method::NotFound.new(
method => ~$0,
typename => ~$1,
);
}
else {
$ex := nqp::create(X::AdHoc);
nqp::bindattr($ex, X::AdHoc, '$!payload', nqp::p6box_s(nqp::atkey_s($parrot_ex, 'message')));
}
nqp::bindattr($ex, Exception, '$!ex', $parrot_ex);
$ex;
}
}
my class X::Comp::AdHoc { ... }
sub COMP_EXCEPTION(|) {
my Mu $parrot_ex := nqp::shift(pir::perl6_current_args_rpa__P());
my Mu $payload := nqp::atkey($parrot_ex, 'payload');
if nqp::p6bool(nqp::istype($payload, Exception)) {
nqp::bindattr($payload, Exception, '$!ex', $parrot_ex);
$payload;
} else {
my $ex := nqp::create(X::Comp::AdHoc);
nqp::bindattr($ex, Exception, '$!ex', $parrot_ex);
nqp::bindattr($ex, X::AdHoc, '$!payload', nqp::p6box_s(nqp::atkey_s($parrot_ex, 'message')));
$ex;
}
}
do {
sub is_runtime($bt) {
for $bt.keys {
try {
return True if nqp::iseq_s($bt[$_]<sub>, 'eval')
&& nqp::iseq_s(
nqp::join(';', $bt[$_]<sub>.get_namespace.get_name),
'nqp;HLL;Compiler'
);
return False if nqp::iseq_s($bt[$_]<sub>, 'compile')
&& nqp::iseq_s(
nqp::join(';', $bt[$_]<sub>.get_namespace.get_name),
'nqp;HLL;Compiler'
);
}
}
return False;
}
sub print_exception(|) is hidden_from_backtrace {
my Mu $ex := nqp::atpos(pir::perl6_current_args_rpa__P(), 0);
try {
my $e := EXCEPTION($ex);
my Mu $err := pir::getstderr__P();
if X::Comp.ACCEPTS($e) || is_runtime($ex.backtrace) {
$err.print: $e.gist;
$err.print: "\n";
}
else {
$err.print: "===SORRY!===\n";
$err.print: $ex;
$err.print: "\n";
}
$_() for pir::perl6ize_type__PP(@*END_PHASERS);
}
if $! {
pir::perl6_based_rethrow__0PP(nqp::getattr($!, Exception, '$!ex'), $ex);
}
}
sub print_control(|) is hidden_from_backtrace {
my Mu $ex := nqp::atpos(pir::perl6_current_args_rpa__P(), 0);
my int $type = nqp::atkey_i($ex, 'type');
if ($type == pir::const::CONTROL_OK) {
my Mu $err := pir::getstderr__P();
my $msg = nqp::p6box_s(nqp::atkey_s($ex, 'message'));
$err.print: $msg ?? "$msg" !! "Warning";
$err.print: Backtrace.new($ex.backtrace, 0).nice(:oneline);
$err.print: "\n";
my $resume := nqp::atkey($ex, 'resume');
if ($resume) {
$resume();
}
}
if ($type == pir::const::CONTROL_RETURN) {
die("stray return control exception");
}
if ($type == pir::const::CONTROL_LOOP_LAST) {
X::ControlFlow.new(illegal => 'last', enclosing => 'loop construct').throw;
}
if ($type == pir::const::CONTROL_LOOP_NEXT) {
X::ControlFlow.new(illegal => 'next', enclosing => 'loop construct').throw;
}
if ($type == pir::const::CONTROL_LOOP_REDO) {
X::ControlFlow.new(illegal => 'redo', enclosing => 'loop construct').throw;
}
if ($type == pir::const::CONTROL_CONTINUE) {
X::ControlFlow.new(illegal => 'proceed', enclosing => 'when clause').throw;
}
if ($type == pir::const::CONTROL_BREAK) {
# XXX: should work like leave() ?
X::ControlFlow.new(illegal => 'succeed', enclosing => 'when clause').throw;
}
if ($type == pir::const::CONTROL_TAKE) {
X::ControlFlow.new(illegal => 'take', enclosing => 'gather').throw;
}
}
my Mu $comp := pir::compreg__Ps('perl6');
$comp.HOW.add_method($comp, 'handle-exception',
method (|) {
my Mu $ex := nqp::atpos(pir::perl6_current_args_rpa__P(), 1);
pir::perl6_invoke_catchhandler__vPP(&print_exception, $ex);
nqp::exit(1);
0;
}
);
$comp.HOW.add_method($comp, 'handle-control',
method (|) {
my Mu $ex := nqp::atpos(pir::perl6_current_args_rpa__P(), 1);
pir::perl6_invoke_catchhandler__vPP(&print_control, $ex);
nqp::rethrow($ex);
}
);
}
my role X::OS {
has $.os-error;
}
my role X::IO does X::OS { };
my class X::IO::Rename does X::IO is Exception {
has $.from;
has $.to;
method message() {
"Failed to rename '$.from' to '$.to': $.os-error"
}
}
my class X::IO::Copy does X::IO is Exception {
has $.from;
has $.to;
method message() {
"Failed to copy '$.from' to '$.to': $.os-error"
}
}
my class X::IO::Mkdir does X::IO is Exception {
has $.path;
has $.mode;
method message() {
"Failed to create directory '$.path' with mode '0o{$.mode.fmt("%03o")}': $.os-error"
}
}
my class X::IO::Chdir does X::IO is Exception {
has $.path;
method message() {
"Failed to change the working directory to '$.path': $.os-error"
}
}
my class X::IO::Dir does X::IO is Exception {
has $.path;
method message() {
"Failed to get the directory contents of '$.path': $.os-error"
}
}
my class X::IO::Cwd does X::IO is Exception {
method message() {
"Failed to get the working directory: $.os-error"
}
}
my class X::IO::Rmdir does X::IO is Exception {
has $.path;
method message() {
"Failed to remove the directory '$.path': $.os-error"
}
}
my class X::IO::Unlink does X::IO is Exception {
has $.path;
method message() {
"Failed to remove the file '$.path': $.os-error"
}
}
my class X::IO::Chmod does X::IO is Exception {
has $.path;
has $.mode;
method message() {
"Failed to set the mode of '$.path' to '0o{$.mode.fmt("%03o")}': $.os-error"
}
}
my role X::Comp is Exception {
has $.filename;
has $.line;
has $.column;
has @.modules;
multi method gist(::?CLASS:D:) {
my $r = "===SORRY!===\n$.message\nat $.filename():$.line";
for @.modules.reverse[1..*] {
$r ~= $_<module>.defined
?? "\n from module $_<module> ($_<filename>:$_<line>)"
!! "\n from $_<filename>:$_<line>";
}
$r;
}
method SET_FILE_LINE($file, $line) {
$!filename = $file;
$!line = $line;
}
}
# XXX a hack for getting line numbers from exceptions from the metamodel
my class X::Comp::AdHoc is X::AdHoc does X::Comp { }
my role X::Syntax does X::Comp { }
my role X::Pod { }
my class X::NYI is Exception {
has $.feature;
method message() { "$.feature is not yet implemented. Sorry. " }
}
my class X::Comp::NYI is X::NYI does X::Comp { };
my class X::OutOfRange is Exception {
has $.what = 'Argument';
has $.got = '<unknown>';
has $.range = '<unknown>';
has $.comment;
method message() {
$.comment.defined
?? "$.what out of range. Is: $.got, should be in $.range.gist(); $.comment"
!! "$.what out of range. Is: $.got, should be in $.range.gist()"
}
}
my class X::Buf::AsStr is Exception {
has $.method;
method message() {
"Cannot use a Buf as a string, but you called the $.method method on it";
}
}
my class X::Buf::Pack is Exception {
has $.directive;
method message() {
"Unrecognized directive '$.directive'";
}
}
my class X::Buf::Pack::NonASCII is Exception {
has $.char;
method message() {
"non-ASCII character '$.char' while processing an 'A' template in pack";
}
}
my class X::Signature::Placeholder does X::Comp {
has $.placeholder;
method message() {
"Placeholder variable '$.placeholder' cannot override existing signature";
}
}
my class X::Placeholder::Block does X::Comp {
has $.placeholder;
method message() {
"Placeholder variable $.placeholder may not be used here because the surrounding block takes no signature";
}
}
my class X::Placeholder::Mainline is X::Placeholder::Block {
method message() {
"Cannot use placeholder parameter $.placeholder in the mainline"
}
}
my class X::Undeclared does X::Comp {
has $.what = 'Variable';
has $.symbol;
method message() {
"$.what $.symbol is not declared";
}
}
my class X::Attribute::Undeclared is X::Undeclared {
has $.package-kind;
has $.package-name;
method message() {
"Attribute $.symbol not declared in $.package-kind $.package-name";
}
}
my class X::Redeclaration does X::Comp {
has $.symbol;
has $.postfix = '';
has $.what = 'symbol';
method message() {
"Redeclaration of $.what $.symbol$.postfix";
}
}
my class X::Phaser::Multiple does X::Comp {
has $.block;
method message() { "Only one $.block block is allowed" }
}
my class X::Obsolete does X::Comp {
has $.old;
has $.replacement; # can't call it $.new, collides with constructor
has $.when = 'in Perl 6';
method message() { "Unsupported use of $.old; $.when please use $.replacement" }
}
my class X::Parameter::Default does X::Comp {
has $.how;
has $.parameter;
method message() {
$.parameter
?? "Cannot put default on $.how parameter $.parameter"
!! "Cannot put default on anonymous $.how parameter";
}
}
my class X::Parameter::Placeholder does X::Comp {
has $.parameter;
has $.right;
method message() {
"In signature parameter, placeholder variables like $.parameter are illegal\n"
~ "you probably meant a named parameter: '$.right'";
}
}
my class X::Parameter::Twigil does X::Comp {
has $.parameter;
has $.twigil;
method message() {
"In signature parameter $.parameter, it is illegal to use the $.twigil twigil";
}
}
my class X::Parameter::MultipleTypeConstraints does X::Comp {
has $.parameter;
method message() {
($.parameter ?? "Parameter $.parameter" !! 'A parameter')
~ " may only have one prefix type constraint";
}
}
my class X::Parameter::WrongOrder does X::Comp {
has $.misplaced;
has $.parameter;
has $.after;
method message() {
"Cannot put $.misplaced parameter $.parameter after $.after parameters";
}
}
my class X::Signature::NameClash does X::Comp {
has $.name;
method message() {
"Name $.name used for more than one named parameter";
}
}
my class X::Method::Private::Permission does X::Comp {
has $.method;
has $.source-package;
has $.calling-package;
method message() {
"Cannot call private method '$.method' on package $.source-package because it does not trust $.calling-package";
}
}
my class X::Method::Private::Unqualified does X::Comp {
has $.method;
method message() {
"Private method call to $.method must be fully qualified with the package containing the method";
}
}
my class X::Bind is Exception {
has $.target;
method message() {
$.target.defined
?? "Cannot bind to $.target"
!! 'Cannot use bind operator with this left-hand side'
}
}
my class X::Bind::NativeType does X::Comp {
has $.name;
method message() {
"Cannot bind to natively typed variable '$.name'; use assignment instead"
}
}
my class X::Bind::Slice is Exception {
has $.type;
method message() {
"Cannot bind to {$.type.^name} slice";
}
}
my class X::Bind::ZenSlice is X::Bind::Slice {
method message() {
"Cannot bind to {$.type.^name} zen slice";
}
}
my class X::Value::Dynamic does X::Comp {
has $.what;
method message() { "$.what value must be known at compile time" }
}
my class X::Syntax::Name::Null does X::Syntax {
method message() { 'Name component may not be null'; }
}
my class X::Syntax::UnlessElse does X::Syntax {
method message() { '"unless" does not take "else", please rewrite using "if"' }
}
my class X::Syntax::Reserved does X::Syntax {
has $.reserved;
has $.instead = '';
method message() { "The $.reserved is reserved$.instead" }
}
my class X::Syntax::P5 does X::Syntax {
method message() { 'This appears to be Perl 5 code' }
}
my class X::Syntax::NegatedPair does X::Syntax {
has $.key;
method message() { "Argument not allowed on negated pair with key '$.key'" }
}
my class X::Syntax::Variable::Numeric does X::Syntax {
has $.what = 'variable';
method message() { "Cannot declare a numeric $.what" }
}
my class X::Syntax::Variable::Match does X::Syntax {
method message() { 'Cannot declare a match variable' }
}
my class X::Syntax::Variable::Twigil does X::Syntax {
has $.twigil;
has $.scope;
method message() { "Cannot use $.twigil twigil on $.scope variable" }
}
my class X::Syntax::Variable::IndirectDeclaration does X::Syntax {
method message() { 'Cannot declare a variable by indirect name (use a hash instead?)' }
}
my class X::Syntax::Augment::WithoutMonkeyTyping does X::Syntax {
method message() { "augment not allowed without 'use MONKEY_TYPING'" };
}
my class X::Syntax::Augment::Role does X::Syntax {
has $.role-name;
method message() { "Cannot augment role $.role-name, since roles are immutable" };
}
my class X::Syntax::Argument::MOPMacro does X::Syntax {
has $.macro;
method message() { "Cannot give arguments to $.macro" };
}
my class X::Does::TypeObject is Exception {
method message() { "Cannot use 'does' operator with a type object." }
}
my class X::Role::Initialization is Exception {
has $.role;
method message() { "Can only supply an initialization value for a role if it has a single public attribute, but this is not the case for '{$.role.^name}'" }
}
my class X::Syntax::Comment::Embedded does X::Syntax {
method message() { "Opening bracket required for #` comment" }
}
my class X::Syntax::Pod::BeginWithoutIdentifier does X::Syntax does X::Pod {
method message() {
'=begin must be followed by an identifier; (did you mean "=begin pod"?)'
}
}
my class X::Syntax::Pod::BeginWithoutEnd does X::Syntax does X::Pod {
method message() { '=begin without matching =end' }
}
my class X::Syntax::Confused does X::Syntax {
method message() { 'Confused' }
}
my class X::Syntax::Malformed does X::Syntax {
has $.what;
method message() { "Malformed $.what" }
}
my class X::Syntax::Missing does X::Syntax {
has $.what;
method message() { "Missing $.what" }
}
my class X::Syntax::SigilWithoutName does X::Syntax {
method message() { 'Non-declarative sigil is missing its name' }
}
my class X::Syntax::Self::WithoutObject does X::Syntax {
method message() { "'self' used where no object is available" }
}
my class X::Syntax::VirtualCall does X::Syntax {
has $.call;
method message() { "Virtual call $.call may not be used on partially constructed objects" }
}
my class X::Syntax::NoSelf does X::Syntax {
has $.variable;
method message() { "Variable $.variable used where no 'self' is available" }
}
my class X::Syntax::Number::RadixOutOfRange does X::Syntax {
has $.radix;
method message() { "Radix $.radix out of range (allowed: 2..36)" }
}
my class X::Syntax::Regex::Adverb does X::Syntax {
has $.adverb;
has $.construct;
method message() { "Adverb $.adverb not allowed on $.construct" }
}
my class X::Syntax::Signature::InvocantMarker does X::Syntax {
method message() {
"Can only use : as invocant marker in a signature after the first parameter"
}
}
my class X::Syntax::Extension::Category does X::Syntax {
has $.category;
method message() {
"Cannot add tokens of category '$.category'";
}
}
my class X::Syntax::InfixInTermPosition does X::Syntax {
has $.infix;
method message() {
"Preceding context expects a term, but found infix $.infix instead";
}
}
my class X::Attribute::Package does X::Comp {
has $.package-kind;
has $.name;
method message() { "A $.package-kind cannot have attributes, but you tried to declare '$.name'" }
}
my class X::Attribute::NoPackage does X::Comp {
has $.name;
method message() { "You cannot declare attribute '$.name' here; maybe you'd like a class or a role?" }
}
my class X::Declaration::Scope does X::Comp {
has $.scope;
has $.declaration;
method message() { "Cannot use '$.scope' with $.declaration declaration" }
}
my class X::Declaration::Scope::Multi is X::Declaration::Scope {
method message() {
"Cannot use '$.scope' with individual multi candidates. Please declare an {$.scope}-scoped proto instead";
}
}
my class X::Anon::Multi does X::Comp {
has $.multiness;
has $.routine-type = 'routine';
method message() { "Cannot put $.multiness on anonymous $.routine-type" }
}
my class X::Anon::Augment does X::Comp {
has $.package-kind;
method message() { "Cannot augment anonymous $.package-kind" }
}
my class X::Augment::NoSuchType does X::Comp {
has $.package-kind;
has $.package;
method message() { "You tried to augment $.package-kind $.package, but it does not exist" }
}
my class X::Routine::Unwrap is Exception {
method message() { "Cannot unwrap routine: invalid wrap handle" }
}
my class X::Constructor::Positional is Exception {
method message() { "Default constructor only takes named arguments" }
}
my class X::Hash::Store::OddNumber is Exception {
method message() { "Odd number of elements found where hash expected" }
}
my class X::Package::Stubbed does X::Comp {
has @.packages;
# TODO: suppress display of line number
method message() {
"The following packages were stubbed but not defined:\n "
~ @.packages.join("\n ");
}
}
my class X::Phaser::PrePost is Exception {
has $.phaser = 'PRE';
has $.condition;
method message {
my $what = $.phaser eq 'PRE' ?? 'Precondition' !! 'Postcondition';
$.condition.defined
?? "$what '$.condition.trim()' failed"
!! "$what failed";
}
}
my class X::Str::Numeric is Exception {
has $.source;
has $.pos;
has $.reason;
method source-indicator {
constant marker = chr(0x23CF);
join '', "in '",
$.source.substr(0, $.pos),
marker,
$.source.substr($.pos),
"' (indicated by ",
marker,
")",
;
}
method message() {
"Cannot convert string to number: $.reason $.source-indicator";
}
}
my class X::Str::Match::x is Exception {
has $.got;
method message() {
"in Str.match, got invalid value of type {$.got.^name} for :x, must be Int or Range"
}
}
my class X::Str::Trans::IllegalKey is Exception {
has $.key;
method message {
"in Str.trans, got illegal substitution key of type {$.key.^name} (should be a Regex or Str)"
}
}
my class X::Str::Trans::InvalidArg is Exception {
has $.got;
method message() {
"Only Pair objects are allowed as arguments to Str.trans, got {$.got.^name}";
}
}
my class X::Sequence::Deduction is Exception {
method message() { 'Unable to deduce sequence' }
}
my class X::ControlFlow is Exception {
has $.illegal; # something like 'next'
has $.enclosing; # .... outside a loop
method message() { "$.illegal without $.enclosing" }
}
my class X::ControlFlow::Return is X::ControlFlow {
method illegal() { 'return' }
method enclosing() { 'Routine' }
method message() { 'Attempt to return outside of any Routine' }
}
my class X::Composition::NotComposable does X::Comp {
has $.target-name;
has $.composer;
method message() {
$.composer.^name ~ " is not composable, so $.target-name cannot compose it";
}
}
my class X::TypeCheck is Exception {
has $.operation;
has $.got;
has $.expected;
method message() {
"Type check failed in $.operation; expected '{$.expected.^name}' but got '{$.got.^name}'";
}
}
my class X::TypeCheck::Binding is X::TypeCheck {
method operation { 'binding' }
}
my class X::TypeCheck::Return is X::TypeCheck {
method operation { 'returning' }
method message() {
"Type check failed for return value; expected '{$.expected.^name}' but got '{$.got.^name}'";
}
}
my class X::TypeCheck::Assignment is X::TypeCheck {
has $.symbol;
method operation { 'assignment' }
method message {
$.symbol.defined
?? "Type check failed in assignment to '$.symbol'; expected '{$.expected.^name}' but got '{$.got.^name}'"
!! "Type check failed in assignment; expected '{$.expected.^name}' but got '{$.got.^name}'";
}
}
my class X::Assignment::RO is Exception {
method message {
"Cannot assign to a non-container";
}
}
my class X::NoDispatcher is Exception {
has $.redispatcher;
method message() {
"$.redispatcher is not in the dynamic scope of a dispatcher";
}
}
my class X::Localizer::NoContainer is Exception {
has $.localizer;
method message() {
"Can only use '$.localizer' on a container";
}
}
my class X::Mixin::NonComposable is Exception {
has $.target;
has $.rolish;
method message() {
"Cannot mix in non-composable type {$.rolish.^name} into object of type {$.target.^name}";
}
}
my class X::Inheritance::Unsupported does X::Comp {
# note that this exception is thrown before the child type object
# has been composed, so it's useless to carry it around. Use the
# name instead.
has $.child-typename;
has $.parent;
method message {
$.parent.^name ~ ' does not support inheritance, so '
~ $.child-typename ~ ' cannot inherit from it';
}
}
my class X::Export::NameClash does X::Comp {
has $.symbol;
method message() {
"A symbol '$.symbol' has already been exported";
}
}
my class X::HyperOp::NonDWIM is Exception {
has &.operator;
has $.left-elems;
has $.right-elems;
method message() {
"Lists on both side of non-dwimmy hyperop of &.operator.name() are not of the same length\n"
~ "left: $.left-elems elements, right: $.right-elems elements";
}
}
my class X::Set::Coerce is Exception {
has $.thing;
method message {
"Cannot coerce object of type {$.thing.^name} to Set. To create a one-element set, pass it to the 'set' function";
}
}
my role X::Temporal is Exception { }
my class X::Temporal::InvalidFormat does X::Temporal {
has $.invalid-str;
has $.target = 'Date';
has $.format;
method message() {
"Invalid $.target string '$.invalid-str'; use $.format instead";
}
}
my class X::Temporal::Truncation does X::Temporal {
has $.invocant;
has $.error;
method message() {
"Error while truncating $.invocant: $.error";
}
}
my class X::DateTime::TimezoneClash does X::Temporal {
method message() {
'DateTime.new(Str): :timezone argument not allowed with a timestamp offset';
}
}
my class X::Eval::NoSuchLang is Exception {
has $.lang;
method message() {
"No compiler compiler available for language '$.lang'";
}
}
my class X::Import::MissingSymbols is Exception {
has $.from;
has @.missing;
method message() {
"Trying to import from '$.from', but the following symbols are missing: "
~ @.missing.join(', ');
}
}
my class X::Numeric::Real is Exception {
has $.target;
has $.reason;
has $.source;
method message() {
"Can not convert $.source to {$.target.^name}: $.reason";
}
}
{
my %c_ex;
%c_ex{'X::TypeCheck::Binding'} := sub ($got, $expected) is hidden_from_backtrace {
X::TypeCheck::Binding.new(:$got, :$expected).throw;
};
%c_ex<X::TypeCheck::Assignment> := sub ($symbol, $got, $expected) is hidden_from_backtrace {
X::TypeCheck::Assignment.new(:$symbol, :$got, :$expected).throw;
};
%c_ex{'X::TypeCheck::Return'} := sub ($got, $expected) is hidden_from_backtrace {
X::TypeCheck::Return.new(:$got, :$expected).throw;
};
%c_ex<X::Assignment::RO> := sub () is hidden_from_backtrace {
X::Assignment::RO.new.throw;
};
%c_ex{'X::ControlFlow::Return'} := sub () is hidden_from_backtrace {
X::ControlFlow::Return.new().throw;
};
%c_ex{'X::NoDispatcher'} := sub ($redispatcher) is hidden_from_backtrace {
X::NoDispatcher.new(:$redispatcher).throw;
};
my Mu $parrot_c_ex := nqp::getattr(%c_ex, EnumMap, '$!storage');
pir::set_hll_global__vsP('P6EX', $parrot_c_ex);
0;
}
# vim: ft=perl6