-
Notifications
You must be signed in to change notification settings - Fork 72
/
error_handler.go
79 lines (66 loc) · 2.11 KB
/
error_handler.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
package ghostferry
import (
"encoding/json"
"fmt"
"net/http"
"os"
"sync/atomic"
"github.com/sirupsen/logrus"
)
type ErrorHandler interface {
// Usually called from Fatal. When called from Fatal, if this method returns
// true, Fatal should panic, otherwise it should not.
ReportError(from string, err error)
Fatal(from string, err error)
}
type PanicErrorHandler struct {
Ferry *Ferry
ErrorCallback HTTPCallback
DumpStateToStdoutOnError bool
errorCount int32
}
func (this *PanicErrorHandler) ReportError(from string, err error) {
logger := logrus.WithField("tag", "error_handler")
stateJSON, jsonErr := this.Ferry.SerializeStateToJSON()
if jsonErr != nil {
logger.WithError(jsonErr).Error("failed to dump state to JSON...")
} else {
if this.DumpStateToStdoutOnError {
fmt.Fprintln(os.Stdout, stateJSON)
}
}
// Invoke ErrorCallback if defined
if this.ErrorCallback != (HTTPCallback{}) {
client := &http.Client{}
errorData := make(map[string]string)
errorData["ErrFrom"] = from
errorData["ErrMessage"] = err.Error()
errorData["StateDump"] = stateJSON
errorDataBytes, jsonErr := json.MarshalIndent(errorData, "", " ")
if jsonErr != nil {
logger.WithField("error", jsonErr).Errorf("ghostferry failed to marshal error data")
} else {
this.ErrorCallback.Payload = string(errorDataBytes)
postErr := this.ErrorCallback.Post(client)
if postErr != nil {
logger.WithField("error", postErr).Errorf("ghostferry failed to notify error")
}
}
}
var errmsg string
if this.DumpStateToStdoutOnError {
errmsg = "fatal error detected, state dump in stdout"
} else {
errmsg = "fatal error detected"
}
// Print error to STDERR
logger.WithError(err).WithField("errfrom", from).Error(errmsg)
}
func (this *PanicErrorHandler) Fatal(from string, err error) {
if atomic.AddInt32(&this.errorCount, 1) > 1 {
logrus.WithField("tag", "error_handler").WithError(err).WithField("errfrom", from).Error("multiple fatal errors detected, not reporting again")
return
}
this.ReportError(from, err)
panic("fatal error detected, see logs for details")
}