-
Notifications
You must be signed in to change notification settings - Fork 2
/
error.go
64 lines (58 loc) · 1.36 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
56
57
58
59
60
61
62
63
64
package nject
import (
"errors"
"sync"
)
type njectError struct {
err error
details string
}
func (ne *njectError) Error() string {
return ne.err.Error()
}
// DetailedError transforms errors into strings. If
// the error happens to be an error returned by Bind()
// or something that called Bind() then it will return
// a much more detailed error than just calling err.Error()
func DetailedError(err error) string {
var njectError *njectError
if errors.As(err, &njectError) {
dups := duplicateTypes()
if dups != "" {
return err.Error() + "\n\n" + njectError.details +
"\n\nWarning: the following type names refer to more than one type:\n" +
dups
}
return err.Error() + "\n\n" + njectError.details
}
return err.Error()
}
var duplicatesThrough int
var dupLock sync.Mutex
var duplicates string
var duplicatesFound = make(map[string]struct{})
func duplicateTypes() string {
max := func() int {
lock.Lock()
defer lock.Unlock()
return typeCounter
}()
dupLock.Lock()
defer dupLock.Unlock()
if duplicatesThrough == max {
return duplicates
}
names := make(map[string]struct{})
for i := 1; i <= max; i++ {
n := typeCode(i).String()
if _, ok := names[n]; ok {
if _, ok := duplicatesFound[n]; !ok {
duplicates += " " + n
duplicatesFound[n] = struct{}{}
}
}
names[n] = struct{}{}
}
duplicatesThrough = max
return duplicates
}