-
Notifications
You must be signed in to change notification settings - Fork 1
/
NewError.go
60 lines (50 loc) · 1.16 KB
/
NewError.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
package fibertools
import (
"fmt"
"runtime"
"github.com/gofiber/fiber/v2"
)
type RichError struct {
Stack []RichStack
Message string
Code int
}
type RichStack struct {
PCName string
Function string
Line int
}
func (e *RichError) Error() string {
return e.Message
}
func (e *RichError) StackTrace() []string {
var messages []string
for _, v := range e.Stack {
messages = append(messages, fmt.Sprintf("[Error] in %s[%s:%d] %s", v.PCName, v.Function, v.Line, e.Message))
}
return messages
}
// Error function that can be returned on general errors || panics in order to get stack trace in error handler. Must be used in tandom with fibertools.Recover() for most value.
func NewError(err error) *RichError {
richErr := RichError{
Message: err.Error(),
}
maxStackAmount := 6
for i := 1; i < maxStackAmount; i++ {
pc, fn, line, exists := runtime.Caller(i)
if exists {
richErr.Stack = append(richErr.Stack, RichStack{
PCName: runtime.FuncForPC(pc).Name(),
Function: fn,
Line: line,
})
}
}
fiberErr, ok := err.(*fiber.Error)
if !ok {
richErr.Code = 500
} else {
richErr.Code = fiberErr.Code
}
return &richErr
}