-
Notifications
You must be signed in to change notification settings - Fork 42
/
exception.go
78 lines (67 loc) · 1.3 KB
/
exception.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
package exception
import (
"fmt"
log "github.com/Lafeng/deblocus/golang/glog"
"runtime"
)
// injection
var DEBUG bool
type Exception struct {
msg string
code int
warning bool
}
func (e *Exception) Error() string {
return e.msg
}
func (e *Exception) Code() int {
return e.code
}
func (e *Exception) Warning() bool {
return e.warning
}
func (e *Exception) Apply(appendage interface{}) *Exception {
newE := new(Exception)
newE.code = e.code
newE.msg = fmt.Sprintf("%s %v", e.msg, appendage)
return newE
}
func NewW(msg string) *Exception {
return &Exception{msg: msg, warning: true}
}
func New(code int, msg string) *Exception {
return &Exception{msg: msg, code: code}
}
func CatchException(e interface{}) bool {
if e != nil {
if s, y := e.(string); y {
log.Warningln(s)
} else if ex, y := e.(*Exception); y && ex.warning {
log.Errorln(ex.msg)
return true
} else {
log.Errorln(e)
}
if DEBUG || bool(log.V(3)) {
buf := make([]byte, 1600)
n := runtime.Stack(buf, false)
log.DirectPrintln(string(buf[:n]))
}
return true
}
return false
}
func ErrorOf(e interface{}) (err error, ie bool) {
if e == nil {
return nil, false
} else {
ie = true
}
switch e.(type) {
case error:
err = e.(error)
default:
err = &Exception{msg: fmt.Sprintf("%v", e)}
}
return
}