forked from dorajistyle/goyangi
/
error.go
49 lines (43 loc) · 1.48 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
package log
import (
"fmt"
"runtime"
"github.com/Sirupsen/logrus"
)
// CheckError check error and return true if error is nil and return false if error is not nil.
func CheckError(err error) bool {
return CheckErrorWithMessage(err, "")
}
// CheckError check error and return true if error is nil and return false if error is not nil.
func CheckErrorNoStack(err error) bool {
return CheckErrorNoStackWithMessage(err, "")
}
// CheckErrorWithMessage check error with message and log messages with stack. And then return true if error is nil and return false if error is not nil.
func CheckErrorWithMessage(err error, msg string, args ...interface{}) bool {
if err != nil {
var stack [4096]byte
runtime.Stack(stack[:], false)
// logrus.Errorf(msg, args)
if len(args) == 0 {
logrus.Error(msg + fmt.Sprintf("%q\n%s\n", err, stack[:]))
} else {
logrus.Error(fmt.Sprintf(msg, args...) + fmt.Sprintf("%q\n%s\n", err, stack[:]))
}
// logrus.Printf(msg+"\n%q\n%s\n",args, err, stack[:])
return false
}
return true
}
// CheckErrorNoStackWithMessage check error with message and return true if error is nil and return false if error is not nil.
func CheckErrorNoStackWithMessage(err error, msg string, args ...interface{}) bool {
if err != nil {
if len(args) == 0 {
logrus.Error(msg + fmt.Sprintf("%q\n", err))
} else {
logrus.Error(fmt.Sprintf(msg, args...) + fmt.Sprintf("%q\n", err))
}
// logrus.Printf(msg+"\n%q\n%s\n",args, err, stack[:])
return false
}
return true
}