forked from gemnasium/logrus-graylog-hook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
55 lines (46 loc) · 1.06 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
package graylog
import (
"encoding/json"
"runtime"
"github.com/pkg/errors"
)
// newMarshalableError builds an error which encodes its error message into JSON
func newMarshalableError(err error) *marshalableError {
return &marshalableError{err}
}
// a marshalableError is an error that can be encoded into JSON
type marshalableError struct {
err error
}
// MarshalJSON implements json.Marshaler for marshalableError
func (m *marshalableError) MarshalJSON() ([]byte, error) {
return json.Marshal(m.err.Error())
}
type causer interface {
Cause() error
}
type stackTracer interface {
StackTrace() errors.StackTrace
}
func extractStackTrace(err error) errors.StackTrace {
var tracer stackTracer
for {
if st, ok := err.(stackTracer); ok {
tracer = st
}
if cause, ok := err.(causer); ok {
err = cause.Cause()
continue
}
break
}
if tracer == nil {
return nil
}
return tracer.StackTrace()
}
func extractFileAndLine(stacktrace errors.StackTrace) (string, int) {
pc := uintptr(stacktrace[0])
fn := runtime.FuncForPC(pc)
return fn.FileLine(pc)
}