Repository navigation
Expand file tree
/
Copy patherrors.go
More file actions
50 lines (43 loc) · 1.51 KB
/
Copy patherrors.go
File metadata and controls
50 lines (43 loc) · 1.51 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
package common
import (
"regexp"
"strings"
)
const maxRangeMatchGroups = 2
var (
// Matches "block range too large, max range: 1000"
reMaxRange = regexp.MustCompile(`block range too large, max range:\s*(\d+)`)
// Matches "exceeded maximum block range: 5000"
reExceededBlockRange = regexp.MustCompile(`exceeded maximum block range:\s*(\d+)`)
// Matches "eth_getLogs is limited to a 10,000 range" (number may contain comma thousands separators)
reEthGetLogsLimited = regexp.MustCompile(`eth_getLogs is limited to a\s+([\d,]+)\s+range`)
// Matches "query exceeds max block range 100000"
reQueryExceedsMaxBlockRange = regexp.MustCompile(`query exceeds max block range\s+(\d+)`)
)
// ParseMaxRangeFromError extracts the max range value from error message
// Expected formats:
// - "block range too large, max range: 1000"
// - "exceeded maximum block range: 5000"
// - "eth_getLogs is limited to a 10,000 range"
func ParseMaxRangeFromError(errMsg string) (uint64, bool) {
var matches []string
for _, re := range []*regexp.Regexp{reMaxRange,
reExceededBlockRange,
reEthGetLogsLimited,
reQueryExceedsMaxBlockRange} {
matches = re.FindStringSubmatch(errMsg)
if len(matches) >= maxRangeMatchGroups {
break
}
}
if len(matches) < maxRangeMatchGroups {
return 0, false
}
// Strip comma thousands separators (e.g. "10,000" -> "10000") before parsing
numStr := strings.ReplaceAll(matches[1], ",", "")
maxRange, err := ParseUint64HexOrDecimal(numStr)
if err != nil {
return 0, false
}
return maxRange, true
}