-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetails.go
More file actions
114 lines (102 loc) · 1.85 KB
/
details.go
File metadata and controls
114 lines (102 loc) · 1.85 KB
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
112
113
114
/**
* Copyright 2019 Innodev LLC. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/
package errors
import (
"fmt"
"runtime"
"github.com/jinzhu/copier"
)
func _copy(e interface{}) *Err {
switch err := e.(type) {
case *Err:
if err == nil {
return nil
}
return _copy(*err)
case Err:
out := new(Err)
copier.Copy(&out, err)
if err.Meta != nil {
out.Meta = map[string]interface{}{}
for k, v := range err.Meta {
out.Meta[k] = v
}
}
if err.Stack != nil {
out.Stack = make([]Frame, len(err.Stack))
for i := range out.Stack {
copier.Copy(&out.Stack[i], err.Stack[i])
}
}
return out
}
return nil
}
func _new(e interface{}, depth int) Error {
if e == nil {
return nil
}
var msg string
switch val := e.(type) {
case *Err:
return _copy(*val)
case error:
msg = val.Error()
default:
msg = fmt.Sprintf("%v", e)
}
out := &Err{
Meta: map[string]interface{}{},
Message: msg,
}
if depth != -1 {
out.Stack = getStack(depth)
}
return out
}
func _wrap(err error, message string) Error {
if err == nil {
return nil
}
out := &Err{
Message: message,
}
if e, ok := err.(*Err); ok {
cpy := _copy(e)
out.Err = cpy
out.Stack = cpy.Stack
out.Meta = cpy.Meta
} else {
out.Stack = getStack(2)
}
return out
}
func getStack(skip int) []Frame {
pcs := make([]uintptr, StackBufferSize)
length := runtime.Callers(2+skip, pcs)
if length == 0 {
return nil
}
pcs = pcs[:length]
frames := runtime.CallersFrames(pcs)
out := make([]Frame, 0, length)
for {
frame, more := frames.Next()
if !more {
break
}
fn := frame.Func.Name()
if fn == "runtime.main" || fn == "runtime.goexit" {
continue
}
out = append(out, Frame{
Function: fn,
File: frame.File,
Line: frame.Line,
})
}
return out
}