-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy patherror.go
86 lines (81 loc) · 3.16 KB
/
error.go
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
package devstatscode
import (
"fmt"
"os"
"runtime/debug"
"strings"
"time"
"github.com/lib/pq"
)
// FatalOnError displays error message (if error present) and exits program
func FatalOnError(err error) string {
if err != nil {
tm := time.Now()
switch e := err.(type) {
case *pq.Error:
errName := e.Code.Name()
if errName == "too_many_connections" {
fmt.Fprintf(os.Stderr, "PqError: code=%s, name=%s, detail=%s\n", e.Code, errName, e.Detail)
fmt.Fprintf(os.Stderr, "Warning: too many postgres connections: %+v: '%s'\n", tm, err.Error())
return Retry
} else if errName == "cannot_connect_now" {
fmt.Fprintf(os.Stderr, "PqError: code=%s, name=%s, detail=%s\n", e.Code, errName, e.Detail)
fmt.Fprintf(os.Stderr, "Warning: DB shutting down: %+v: '%s', sleeping 15 minutes to settle\n", tm, err.Error())
time.Sleep(time.Duration(900) * time.Second)
tm = time.Now()
fmt.Fprintf(os.Stderr, "Warning: DB shutting down: %+v: '%s', waited 15 minutes, retrying\n", tm, err.Error())
return Reconnect
}
Printf("PqError: code=%s, name=%s, detail=%s\n", e.Code, errName, e.Detail)
fmt.Fprintf(os.Stderr, "PqError: code=%s, name=%s, detail=%s\n", e.Code, errName, e.Detail)
if os.Getenv("DURABLE_PQ") != "" && os.Getenv("DURABLE_PQ") != "0" && os.Getenv("DURABLE_PQ") != "false" {
switch errName {
case "program_limit_exceeded", "undefined_column", "invalid_catalog_name", "character_not_in_repertoire":
Printf("%s error is not retryable, even with DURABLE_PQ\n", errName)
default:
fmt.Fprintf(os.Stderr, "retrying with DURABLE_PQ\n")
return Reconnect
}
}
default:
fmt.Fprintf(os.Stderr, "ErrorType: %T, error: %+v\n", e, e)
fmt.Fprintf(os.Stderr, "ErrorType: %T, error: %+v\n", e, e)
}
if strings.Contains(err.Error(), "driver: bad connection") {
fmt.Fprintf(os.Stderr, "Warning: bad driver, retrying\n")
return Reconnect
}
if strings.Contains(err.Error(), "cannot assign requested address") {
fmt.Fprintf(os.Stderr, "Warning: cannot assign requested address, retrying in 15 minutes\n")
time.Sleep(time.Duration(900) * time.Second)
fmt.Fprintf(os.Stderr, "Warning: cannot assign requested address - waited 15 minutes, retrying\n")
return Reconnect
}
/*
if strings.Contains(err.Error(), "database is closed") {
Printf("Warning: database is closed, retrying\n")
return Reconnect
}
*/
fmt.Fprintf(os.Stderr, "Error(time=%+v):\nError: '%s'\nStacktrace:\n%s\n", tm, err.Error(), string(debug.Stack()))
if os.Getenv("NO_FATAL_DELAY") == "" {
time.Sleep(time.Duration(60) * time.Second)
}
panic(fmt.Sprintf("stacktrace: %+v", err))
}
return OK
}
// Fatalf - it will call FatalOnError using fmt.Errorf with args provided
func Fatalf(f string, a ...interface{}) {
FatalOnError(fmt.Errorf(f, a...))
}
// FatalNoLog displays error message (if error present) and exits program, should be used for very early init state
func FatalNoLog(err error) string {
if err != nil {
tm := time.Now()
fmt.Fprintf(os.Stderr, "Error(time=%+v):\nError: '%s'\nStacktrace:\n", tm, err.Error())
time.Sleep(time.Duration(60) * time.Second)
panic(fmt.Sprintf("stacktrace: %+v", err))
}
return OK
}