• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

go-playground / log / 5885156600

17 Aug 2023 12:15AM UTC coverage: 82.235% (-0.3%) from 82.521%
5885156600

Pull #54

github

deankarn
fix errors dep + tests
Pull Request #54: fix errors dep + tests

574 of 698 relevant lines covered (82.23%)

10.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.02
/errors.go
1
package log
2

3
import (
4
        "fmt"
5
        "github.com/go-playground/errors/v5"
6
        "strconv"
7
        "strings"
8

9
        runtimeext "github.com/go-playground/pkg/v5/runtime"
10
)
11

12
func errorsWithError(e Entry, err error) Entry {
5✔
13
        frame := runtimeext.StackLevel(2)
5✔
14

5✔
15
        switch t := err.(type) {
5✔
16
        case errors.Chain:
3✔
17
                types := make([]byte, 0, 32)
3✔
18
                tags := make([]Field, 0, len(t))
3✔
19
                dedupeTags := make(map[string]bool)
3✔
20
                dedupeType := make(map[string]bool)
3✔
21
                errorBuff := BytePool().Get()
3✔
22
                for _, e := range t {
9✔
23
                        errorBuff.B = formatLink(e, errorBuff.B)
6✔
24
                        errorBuff.B = append(errorBuff.B, ' ')
6✔
25

6✔
26
                        for _, tag := range e.Tags {
8✔
27
                                key := fmt.Sprintf("%s-%v", tag.Key, tag.Value)
2✔
28
                                if dedupeTags[key] {
2✔
29
                                        continue
×
30
                                }
31
                                dedupeTags[key] = true
2✔
32
                                tags = append(tags, Field{Key: tag.Key, Value: tag.Value})
2✔
33
                        }
34
                        for _, typ := range e.Types {
10✔
35
                                if dedupeType[typ] {
4✔
36
                                        continue
×
37
                                }
38
                                dedupeType[typ] = true
4✔
39
                                types = append(types, typ...)
4✔
40
                                types = append(types, ',')
4✔
41
                        }
42
                }
43

44
                sourceBuff := BytePool().Get()
3✔
45
                sourceBuff.B = extractSource(sourceBuff.B, frame)
3✔
46
                e.Fields = append(e.Fields, Field{Key: "source", Value: string(sourceBuff.B[:len(sourceBuff.B)-1])})
3✔
47
                BytePool().Put(sourceBuff)
3✔
48
                e.Fields = append(e.Fields, Field{Key: "error", Value: string(errorBuff.B[:len(errorBuff.B)-1])})
3✔
49
                BytePool().Put(errorBuff)
3✔
50

3✔
51
                e.Fields = append(e.Fields, tags...) // we do it this way to maintain order of error, source as first fields
3✔
52
                if len(types) > 0 {
5✔
53
                        e.Fields = append(e.Fields, Field{Key: "types", Value: string(types[:len(types)-1])})
2✔
54
                }
2✔
55

56
        default:
2✔
57
                errorBuff := BytePool().Get()
2✔
58
                errorBuff.B = extractSource(errorBuff.B, frame)
2✔
59
                errorBuff.B = append(errorBuff.B, err.Error()...)
2✔
60
                e.Fields = append(e.Fields, Field{Key: "error", Value: string(errorBuff.B)})
2✔
61
                BytePool().Put(errorBuff)
2✔
62
        }
63
        return e
5✔
64
}
65

66
func extractSource(b []byte, source runtimeext.Frame) []byte {
11✔
67
        var funcName string
11✔
68

11✔
69
        idx := strings.LastIndexByte(source.Frame.Function, '.')
11✔
70
        if idx == -1 {
11✔
71
                b = append(b, source.File()...)
×
72
        } else {
11✔
73
                funcName = source.Frame.Function[idx+1:]
11✔
74
                remaining := source.Frame.Function[:idx]
11✔
75

11✔
76
                idx = strings.LastIndexByte(remaining, '/')
11✔
77
                if idx > -1 {
22✔
78
                        b = append(b, source.Frame.Function[:idx+1]...)
11✔
79
                        remaining = source.Frame.Function[idx+1:]
11✔
80
                }
11✔
81

82
                idx = strings.IndexByte(remaining, '.')
11✔
83
                if idx == -1 {
11✔
84
                        b = append(b, remaining...)
×
85
                } else {
11✔
86
                        b = append(b, remaining[:idx]...)
11✔
87
                }
11✔
88
                b = append(b, '/')
11✔
89
                b = append(b, source.File()...)
11✔
90
        }
91
        b = append(b, ':')
11✔
92
        b = strconv.AppendInt(b, int64(source.Line()), 10)
11✔
93
        if funcName != "" {
22✔
94
                b = append(b, ':')
11✔
95
                b = append(b, funcName...)
11✔
96
        }
11✔
97
        b = append(b, ' ')
11✔
98
        return b
11✔
99
}
100

101
func formatLink(l *errors.Link, b []byte) []byte {
6✔
102
        b = extractSource(b, l.Source)
6✔
103
        if l.Prefix != "" {
9✔
104
                b = append(b, l.Prefix...)
3✔
105
        }
3✔
106
        if l.Err != nil {
9✔
107
                if l.Prefix != "" {
3✔
108
                        b = append(b, ": "...)
×
109
                }
×
110
                b = append(b, l.Err.Error()...)
3✔
111
        }
112
        return b
6✔
113
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc