-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexecution_error_test.go
More file actions
1732 lines (1566 loc) · 47.7 KB
/
Copy pathexecution_error_test.go
File metadata and controls
1732 lines (1566 loc) · 47.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
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
package evm
import (
"bytes"
"context"
"encoding/json"
"errors"
"math/big"
"strings"
"testing"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
gethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/smartcontractkit/mcms/sdk/evm/bindings"
"github.com/smartcontractkit/mcms/sdk/evm/mocks"
)
const (
errMsgOriginalError = "original error"
errMsgExecutionFailed = "execution failed"
errMsgRawRevertData = "raw revert data"
errMsgUnderlyingReason = "underlying reason"
errMsgRevertReason = "revert reason"
errMsgExecutionRevertedHex = "execution reverted: 0x1234567890abcdef"
errMsgCustomError = "execution reverted: custom error 0x70de1b4b: aabbccdd"
errMsgExpectedNilFmt = "Expected nil for input: %q"
errMsgExpectedNonNilFmt = "Expected non-nil result for input: %q"
descriptionEmptyString = "empty string"
errMsgFailedTimelockABI = "Failed to get RBACTimelock ABI"
)
type executionErrorErrorCase struct {
name string
execErr *ExecutionError
expectedContains []string
expectedNotContains []string
}
var executionErrorErrorCases = []executionErrorErrorCase{
{
name: "with decoded underlying reason - highest priority",
execErr: &ExecutionError{
OriginalError: errors.New(errMsgOriginalError),
UnderlyingReasonRaw: "0x1234",
UnderlyingReasonDecoded: "decoded underlying revert reason",
RevertReasonDecoded: "decoded reason",
RevertReasonRaw: &CustomErrorData{
Selector: [4]byte{0x12, 0x34, 0x56, 0x78},
Data: []byte{0xaa, 0xbb, 0xcc},
},
},
expectedContains: []string{
errMsgExecutionFailed,
errMsgOriginalError,
errMsgUnderlyingReason + ": decoded underlying revert reason",
},
expectedNotContains: []string{
errMsgRevertReason + ": decoded reason",
errMsgRawRevertData,
},
},
{
name: "with raw underlying reason when decoded unavailable",
execErr: &ExecutionError{
OriginalError: errors.New(errMsgOriginalError),
UnderlyingReasonRaw: "underlying revert reason",
RevertReasonDecoded: "decoded reason",
RevertReasonRaw: &CustomErrorData{
Selector: [4]byte{0x12, 0x34, 0x56, 0x78},
Data: []byte{0xaa, 0xbb, 0xcc},
},
},
expectedContains: []string{
errMsgExecutionFailed,
errMsgOriginalError,
errMsgUnderlyingReason + ": underlying revert reason",
},
expectedNotContains: []string{
errMsgRevertReason + ": decoded reason",
errMsgRawRevertData,
},
},
{
name: "with decoded revert reason - second priority",
execErr: &ExecutionError{
OriginalError: errors.New(errMsgOriginalError),
RevertReasonDecoded: "OutOfBoundsGroup",
RevertReasonRaw: &CustomErrorData{
Selector: [4]byte{0x12, 0x34, 0x56, 0x78},
Data: []byte{0xaa, 0xbb, 0xcc},
},
},
expectedContains: []string{
errMsgExecutionFailed,
errMsgOriginalError,
errMsgRevertReason + ": OutOfBoundsGroup",
},
expectedNotContains: []string{
errMsgUnderlyingReason,
errMsgRawRevertData,
},
},
{
name: "with raw revert reason - third priority",
execErr: &ExecutionError{
OriginalError: errors.New(errMsgOriginalError),
RevertReasonRaw: &CustomErrorData{
Selector: [4]byte{0x12, 0x34, 0x56, 0x78},
Data: []byte{0xaa, 0xbb, 0xcc},
},
},
expectedContains: []string{
errMsgExecutionFailed,
errMsgOriginalError,
errMsgRawRevertData,
"12345678aabbcc",
},
expectedNotContains: []string{
errMsgUnderlyingReason,
errMsgRevertReason,
},
},
{
name: "with empty raw revert reason - selector only still shows raw data",
execErr: &ExecutionError{
OriginalError: errors.New(errMsgOriginalError),
RevertReasonRaw: &CustomErrorData{
Selector: [4]byte{0x12, 0x34, 0x56, 0x78},
Data: []byte{},
},
},
expectedContains: []string{
errMsgExecutionFailed,
errMsgOriginalError,
errMsgRawRevertData,
"12345678",
},
expectedNotContains: []string{
errMsgUnderlyingReason,
errMsgRevertReason + ":",
},
},
{
name: "with nil raw revert reason - should not show raw data",
execErr: &ExecutionError{
OriginalError: errors.New(errMsgOriginalError),
RevertReasonRaw: nil,
},
expectedContains: []string{
errMsgExecutionFailed,
errMsgOriginalError,
},
expectedNotContains: []string{
errMsgUnderlyingReason,
errMsgRevertReason,
errMsgRawRevertData,
},
},
{
name: "only original error - fallback case",
execErr: &ExecutionError{
OriginalError: errors.New(errMsgOriginalError),
},
expectedContains: []string{
errMsgExecutionFailed,
errMsgOriginalError,
},
expectedNotContains: []string{
errMsgUnderlyingReason,
errMsgRevertReason,
errMsgRawRevertData,
},
},
{
name: "with CallReverted selector",
execErr: &ExecutionError{
OriginalError: errors.New(errMsgOriginalError),
RevertReasonRaw: &CustomErrorData{
Selector: CallRevertedSelector,
Data: []byte{0xaa, 0xbb, 0xcc},
},
},
expectedContains: []string{
errMsgExecutionFailed,
errMsgOriginalError,
errMsgRawRevertData,
"70de1b4b",
},
},
{
name: "with decoded reason but no underlying reason",
execErr: &ExecutionError{
OriginalError: errors.New("contract call failed"),
RevertReasonDecoded: "InsufficientSigners",
},
expectedContains: []string{
errMsgExecutionFailed,
"contract call failed",
errMsgRevertReason + ": InsufficientSigners",
},
expectedNotContains: []string{
errMsgUnderlyingReason,
errMsgRawRevertData,
},
},
}
func TestExecutionErrorError(t *testing.T) {
t.Parallel()
for _, tt := range executionErrorErrorCases {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := tt.execErr.Error()
// Check that all expected substrings are present
for _, expected := range tt.expectedContains {
assert.Contains(t, result, expected, "Error message should contain: %s", expected)
}
// Check that unexpected substrings are not present
for _, notExpected := range tt.expectedNotContains {
assert.NotContains(t, result, notExpected, "Error message should NOT contain: %s", notExpected)
}
})
}
}
func TestExecutionErrorUnwrap(t *testing.T) {
t.Parallel()
originalErr := errors.New(errMsgOriginalError)
execErr := &ExecutionError{
OriginalError: originalErr,
}
unwrapped := execErr.Unwrap()
require.Equal(t, originalErr, unwrapped, "Unwrap should return the original error")
}
func TestCustomErrorData(t *testing.T) {
t.Parallel()
tests := []struct {
name string
customErr *CustomErrorData
expectedHex string
expectedCombined []byte
}{
{
name: "nil CustomErrorData",
customErr: nil,
expectedHex: "",
expectedCombined: nil,
},
{
name: "CallReverted selector",
customErr: &CustomErrorData{
Selector: CallRevertedSelector,
Data: []byte{0xaa, 0xbb, 0xcc},
},
expectedHex: "70de1b4b",
expectedCombined: []byte{0x70, 0xde, 0x1b, 0x4b, 0xaa, 0xbb, 0xcc},
},
{
name: "custom selector with empty data",
customErr: &CustomErrorData{
Selector: [4]byte{0x12, 0x34, 0x56, 0x78},
Data: []byte{},
},
expectedHex: "12345678",
expectedCombined: []byte{0x12, 0x34, 0x56, 0x78},
},
{
name: "custom selector with data",
customErr: &CustomErrorData{
Selector: [4]byte{0xAB, 0xCD, 0xEF, 0x01},
Data: []byte{0x11, 0x22, 0x33, 0x44},
},
expectedHex: "abcdef01",
expectedCombined: []byte{0xAB, 0xCD, 0xEF, 0x01, 0x11, 0x22, 0x33, 0x44},
},
{
name: "zero selector",
customErr: &CustomErrorData{
Selector: [4]byte{0x00, 0x00, 0x00, 0x00},
Data: []byte{0xFF},
},
expectedHex: "00000000",
expectedCombined: []byte{0x00, 0x00, 0x00, 0x00, 0xFF},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
// Test HexSelector()
hexSelector := tt.customErr.HexSelector()
assert.Equal(t, tt.expectedHex, hexSelector, "HexSelector() mismatch")
// Test Combined()
combined := tt.customErr.Combined()
assert.Equal(t, tt.expectedCombined, combined, "Combined() mismatch")
})
}
}
func TestCustomErrorDataJSON(t *testing.T) {
t.Parallel()
t.Run("marshal_and_unmarshal_with_data", func(t *testing.T) {
t.Parallel()
custom := &CustomErrorData{
Selector: [4]byte{0xde, 0xad, 0xbe, 0xef},
Data: []byte{0xaa, 0xbb, 0xcc},
}
bytes, err := json.Marshal(custom)
require.NoError(t, err)
assert.JSONEq(t, `{"selector":"0xdeadbeef","data":"0xaabbcc"}`, string(bytes))
var decoded CustomErrorData
require.NoError(t, json.Unmarshal(bytes, &decoded))
assert.Equal(t, custom.Selector, decoded.Selector)
assert.Equal(t, custom.Data, decoded.Data)
})
t.Run("marshal_without_data", func(t *testing.T) {
t.Parallel()
custom := &CustomErrorData{
Selector: [4]byte{0x70, 0xde, 0x1b, 0x4b},
}
bytes, err := json.Marshal(custom)
require.NoError(t, err)
assert.JSONEq(t, `{"selector":"0x70de1b4b"}`, string(bytes))
})
t.Run("unmarshal_invalid_hex_data", func(t *testing.T) {
t.Parallel()
var decoded CustomErrorData
err := json.Unmarshal([]byte(`{"selector":"0xdeadbeef","data":"0xzz"}`), &decoded)
require.Error(t, err)
})
}
func TestExecutionErrorJSON(t *testing.T) {
t.Parallel()
t.Run("marshal_strips_original_error", func(t *testing.T) {
t.Parallel()
execErr := &ExecutionError{
RevertReasonRaw: &CustomErrorData{
Selector: CallRevertedSelector,
Data: []byte{0x01, 0x02},
},
RevertReasonDecoded: "CallReverted(truncated)",
UnderlyingReasonRaw: "0xdeadbeef",
UnderlyingReasonDecoded: "OutOfBoundsGroup()",
OriginalError: errors.New("execution reverted: custom error"),
}
bytes, err := json.Marshal(execErr)
require.NoError(t, err)
var parsed map[string]any
require.NoError(t, json.Unmarshal(bytes, &parsed))
require.Equal(t, "execution reverted: custom error", parsed["OriginalError"])
require.Equal(t, "CallReverted(truncated)", parsed["RevertReasonDecoded"])
require.Equal(t, "OutOfBoundsGroup()", parsed["UnderlyingReasonDecoded"])
})
t.Run("unmarshal_restores_original_error", func(t *testing.T) {
t.Parallel()
payload := `{
"Transaction": null,
"RevertReasonRaw": {"selector": "0x70de1b4b", "data": "0x"},
"RevertReasonDecoded": "CallReverted(truncated)",
"UnderlyingReasonRaw": "0xdeadbeef",
"UnderlyingReasonDecoded": "OutOfBoundsGroup()",
"OriginalError": "execution reverted: custom error"
}`
var execErr ExecutionError
require.NoError(t, json.Unmarshal([]byte(payload), &execErr))
require.EqualError(t, execErr.OriginalError, "execution reverted: custom error")
require.Equal(t, "CallReverted(truncated)", execErr.RevertReasonDecoded)
require.Equal(t, "OutOfBoundsGroup()", execErr.UnderlyingReasonDecoded)
require.NotNil(t, execErr.RevertReasonRaw)
assert.Equal(t, CallRevertedSelector, execErr.RevertReasonRaw.Selector)
})
}
func TestExtractHexEncodedRevertData(t *testing.T) {
t.Parallel()
tests := []struct {
name string
errStr string
expected []byte
shouldMatch bool
}{
{
name: "valid hex string",
errStr: errMsgExecutionRevertedHex,
expected: common.FromHex("0x1234567890abcdef"),
shouldMatch: true,
},
{
name: "mixed case hex",
errStr: "0xAbCdEf123456",
expected: common.FromHex("0xAbCdEf123456"),
shouldMatch: true,
},
{
name: "hex stops at non-hex character",
errStr: "0x123456xyz789",
expected: common.FromHex("0x123456"),
shouldMatch: true,
},
{
name: "multiple hex strings - matches first",
errStr: "0x123456 0xabcdef",
expected: common.FromHex("0x123456"),
shouldMatch: true,
},
{
name: descriptionEmptyString,
errStr: "",
expected: nil,
shouldMatch: false,
},
{
name: "no hex pattern",
errStr: "just some text without hex",
expected: nil,
shouldMatch: false,
},
{
name: "only 0x prefix",
errStr: "0x",
expected: nil,
shouldMatch: false,
},
{
name: "0x followed by non-hex",
errStr: "0xghijkl",
expected: nil,
shouldMatch: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := extractHexEncodedRevertData(tt.errStr)
if !tt.shouldMatch {
assert.Nil(t, result, errMsgExpectedNilFmt, tt.errStr)
return
}
require.NotNil(t, result, errMsgExpectedNonNilFmt, tt.errStr)
assert.Equal(t, tt.expected, result, "Extracted hex data mismatch for input: %q", tt.errStr)
})
}
}
func TestExtractBytesArrayRevertData(t *testing.T) {
t.Parallel()
tests := []struct {
name string
errStr string
expected []byte
shouldMatch bool
}{
{
name: "valid bytes array",
errStr: "[[8 195 121 160]]",
expected: []byte{8, 195, 121, 160},
shouldMatch: true,
},
{
name: "bytes array with whitespace variations",
errStr: "[[8 195\t121\n160]]",
expected: []byte{8, 195, 121, 160},
shouldMatch: true,
},
{
name: "bytes array with invalid values - skips invalid",
errStr: "[[8 abc 195 256 -1 121]]",
expected: []byte{8, 195, 121},
shouldMatch: true,
},
{
name: "empty brackets",
errStr: "[[]]",
expected: nil,
shouldMatch: false,
},
{
name: "all invalid values",
errStr: "[[abc xyz 999]]",
expected: nil,
shouldMatch: false,
},
{
name: "no brackets",
errStr: "8 195 121 160",
expected: nil,
shouldMatch: false,
},
{
name: "incomplete brackets",
errStr: "[[8 195 121",
expected: nil,
shouldMatch: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := extractBytesArrayRevertData(tt.errStr)
if !tt.shouldMatch {
assert.Nil(t, result, errMsgExpectedNilFmt, tt.errStr)
return
}
require.NotNil(t, result, errMsgExpectedNonNilFmt, tt.errStr)
assert.Equal(t, tt.expected, result, "Extracted bytes mismatch for input: %q", tt.errStr)
})
}
}
func TestExtractCustomErrorRevertData(t *testing.T) {
t.Parallel()
tests := []struct {
name string
errStr string
expected *CustomErrorData
shouldMatch bool
}{
{
name: "valid custom error",
errStr: errMsgCustomError,
expected: &CustomErrorData{
Selector: CallRevertedSelector,
Data: common.FromHex("0xaabbccdd"),
},
shouldMatch: true,
},
{
name: "mixed case hex with spaces in data",
errStr: "execution reverted: custom error 0xAbCd1234: aa bb cc dd",
expected: &CustomErrorData{
Selector: [4]byte{0xAB, 0xCD, 0x12, 0x34},
Data: common.FromHex("0xaabbccdd"),
},
shouldMatch: true,
},
{
name: "with ellipsis truncation",
errStr: "execution reverted: custom error 0x70de1b4b: aabbcc…ddeeff",
expected: &CustomErrorData{
Selector: CallRevertedSelector,
Data: common.FromHex("0xaabbcc"),
},
shouldMatch: true,
},
{
name: "stops at non-hex character",
errStr: "execution reverted: custom error 0x12345678: aabbccddxyz",
expected: &CustomErrorData{
Selector: [4]byte{0x12, 0x34, 0x56, 0x78},
Data: common.FromHex("0xaabbccdd"),
},
shouldMatch: true,
},
{
name: descriptionEmptyString,
errStr: "",
expected: nil,
shouldMatch: false,
},
{
name: "no custom error pattern",
errStr: "execution reverted: some other error",
expected: nil,
shouldMatch: false,
},
{
name: "invalid selector length",
errStr: "execution reverted: custom error 0x123: aabbccdd",
expected: nil,
shouldMatch: false,
},
{
name: "no data after colon",
errStr: "execution reverted: custom error 0x12345678:",
expected: nil,
shouldMatch: false,
},
{
name: "invalid hex in data",
errStr: "execution reverted: custom error 0x12345678: xyz",
expected: nil,
shouldMatch: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := extractCustomErrorRevertData(tt.errStr)
if !tt.shouldMatch {
assert.Nil(t, result, errMsgExpectedNilFmt, tt.errStr)
return
}
require.NotNil(t, result, errMsgExpectedNonNilFmt, tt.errStr)
assert.Equal(t, tt.expected.Selector, result.Selector, "Selector mismatch for input: %q", tt.errStr)
assert.Equal(t, tt.expected.Data, result.Data, "Data mismatch for input: %q", tt.errStr)
})
}
}
func TestParseBytesFromString(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input string
expected []byte
}{
{
name: "valid bytes string",
input: "8 195 121 160",
expected: []byte{8, 195, 121, 160},
},
{
name: "with whitespace variations",
input: " 8\t195\n121 160 ",
expected: []byte{8, 195, 121, 160},
},
{
name: "edge values",
input: "0 128 255",
expected: []byte{0, 128, 255},
},
{
name: descriptionEmptyString,
input: "",
expected: nil,
},
{
name: "only whitespace",
input: " \t\n ",
expected: nil,
},
{
name: "invalid values skipped",
input: "8 abc 121 256 -1 160",
expected: []byte{8, 121, 160},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := parseBytesFromString(tt.input)
assert.Equal(t, tt.expected, result, "Parsed bytes mismatch for input: %q", tt.input)
})
}
}
func TestExtractRevertReasonFromError(t *testing.T) {
t.Parallel()
tests := []struct {
name string
err error
expectedRawData []byte
hasCustomError bool
expectedSelector [4]byte
shouldHaveData bool
}{
{
name: "nil error",
err: nil,
expectedRawData: nil,
hasCustomError: false,
shouldHaveData: false,
},
{
name: "custom error format - highest priority",
err: errors.New(errMsgCustomError),
expectedRawData: append(CallRevertedSelector[:], common.FromHex("0xaabbccdd")...),
hasCustomError: true,
expectedSelector: CallRevertedSelector,
shouldHaveData: true,
},
{
name: "CallReverted format with bytes array",
err: errors.New("contract error: error -`CallReverted` args [[8 195 121 160]]"),
expectedRawData: []byte{8, 195, 121, 160},
hasCustomError: false,
shouldHaveData: true,
},
{
name: "hex-encoded format",
err: errors.New(errMsgExecutionRevertedHex),
expectedRawData: common.FromHex("0x1234567890abcdef"),
hasCustomError: false,
shouldHaveData: true,
},
{
name: "bytes array format (without CallReverted)",
err: errors.New("error with bytes [[8 195 121 160]]"),
expectedRawData: []byte{8, 195, 121, 160},
hasCustomError: false,
shouldHaveData: true,
},
{
name: "plain error without revert data",
err: errors.New("some generic error"),
expectedRawData: nil,
hasCustomError: false,
shouldHaveData: false,
},
{
name: "invalid formats fall through",
err: errors.New("CallReverted but no bytes array"),
expectedRawData: nil,
hasCustomError: false,
shouldHaveData: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := extractRevertReasonFromError(tt.err)
if !tt.shouldHaveData {
assert.Nil(t, result.RawData, "Expected nil RawData for error: %v", tt.err)
assert.Nil(t, result.CustomError, "Expected nil CustomError for error: %v", tt.err)
return
}
require.NotNil(t, result.RawData, "Expected non-nil RawData for error: %v", tt.err)
assert.Equal(t, tt.expectedRawData, result.RawData, "RawData mismatch for error: %v", tt.err)
if tt.hasCustomError {
require.NotNil(t, result.CustomError, "Expected non-nil CustomError for error: %v", tt.err)
assert.Equal(t, tt.expectedSelector, result.CustomError.Selector, "Selector mismatch for error: %v", tt.err)
// Verify Combined() matches RawData
assert.Equal(t, result.RawData, result.CustomError.Combined(), "CustomError.Combined() should match RawData")
} else {
assert.Nil(t, result.CustomError, "Expected nil CustomError for error: %v", tt.err)
}
})
}
}
func TestDecodeRevertReasonFromCustomError(t *testing.T) {
t.Parallel()
// Error(string) selector: keccak256("Error(string)")[:4] = 0x08c379a0
errorStringSelector := [4]byte{0x08, 0xc3, 0x79, 0xa0}
tests := []struct {
name string
customErr *CustomErrorData
expectedResult string
shouldDecode bool
description string
}{
{
name: "nil CustomErrorData",
customErr: nil,
expectedResult: "",
shouldDecode: false,
description: "Should return empty string for nil input",
},
{
name: "CallReverted with empty data",
customErr: &CustomErrorData{
Selector: CallRevertedSelector,
Data: []byte{},
},
expectedResult: "",
shouldDecode: false,
description: "CallReverted with empty data",
},
{
name: "CallReverted with valid data",
customErr: &CustomErrorData{
Selector: CallRevertedSelector,
Data: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
},
expectedResult: "CallReverted(truncated)",
shouldDecode: true,
description: "CallReverted with insufficient data returns truncated marker",
},
{
name: "Error(string) with valid string data",
customErr: &CustomErrorData{
Selector: errorStringSelector,
Data: encodeErrorString("test error message"),
},
expectedResult: "test error message",
shouldDecode: true,
description: "Error(string) decoding with proper ABI encoding",
},
{
name: "Error(string) with empty string",
customErr: &CustomErrorData{
Selector: errorStringSelector,
Data: encodeErrorString(""),
},
expectedResult: "",
shouldDecode: false,
description: "Error(string) with empty string",
},
{
name: "unknown selector",
customErr: &CustomErrorData{
Selector: [4]byte{0xFF, 0xFF, 0xFF, 0xFF},
Data: []byte{0x12, 0x34, 0x56, 0x78},
},
expectedResult: "",
shouldDecode: false,
description: "Unknown selector should return empty string",
},
{
name: "Error(string) with malformed data",
customErr: &CustomErrorData{
Selector: errorStringSelector,
Data: []byte{0x00, 0x00, 0x00, 0x00},
},
expectedResult: "",
shouldDecode: false,
description: "Error(string) with malformed/incomplete data should fail",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := decodeRevertReasonFromCustomError(tt.customErr)
if !tt.shouldDecode {
assert.Empty(t, result, "Expected empty result for: %s. Got: %q", tt.description, result)
} else {
assert.NotEmpty(t, result, "Expected non-empty result for: %s", tt.description)
if tt.expectedResult != "" {
assert.Equal(t, tt.expectedResult, result, "Decoded result mismatch for: %s", tt.description)
}
}
})
}
}
func TestFormatDecodedError(t *testing.T) {
t.Parallel()
tests := []struct {
name string
errorName string
decodedValues []any
expectedResult string
}{
{
name: "empty error name",
errorName: "",
decodedValues: []any{"arg1", "arg2"},
expectedResult: "",
},
{
name: "no arguments",
errorName: "InsufficientSigners",
decodedValues: []any{},
expectedResult: "InsufficientSigners",
},
{
name: "single argument",
errorName: "OutOfBoundsGroup",
decodedValues: []any{uint8(255)},
expectedResult: "OutOfBoundsGroup(255)",
},
{
name: "multiple arguments",
errorName: "CustomError",
decodedValues: []any{"arg1", 123, true},
expectedResult: "CustomError(arg1, 123, true)",
},
{
name: "with nil values - nil skipped",
errorName: "ErrorWithNil",
decodedValues: []any{"arg1", nil, "arg2"},
expectedResult: "ErrorWithNil(arg1, arg2)",
},
{
name: "all nil values",
errorName: "ErrorWithAllNil",
decodedValues: []any{nil, nil},
expectedResult: "ErrorWithAllNil",
},
{
name: "address argument",
errorName: "AddressError",
decodedValues: []any{common.HexToAddress("0x1234567890123456789012345678901234567890")},
expectedResult: "AddressError(0x1234567890123456789012345678901234567890)",
},
{
name: "big int argument",
errorName: "BigIntError",
decodedValues: []any{big.NewInt(1000)},
expectedResult: "BigIntError(1000)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := formatDecodedError(tt.errorName, tt.decodedValues)
assert.Equal(t, tt.expectedResult, result, "Format mismatch for errorName=%q, values=%v", tt.errorName, tt.decodedValues)
})
}
}
func TestExtractRevertDataFromCallError(t *testing.T) {
t.Parallel()
tests := []struct {
name string
err error
expected []byte
shouldMatch bool
}{
{
name: "nil error",
err: nil,
expected: nil,
shouldMatch: false,
},
{
name: "error with hex-encoded revert data",
err: errors.New(errMsgExecutionRevertedHex),
expected: common.FromHex("0x1234567890abcdef"),
shouldMatch: true,
},
{
name: "error with bytes array format",
err: errors.New("execution reverted: CallReverted args [[8 195 121 160]]"),
expected: []byte{8, 195, 121, 160},
shouldMatch: true,
},
{
name: "error without revert data",
err: errors.New("some generic error"),
expected: nil,
shouldMatch: false,
},
{
name: "error with both formats - hex takes priority",
err: errors.New("execution reverted: 0x12345678 and bytes [[8 195 121]]"),
expected: common.FromHex("0x12345678"),
shouldMatch: true,
},
}