-
Notifications
You must be signed in to change notification settings - Fork 48
/
handleerror.js
121 lines (104 loc) · 3.81 KB
/
handleerror.js
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
let handleError = (message) => {
/***************************************************************************
*
* adopted from https://github.com/tandrewnichols/extract-json-from-string
*
***************************************************************************/
const extractjson = (str) => {
const jsonify = (almostJson) => {
try {
return JSON.parse(almostJson);
} catch (e) {
almostJson = almostJson.replace(/([a-zA-Z0-9_$]+\s*):/g, '"$1":').replace(/'([^']+?)'([\s,\]\}])/g, '"$1"$2');
return JSON.parse(almostJson);
}
};
const chars = {
'[': ']',
'{': '}'
};
const any = (iteree, iterator) => {
let result;
for (let i = 0; i < iteree.length; i++) {
result = iterator(iteree[i], i, iteree);
if (result) {
break;
}
}
return result;
};
const extract = (str) => {
let startIndex = str.search(/[\{\[]/);
if (startIndex === -1) {
return null;
}
let openingChar = str[ startIndex ];
let closingChar = chars[ openingChar ];
let endIndex = -1;
let count = 0;
str = str.substring(startIndex);
any(str, (letter, i) => {
if (letter === openingChar) {
count++;
} else if (letter === closingChar) {
count--;
}
if (!count) {
endIndex = i;
return true;
}
});
if (endIndex === -1) {
return null;
}
let obj = str.substring(0, endIndex + 1);
return obj;
};
let result;
const objects = [];
while ((result = extract(str)) !== null) {
try {
let obj = jsonify(result);
objects.push(obj);
} catch (e) {
// Do nothing
}
str = str.replace(result, '');
}
return objects;
};
// Error handling logic
let Errors = {
'1': 'require(!config.permanent, "1"); // the contract was made permanent and no updates can be made',
'2': 'require(!config.permanent, "2"); // the contract was made permanent and no updates can be made',
'3': 'require(!withdrawer.permanent, "3"); // the withdrawer has been fixed permanently',
'4': 'require(_msgSender() == owner() || _msgSender() == withdrawer.account, "4"); // only the owner or the withdrawer can call the withdraw() function',
'5': 'require(sent1, "5"); // error while withdrawing funds',
'6': 'require(sent2, "6"); // error while withdrawing funds',
'7': 'require(verify(auth, _msgSender()), "7"); // invalid merkle proof for the invite list',
'8': 'require(i.price * _count == msg.value, "8"); // incorrect ETH amount',
'9': 'require(i.start <= block.timestamp, "9"); // the mint has not started yet for this invite',
'10': 'require(minted[_msgSender()][auth.key] + _count <= i.limit, "10"); // mint limit reached',
'11': 'require(n+_count-1 <= config.supply, "11"); // cannot mint above total supply',
'12': 'require(_count > 0, "12"); // can gift at least one or more',
'13': 'require(n+_count-1 <= config.supply, "13"); // cannot givt above total supply',
'14': 'require(_isApprovedOrOwner(_msgSender(), _tokenId), "14"); // you do not have the authority to burn the token',
'15': 'require(tokenId > 0 && tokenId <= config.supply, "15");',
'16': 'require(!config.permanent, "16"); // the contract was made permanent and no updates can be made'
}
let error = extractjson(message)
if (error.length > 0) {
error = error[0]
if (error.originalError) {
let match = /execution reverted: (.+)$/.exec(error.originalError.message)
if (match && match.length > 0) {
let code = match[1]
let message = `[ERROR ${code}] ${Errors["" + code]}`
error.message = message
}
}
return error.message
} else {
return message
}
}