-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
60 lines (55 loc) · 1.1 KB
/
errors.go
File metadata and controls
60 lines (55 loc) · 1.1 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
package util
import (
"fmt"
"log"
"os"
"strings"
"sync"
)
var occurred = map[string]int{}
var l sync.Mutex
// This should be moved to log package
func CheckErr(err error, tolerate ...string) {
if err != nil {
errStr := strings.ToLower(err.Error())
for _, tol := range tolerate {
tol = strings.ToLower(tol)
if strings.Contains(errStr, tol) {
l.Lock()
occurred[err.Error()]++
l.Unlock()
if occurred[err.Error()] < 2 {
log.Printf("tolerated error: %v", err)
}
return
}
}
log.Printf("%v", err)
str := StackTraceStr(1, 4, 3)
log.Printf("\n\t%s\n", str)
os.Exit(1)
}
}
// This should be moved to log package
func BubbleUp(err error, tolerate ...string) {
if err != nil {
panic(fmt.Sprintf("%v", err))
}
}
func SqlAlreadyExists(e error) bool {
if e != nil {
canTolerate := []string{
"Duplicate entry",
"UNIQUE constraint failed",
"duplicate key value violates",
}
errStr := strings.ToLower(e.Error())
for _, tol := range canTolerate {
tol = strings.ToLower(tol)
if strings.Contains(errStr, tol) {
return true
}
}
}
return false
}