-
Notifications
You must be signed in to change notification settings - Fork 2
/
overrides_error.go
86 lines (82 loc) · 2.34 KB
/
overrides_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package nject
import (
"fmt"
"reflect"
)
// OverridesError marks a provider that is okay for that provider to override
// error returns. Without this decorator, a wrapper that returns error but
// does not expect to receive an error will cause the injection chain
// compilation to fail.
//
// A common mistake is to have an wrapper that accidentally returns error. It
// looks like this:
//
// func AutoCloseThing(inner func(someType), param anotherType) error {
// thing, err := getThing(param)
// if err != nil {
// return err
// }
// defer thing.Close()
// inner(thing)
// return nil
// }
//
// The above function has two problems. The big problem is that it will
// override any returned errors coming up from below in the call chain
// by returning nil. The fix for this is to have the inner function return
// error. If you aren't sure there will be something below that will
// definitely return error, then you can inject something to provide a nil
// error. Put the following at the end of the sequence:
//
// nject.Shun(nject.NotFinal(func () error { return nil }))
//
// The second issue is that thing.Close() probably returns error. A correct
// wrapper for this looks like this:
//
// func AutoCloseThing(inner func(someType) error, param anotherType) (err error) {
// var thing someType
// thing, err = getThing(param)
// if err != nil {
// return err
// }
// defer func() {
// e := thing.Close()
// if err == nil && e != nil {
// err = e
// }
// }()
// return inner(thing)
// }
//
func OverridesError(fn interface{}) Provider {
return newThing(fn).modify(func(fm *provider) {
fm.overridesError = true
})
}
func checkForMissingOverridesError(collection []*provider) error {
var errorReturnSeen bool
for i := len(collection) - 1; i >= 0; i-- {
fm := collection[i]
if errorReturnSeen && !fm.overridesError && fm.class == wrapperFunc {
consumes, returns := fm.UpFlows()
if hasError(returns) && !hasError(consumes) {
return fmt.Errorf("wrapper returns error but does not consume error. Decorate with OverridesError() if this is intentional. %s", fm)
}
}
if !errorReturnSeen {
_, returns := fm.UpFlows()
if hasError(returns) {
errorReturnSeen = true
}
}
}
return nil
}
func hasError(types []reflect.Type) bool {
for _, typ := range types {
if typ == errorType {
return true
}
}
return false
}