-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
75 lines (66 loc) · 1.59 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
package quick
import (
"errors"
"fmt"
"runtime"
"strconv"
"time"
)
var (
ErrRequestBody = errors.New("request encode can`t coexists with PostForm")
ErrTimeout = errors.New("reqeust timeout")
)
type RedirectError struct {
RedirectNum int
}
func (e *RedirectError) Error() string {
return "exceeded the maximum number of redirects: " + strconv.Itoa(e.RedirectNum)
}
type Error struct {
// wrapped error
err error
msg string
// file path and name
file string
fileLine int
time string
}
func (e *Error) Error() string {
_, ok := e.err.(interface {
Unwrap() error
})
if ok {
return fmt.Sprintf("%s - %s:%d\n%s\n%s", e.time, e.file, e.fileLine, e.msg, e.err.Error())
}
return fmt.Sprintf("%s - %s:%d\n%s\n\n%s\n", e.time, e.file, e.fileLine, e.msg, e.err.Error())
}
func (e *Error) Unwrap() error {
if e.err != nil {
return e.err
}
return nil
}
// WrapErr will wrap a error with some information: filename, line, time and some message.
func WrapErr(err error, msg string) error {
_, file, line, _ := runtime.Caller(1)
return &Error{
err: err,
msg: msg,
file: file,
fileLine: line,
time: time.Now().Format("2006-01-02 15:04:05"),
}
}
// WrapErr will wrap a error with some information: filename, line, time and some message.
// You can format message of error.
func WrapErrf(err error, format string, args ...interface{}) error {
msg := fmt.Sprintf(format, args...)
_, file, line, _ := runtime.Caller(1)
return &Error{
err: err,
msg: msg,
file: file,
fileLine: line,
time: time.Now().Format("2006-01-02 15:04:05"),
}
}