/
error.go
89 lines (76 loc) · 1.56 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
87
88
89
package gotracer
import (
"log"
"runtime"
"time"
)
const layout = "Jan 2, 2006 at 3:04pm (MST)"
type Tracer struct {
Dummy bool
EmailHost string
EmailPort string
EmailUsername string
EmailPassword string
EmailSender string
EmailFrom string
ErrorTo string
}
func (self Tracer) Notify(extra ...func(reason string) string) {
err := recover()
if err != nil {
const size = 4096
var exc_message string
buf := make([]byte, size)
buf = buf[:runtime.Stack(buf, false)]
buffer := string(buf)
switch err.(type) {
case string:
_err, ok := err.(string)
if ok == true {
exc_message = _err
}
case interface{}:
_err, ok := err.(error)
if ok == true {
exc_message = _err.Error()
}
}
extras := ""
for i := range extra {
extras += extra[i](exc_message)
extras += " "
}
self.sendException(&ErrorStack{
Subject: exc_message,
Traceback: buffer,
Extra: extras,
Timestamp: time.Now().Format(layout),
})
}
}
func (self Tracer) sendException(stack *ErrorStack) {
Dummy := self.Dummy
if self.EmailHost == "" || self.EmailPassword == "" {
Dummy = true
}
if Dummy {
log.Println(stack.Subject)
log.Println(stack.Traceback)
} else {
log.Println("Sending Exception: " + stack.Subject)
connection := MakeConn(&self)
connection.SenderName += " Exception"
connection.SendEmail(Message{
self.EmailFrom,
[]string{self.ErrorTo},
stack.Subject,
ErrorTemplate(stack),
})
}
}
type ErrorStack struct {
Subject string
Extra string
Traceback string
Timestamp string
}