-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoerr.go
111 lines (90 loc) · 2.5 KB
/
goerr.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package stackerr
import (
"fmt"
"log"
"net/http"
"os"
"runtime"
"strings"
)
// Err error
type Err struct {
Message string
StatusCode int
stackCount int
stacktrace *Stack
}
func (e *Err) Error() string {
return e.Message
}
// New - creates a new Err struct with 500 Error Code
func New(message string) *Err {
err := Err{
Message: message,
StatusCode: http.StatusInternalServerError,
}
return err.Stack()
}
// NewWithStatusCode - creates a new Err struct with a custom Error Code
func NewWithStatusCode(message string, statusCode int) *Err {
err := Err{
Message: message,
StatusCode: statusCode,
}
return err.Stack()
}
// NewFromError - creates a new Err struct with a custom Error Code
func NewFromError(e error) *Err {
err := Err{
Message: e.Error(),
StatusCode: http.StatusInternalServerError,
}
return err.Stack()
}
// IsNotFound - return true if the error type is resource not found.
func (e *Err) IsNotFound() bool {
return e.StatusCode == http.StatusNotFound
}
// Stack - Adds the current function to Err and a link to the previous Stacktrace
func (e *Err) Stack() *Err {
pc := make([]uintptr, 10) // at least 1 entry needed
runtime.Callers(2, pc)
f := runtime.FuncForPC(pc[1])
file, line := f.FileLine(pc[1])
if os.Getenv("GOPATH") != "" {
file = strings.Replace(file, os.Getenv("GOPATH")+"/src/", "", -1)
}
e.stacktrace = &Stack{File: file, Function: getFunctionName(f), Line: line, CallbackStack: e.stacktrace}
e.stackCount++
return e
}
// StackWithContext - Adds the current function and context to Err and a link to the previous Stacktrace
func (e *Err) StackWithContext(context string) *Err {
pc := make([]uintptr, 10) // at least 1 entry needed
runtime.Callers(2, pc)
f := runtime.FuncForPC(pc[1])
file, line := f.FileLine(pc[1])
if os.Getenv("GOPATH") != "" {
file = strings.Replace(file, os.Getenv("GOPATH")+"/src/", "", -1)
}
e.stacktrace.Context = context
e.stacktrace = &Stack{File: file, Function: getFunctionName(f), Line: line, CallbackStack: e.stacktrace}
e.stackCount++
return e
}
// Sprint - returns a pretty printed string of the Stacktrace ready for printng
func (e *Err) Sprint() string {
return e.stacktrace.Sprint()
}
// Print - prints the Stacktrace
func (e *Err) Print() {
fmt.Print(e.stacktrace.Sprint())
}
// Log - logs the Stacktrace using the native log package
func (e *Err) Log() {
log.Print(e.stacktrace.Sprint())
}
func getFunctionName(f *runtime.Func) string {
parts := strings.Split(f.Name(), "/")
return parts[len(parts)-1]
}