-
Notifications
You must be signed in to change notification settings - Fork 1
/
errf.go
46 lines (39 loc) · 1.04 KB
/
errf.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
package hexa
import (
"github.com/kamva/hexa/hlog"
"github.com/kamva/tracer"
)
// ErrFields checks if the provided error is a Hexa error, returns
// hexa error fields, otherwise returns regular error fields.
func ErrFields(err error) []hlog.Field {
if hexaErrFields := hexaErrFields(err); len(hexaErrFields) != 0 {
return hexaErrFields
}
return []hlog.Field{
hlog.Err(err),
hlog.ErrStack(tracer.Trace(err)),
}
}
func hexaErrFields(err error) []hlog.Field {
e := AsHexaErr(err)
if e == nil {
return nil
}
// Hexa error fields:
fields := []hlog.Field{
hlog.String("_error_id", e.ID()),
hlog.Int("_http_status", e.HTTPStatus()),
}
for k, v := range e.Data() {
fields = append(fields, hlog.Any(k, v))
}
for k, v := range e.ReportData() {
fields = append(fields, hlog.Any(k, v))
}
// If exists error and error is traced,print its stack.
fields = append(fields, hlog.ErrStack(tracer.MoveStackIfNeeded(e, e.InternalError())))
if e.InternalError() != nil {
fields = append(fields, hlog.Err(e.InternalError()))
}
return fields
}