This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
/
errors.ts
1016 lines (918 loc) · 20.2 KB
/
errors.ts
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
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Contains the types for error and `ErrorResult` objects.
* @category Main Format
*
* @packageDocumentation
*/
import debugModule from "debug";
const debug = debugModule("codec:format:errors");
//error counterpart to values.ts
//Note: Many of the errors defined here deliberately *don't* extend Error.
//This is because they're not for throwing. If you want to throw one,
//wrap it in a DecodingError.
import type BN from "bn.js";
import type * as Types from "./types";
import type * as Ast from "@truffle/codec/ast/types";
import type * as Storage from "@truffle/codec/storage/types";
import type { PaddingType } from "@truffle/codec/common";
/*
* SECTION 1: Generic types for values in general (including errors).
*/
/**
* A result which is an error rather than a value
*
* @Category General categories
*/
export type ErrorResult =
| ElementaryErrorResult
| ArrayErrorResult
| MappingErrorResult
| StructErrorResult
| MagicErrorResult
| TypeErrorResult
| TupleErrorResult
| FunctionExternalErrorResult
| FunctionInternalErrorResult
| OptionsErrorResult;
/**
* An error result for an ABI type
*
* @Category General categories
*/
export type AbiErrorResult =
| UintErrorResult
| IntErrorResult
| BoolErrorResult
| BytesErrorResult
| AddressErrorResult
| FixedErrorResult
| UfixedErrorResult
| StringErrorResult
| ArrayErrorResult
| FunctionExternalErrorResult
| TupleErrorResult;
/**
* One of the underlying errors contained in an [[ErrorResult]]
*
* @Category General categories
*/
export type DecoderError =
| GenericError
| UintError
| IntError
| BoolError
| BytesStaticError
| BytesDynamicError
| AddressError
| StringError
| FixedError
| UfixedError
| ArrayError
| MappingError
| StructError
| MagicError
| TypeErrorUnion
| TupleError
| EnumError
| UserDefinedValueTypeError
| ContractError
| FunctionExternalError
| FunctionInternalError
| InternalUseError;
/*
* SECTION 2: Built-in elementary types
*/
/**
* An error result for an elementary value
*
* @Category Elementary types
*/
export type ElementaryErrorResult =
| UintErrorResult
| IntErrorResult
| BoolErrorResult
| BytesErrorResult
| AddressErrorResult
| StringErrorResult
| FixedErrorResult
| UfixedErrorResult
| EnumErrorResult
| UserDefinedValueTypeErrorResult
| ContractErrorResult;
/**
* An error result for a built-in value type
*
* @Category Elementary types
*/
export type BuiltInValueErrorResult =
| UintErrorResult
| IntErrorResult
| BoolErrorResult
| BytesStaticErrorResult
| AddressErrorResult
| FixedErrorResult
| UfixedErrorResult;
/**
* An error result for a bytestring
*
* @Category Elementary types
*/
export type BytesErrorResult = BytesStaticErrorResult | BytesDynamicErrorResult;
/**
* An error result for an unsigned integer
*
* @Category Elementary types
*/
export interface UintErrorResult {
type: Types.UintType;
kind: "error";
error: GenericError | UintError;
}
/**
* A uint-specific error
*
* @Category Elementary types
*/
export type UintError = UintPaddingError;
/**
* A padding error for an unsigned integer (note padding is not always checked)
*
* @Category Elementary types
*/
export interface UintPaddingError {
/**
* hex string
*/
raw: string;
kind: "UintPaddingError";
paddingType: PaddingType;
}
/**
* An error result for a signed integer
*
* @Category Elementary types
*/
export interface IntErrorResult {
type: Types.IntType;
kind: "error";
error: GenericError | IntError;
}
/**
* An int-specific error
*
* @Category Elementary types
*/
export type IntError = IntPaddingError;
/**
* A padding error for a signed integer (note padding is not always checked)
*
* @Category Elementary types
*/
export interface IntPaddingError {
/**
* hex string
*/
raw: string;
kind: "IntPaddingError";
paddingType: PaddingType;
}
/**
* An error result for a boolean
*
* @Category Elementary types
*/
export interface BoolErrorResult {
type: Types.BoolType;
kind: "error";
error: GenericError | BoolError;
}
/**
* A bool-specific error
*
* @Category Elementary types
*/
export type BoolError = BoolOutOfRangeError | BoolPaddingError;
/**
* The bool is neither 0 nor 1
*
* @Category Elementary types
*/
export interface BoolOutOfRangeError {
rawAsBN: BN;
kind: "BoolOutOfRangeError";
}
/**
* A padding error for a boolean
*
* @Category Elementary types
*/
export interface BoolPaddingError {
/**
* hex string
*/
raw: string;
kind: "BoolPaddingError";
paddingType: PaddingType;
}
/**
* An error result for a static-length bytestring
*
* @Category Elementary types
*/
export interface BytesStaticErrorResult {
type: Types.BytesTypeStatic;
kind: "error";
error: GenericError | BytesStaticError;
}
/**
* A static-bytestring-specific error
*
* @Category Elementary types
*/
export type BytesStaticError = BytesPaddingError;
/**
* A padding error for a static-length bytestring (note padding is not always checked)
*
* @Category Elementary types
*/
export interface BytesPaddingError {
/**
* hex string
*/
raw: string;
kind: "BytesPaddingError";
paddingType: PaddingType;
}
/**
* An error result for a dynamic-length bytestring
*
* @Category Elementary types
*/
export interface BytesDynamicErrorResult {
type: Types.BytesTypeDynamic;
kind: "error";
error: GenericError | BytesDynamicError;
}
/**
* A dynamic-bytestring-specific error
*
* @Category Elementary types
*/
export type BytesDynamicError = DynamicDataImplementationError;
/**
* An error result for an address
*
* @Category Elementary types
*/
export interface AddressErrorResult {
type: Types.AddressType;
kind: "error";
error: GenericError | AddressError;
}
/**
* A address-specific error
*
* @Category Elementary types
*/
export type AddressError = AddressPaddingError;
/**
* A padding error for an address (note padding is not always checked)
*
* @Category Elementary types
*/
export interface AddressPaddingError {
/**
* hex string; no checksum
*/
raw: string;
kind: "AddressPaddingError";
paddingType: PaddingType;
}
/**
* An error result for a string
*
* @Category Elementary types
*/
export interface StringErrorResult {
type: Types.StringType;
kind: "error";
error: GenericError | StringError;
}
/**
* A string-specific error
*
* @Category Elementary types
*/
export type StringError = DynamicDataImplementationError;
/**
* An error result for a signed fixed-point number
*
* @Category Elementary types
*/
export interface FixedErrorResult {
type: Types.FixedType;
kind: "error";
error: GenericError | FixedError;
}
/**
* An error result for an unsigned fixed-point number
*
* @Category Elementary types
*/
export interface UfixedErrorResult {
type: Types.UfixedType;
kind: "error";
error: GenericError | UfixedError;
}
/**
* A fixed-specific error
*
* @Category Elementary types
*/
export type FixedError = FixedPaddingError;
/**
* A padding error for a signed fixed-point number (note padding is not always checked)
*
* @Category Elementary types
*/
export interface FixedPaddingError {
/**
* hex string
*/
raw: string;
kind: "FixedPaddingError";
paddingType: PaddingType;
}
/**
* A ufixed-specific error
*
* @Category Elementary types
*/
export type UfixedError = UfixedPaddingError;
/**
* A padding error for an unsigned fixed-point number (note padding is not always checked)
*
* @Category Elementary types
*/
export interface UfixedPaddingError {
/**
* hex string
*/
raw: string;
kind: "UfixedPaddingError";
paddingType: PaddingType;
}
/*
* SECTION 3: User-defined elementary types
*/
/**
* An error result for an enum
*
* @Category User-defined elementary types
*/
export interface EnumErrorResult {
type: Types.EnumType;
kind: "error";
error: GenericError | EnumError;
}
/**
* An enum-specific error
*
* @Category User-defined elementary types
*/
export type EnumError =
| EnumOutOfRangeError
| EnumPaddingError
| EnumNotFoundDecodingError;
/**
* The enum is out of range
*
* @Category User-defined elementary types
*/
export interface EnumOutOfRangeError {
kind: "EnumOutOfRangeError";
type: Types.EnumType;
rawAsBN: BN;
}
/**
* A padding error for an enum
*
* @Category Elementary types
*/
export interface EnumPaddingError {
/**
* hex string
*/
raw: string;
type: Types.EnumType;
kind: "EnumPaddingError";
paddingType: PaddingType;
}
/**
* The enum type definition could not be located
*
* @Category User-defined elementary types
*/
export interface EnumNotFoundDecodingError {
kind: "EnumNotFoundDecodingError";
type: Types.EnumType;
rawAsBN: BN;
}
/**
* An error result for a user-defined value type
*
* @Category User-defined elementary types
*/
export interface UserDefinedValueTypeErrorResult {
type: Types.UserDefinedValueTypeType;
kind: "error";
error: GenericError | UserDefinedValueTypeError;
}
/**
* A UDVT-specific error
*
* @Category User-defined elementary types
*/
export type UserDefinedValueTypeError = WrappedError;
/**
* An error result representing something going wrong decoding
* the underlying type when decoding a UDVT
*
* @Category User-defined elementary types
*/
export interface WrappedError {
kind: "WrappedError";
error: BuiltInValueErrorResult;
}
/**
* An error result for a contract
*
* @Category User-defined elementary types
*/
export interface ContractErrorResult {
type: Types.ContractType;
kind: "error";
error: GenericError | ContractError;
}
/**
* A contract-specific error
*
* @Category User-defined elementary types
*/
export type ContractError = ContractPaddingError;
/**
* A padding error for contract (note padding is not always checked)
*
* @Category User-defined elementary types
*/
export interface ContractPaddingError {
/**
* hex string
*/
raw: string;
kind: "ContractPaddingError";
paddingType: PaddingType;
}
/*
* SECTION 4: Container types (including magic)
*/
/**
* An error result for an array
*
* @Category Container types
*/
export interface ArrayErrorResult {
type: Types.ArrayType;
kind: "error";
error: GenericError | ArrayError;
}
/**
* An arrray-specific error
*
* @Category Container types
*/
export type ArrayError = DynamicDataImplementationError;
/**
* An error result for a mapping
*
* @Category Container types
*/
export interface MappingErrorResult {
type: Types.MappingType;
kind: "error";
error: GenericError | MappingError;
}
/**
* A mapping-specific error (there are none)
*
* @Category Container types
*/
export type MappingError = never;
/**
* An error result for a struct
*
* @Category Container types
*/
export interface StructErrorResult {
type: Types.StructType;
kind: "error";
error: GenericError | StructError;
}
/**
* A struct-specific error
*
* @Category Container types
*/
export type StructError = DynamicDataImplementationError;
/**
* An error result for a tuple
*
* @Category Container types
*/
export interface TupleErrorResult {
type: Types.TupleType;
kind: "error";
error: GenericError | TupleError;
}
/**
* A tuple-specific error
*
* @Category Container types
*/
export type TupleError = DynamicDataImplementationError;
/**
* An error result for a magic variable
*
* @Category Special container types (debugger-only)
*/
export interface MagicErrorResult {
type: Types.MagicType;
kind: "error";
error: GenericError | MagicError;
}
/**
* A magic-specific error (there are none)
*
* @Category Special container types (debugger-only)
*/
export type MagicError = never;
/**
* An error result for a type
*
* @Category Special container types (debugger-only)
*/
export interface TypeErrorResult {
type: Types.TypeType;
kind: "error";
error: GenericError | TypeErrorUnion;
}
/**
* An error specific to type values (there are none);
* this isn't called TypeError because that's not legal
*
* @Category Special container types (debugger-only)
*/
export type TypeErrorUnion = never;
/*
* SECTION 5: External functions
*/
/**
* An error result for an external function
*
* @Category Function types
*/
export interface FunctionExternalErrorResult {
type: Types.FunctionExternalType;
kind: "error";
error: GenericError | FunctionExternalError;
}
/**
* An external-function specific error
*
* @Category Function types
*/
export type FunctionExternalError =
| FunctionExternalNonStackPaddingError
| FunctionExternalStackPaddingError;
/**
* This error kind represents a padding error for an external function pointer located anywhere other than the stack.
*
* @Category Function types
*/
export interface FunctionExternalNonStackPaddingError {
/**
* hex string
*/
raw: string;
kind: "FunctionExternalNonStackPaddingError";
paddingType: PaddingType;
}
/**
* This error kind represents a padding error for external function pointer located on the stack.
*
* @Category Function types
*/
export interface FunctionExternalStackPaddingError {
/**
* hex string (no checksum; also a full word long)
*/
rawAddress: string;
/**
* hex string (but a full word long)
*/
rawSelector: string;
kind: "FunctionExternalStackPaddingError";
}
/*
* SECTION 6: Internal functions
*/
/**
* An error result for an internal function
*
* @Category Function types
*/
export interface FunctionInternalErrorResult {
type: Types.FunctionInternalType;
kind: "error";
error: GenericError | FunctionInternalError;
}
/**
* An internal-function specific error
*
* @Category Function types
*/
export type FunctionInternalError =
| FunctionInternalPaddingError
| NoSuchInternalFunctionError
| DeployedFunctionInConstructorError
| MalformedInternalFunctionError;
/**
* A padding error for an internal function
*
* @Category Function types
*/
export interface FunctionInternalPaddingError {
/**
* hex string
*/
raw: string;
kind: "FunctionInternalPaddingError";
paddingType: PaddingType;
}
/**
* Indicates that the function pointer being decoded
* fails to point to a valid function, and also is not one of the
* default values
*
* @Category Function types
*/
export interface NoSuchInternalFunctionError {
kind: "NoSuchInternalFunctionError";
context: Types.ContractType;
deployedProgramCounter: number;
constructorProgramCounter: number;
}
/**
* Indicates that this is a deployed-style pointer,
* despite the fact that you're in a constructor
*
* @Category Function types
*/
export interface DeployedFunctionInConstructorError {
kind: "DeployedFunctionInConstructorError";
context: Types.ContractType;
deployedProgramCounter: number;
constructorProgramCounter: number;
}
/**
* Used when the deployed PC is zero but the constructor PC
* is nonzero
*
* @Category Function types
*/
export interface MalformedInternalFunctionError {
kind: "MalformedInternalFunctionError";
context: Types.ContractType;
deployedProgramCounter: number;
constructorProgramCounter: number;
}
/*
* SECTION 7: Options
*/
/**
* An options error. This should never happen,
* as options are never decoded, but it's included for
* completeness.
*/
export interface OptionsErrorResult {
type: Types.OptionsType;
kind: "error";
error: GenericError | OptionsError;
}
/**
* The options type has no type-specific errors at the moment
*/
export type OptionsError = never;
/*
* SECTION 8: Generic errors
*/
/**
* A type-non-specific error
*
* @Category Generic errors
*/
export type GenericError =
| UserDefinedTypeNotFoundError
| IndexedReferenceTypeError
| ReadError;
/**
* A read error
*
* @Category Generic errors
*/
export type ReadError =
| UnsupportedConstantError
| ReadErrorStack
| ReadErrorBytes
| ReadErrorStorage
| StorageNotSuppliedError
| UnusedImmutableError
| CodeNotSuppliedError;
/**
* An error resulting from overlarge length or pointer values
*
* @Category Generic errors
*/
export type DynamicDataImplementationError =
| OverlongArraysAndStringsNotImplementedError
| OverlargePointersNotImplementedError;
/**
* An error that may occur in a component other than the main
* core of the decoder itself and thus may need to get thrown to it
*
* @Category Generic errors
*/
export type ErrorForThrowing = UserDefinedTypeNotFoundError | ReadError;
/**
* Used when decoding an indexed parameter of reference (or tuple) type. These
* can't meaningfully be decoded, so instead they decode to an error, sorry.
*
* @Category Generic errors
*/
export interface IndexedReferenceTypeError {
kind: "IndexedReferenceTypeError";
type: Types.ReferenceType | Types.TupleType;
/**
* hex string
*/
raw: string;
}
/**
* An error for when can't find the definition info for a user-defined type
*
* @Category Generic errors
*/
export interface UserDefinedTypeNotFoundError {
kind: "UserDefinedTypeNotFoundError";
type: Types.UserDefinedType;
}
/**
* An error for an unsupported type of constant (this counts as a read error)
*
* @Category Generic errors
*/
export interface UnsupportedConstantError {
kind: "UnsupportedConstantError";
definition: Ast.AstNode;
}
/**
* Read error on the stack
*
* @Category Generic errors
*/
export interface ReadErrorStack {
kind: "ReadErrorStack";
from: number;
to: number;
}
/**
* A byte-based location
*/
export type BytesLocation =
| "memory"
| "calldata"
| "eventdata"
| "returndata"
| "code";
/**
* Read error in a byte-based location (memory, calldata, etc)
*
* @Category Generic errors
*/
export interface ReadErrorBytes {
kind: "ReadErrorBytes";
location: BytesLocation;
start: number;
length: number;
}
/**
* Read error in storage
*
* @Category Generic errors
*/
export interface ReadErrorStorage {
kind: "ReadErrorStorage";
range: Storage.Range;
}
/**
* A read error in storage, but one triggered deliberately to indicate
* that that particular area of storage is unknown, rather than due to
* an unexpected error condition. This error is triggered by passing
* null in response to a storage request.
*
* @Category Generic errors
*/
export interface StorageNotSuppliedError {
kind: "StorageNotSuppliedError";
slot: BN;
}
/**
* Attempting to read an immutable that is never stored anywhere
*
* @Category Generic errors
*/
export interface UnusedImmutableError {
kind: "UnusedImmutableError";
}
/**
* A read error in code, but one triggered deliberately to indicate that
* bytecode is unknown, rather than due to an unexpected error condition. This
* error is triggered by passing null in response to a storage request.
*
* @Category Generic errors
*/
export interface CodeNotSuppliedError {
kind: "CodeNotSuppliedError";
address: string;
}
/**
* Error for array/string/bytestring having length bigger than a JS number
*
* @Category Generic errors
*/
export interface OverlongArraysAndStringsNotImplementedError {
kind: "OverlongArraysAndStringsNotImplementedError";
lengthAsBN: BN;
dataLength?: number; //only included when the special strict mode check fails
}
/**
* Error for dynamic type being represented by pointer bigger than a JS number
*
* @Category Generic errors
*/
export interface OverlargePointersNotImplementedError {
kind: "OverlargePointersNotImplementedError";
pointerAsBN: BN;
}
/* SECTION 9: Internal use errors */
/* you should never see these returned.
* they are only for internal use. */
/**
* Internal-use error
*
* @Category Internal-use errors
*/
export type InternalUseError =
| OverlongArrayOrStringStrictModeError
| InternalFunctionInABIError
| ReEncodingMismatchError;
/**
* Error for the stricter length check in strict mode
*
* @Category Internal-use errors
*/
export interface OverlongArrayOrStringStrictModeError {
kind: "OverlongArrayOrStringStrictModeError";
lengthAsBN: BN;
dataLength: number;
}
/**
* This should never come up, but just to be sure...
*
* @Category Internal-use errors
*/