forked from hoffoo/elasticsearch-dump
-
-
Notifications
You must be signed in to change notification settings - Fork 261
/
verify.go
68 lines (58 loc) · 1.88 KB
/
verify.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
package main
import (
"fmt"
log "github.com/cihub/seelog"
"path/filepath"
"reflect"
"runtime"
"strings"
)
type CheckErrorAction int
const (
ACTION_FATAL_QUIT CheckErrorAction = iota
ACTION_LOG_ERROR
)
//Notice:
// 1. when dev, set to ACTION_FATAL_QUIT, so can check error quickly,
// then can add error logical for the place that once thought could not go wrong
// 2. when released, set to ACTION_LOG_ERROR, so just log error
var verifyAction = ACTION_FATAL_QUIT
// GetCallStackInfo return fileName, lineNo, funName
func GetCallStackInfo(skip int) (string, int, string) {
var funName string
pc, fileName, lineNo, ok := runtime.Caller(skip)
if ok {
funName = strings.TrimPrefix(filepath.Ext(runtime.FuncForPC(pc).Name()), ".")
} else {
funName = "<Unknown>"
fileName, lineNo = "<Unknown>", -1
}
return fileName, lineNo, funName
}
// skip 表示跳过几个调用堆栈, 获取真正有意义的代码调用位置
func checkAndHandleError(err error, msg string, action CheckErrorAction, skip int) {
if err != nil {
fileName, lineNo, funName := GetCallStackInfo(skip)
switch action {
case ACTION_FATAL_QUIT:
log.Errorf("%s:%d: (%s) FAIL(%s), msg=%s", fileName, lineNo, funName, reflect.TypeOf(err).String(), msg)
//log.Fatalf("") //"error at: %s:%d, msg=%s, err=%s", fileName, lineNo, msg, err)
panic(fmt.Sprintf("error at: %s:%d, msg=%s, err=%s", fileName, lineNo, msg, err))
case ACTION_LOG_ERROR:
log.Warnf("%s:%d: (%s) FAIL(%s), msg=%s", fileName, lineNo, funName, reflect.TypeOf(err).String(), msg)
//flog.Infof("error at: %s:%d, msg=%s, err=%s", fileName, lineNo, msg, err)
}
}
}
func Verify(err error) error {
if err != nil {
checkAndHandleError(err, err.Error(), verifyAction, 3)
}
return err
}
func VerifyWithResult(result interface{}, err error) interface{} {
if err != nil {
checkAndHandleError(err, err.Error(), verifyAction, 3)
}
return result
}