|
| 1 | +package nject |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | +) |
| 7 | + |
| 8 | +// OverridesError marks a provider that is okay for that provider to override |
| 9 | +// error returns. Without this decorator, a wrapper that returns error but |
| 10 | +// does not expect to receive an error will cause the injection chain |
| 11 | +// compilation to fail. |
| 12 | +// |
| 13 | +// A common mistake is to have an wrapper that accidently returns error. It |
| 14 | +// looks like this: |
| 15 | +// |
| 16 | +// func AutoCloseThing(inner func(someType), param anotherType) error { |
| 17 | +// thing, err := getThing(param) |
| 18 | +// if err != nil { |
| 19 | +// return err |
| 20 | +// } |
| 21 | +// defer thing.Close() |
| 22 | +// inner(thing) |
| 23 | +// return nil |
| 24 | +// } |
| 25 | +// |
| 26 | +// The above function has two problems. The big problem is that it will |
| 27 | +// override any returned errors coming up from below in the call chain |
| 28 | +// by returning nil. The fix for this is to have the inner function return |
| 29 | +// error. If you aren't sure there will be something below that will |
| 30 | +// definitely return error, then you can inject something to provide a nil |
| 31 | +// error. Put the following at the end of the sequence: |
| 32 | +// |
| 33 | +// nject.Shun(nject.NotFinal(func () error { return nil })) |
| 34 | +// |
| 35 | +// The second issue is that thing.Close() probably returns error. A correct |
| 36 | +// wrapper for this looks like this: |
| 37 | +// |
| 38 | +// func AutoCloseThing(inner func(someType) error, param anotherType) (err error) { |
| 39 | +// var thing someType |
| 40 | +// thing, err = getThing(param) |
| 41 | +// if err != nil { |
| 42 | +// return err |
| 43 | +// } |
| 44 | +// defer func() { |
| 45 | +// e := thing.Close() |
| 46 | +// if err == nil && e != nil { |
| 47 | +// err = e |
| 48 | +// } |
| 49 | +// }() |
| 50 | +// return inner(thing) |
| 51 | +// } |
| 52 | +// |
| 53 | +func OverridesError(fn interface{}) Provider { |
| 54 | + return newThing(fn).modify(func(fm *provider) { |
| 55 | + fm.overridesError = true |
| 56 | + }) |
| 57 | +} |
| 58 | + |
| 59 | +func checkForMissingOverridesError(collection []*provider) error { |
| 60 | + var errorReturnSeen bool |
| 61 | + for i := len(collection) - 1; i >= 0; i-- { |
| 62 | + fm := collection[i] |
| 63 | + if errorReturnSeen && !fm.overridesError && fm.class == wrapperFunc { |
| 64 | + consumes, returns := fm.UpFlows() |
| 65 | + if hasError(returns) && !hasError(consumes) { |
| 66 | + return fmt.Errorf("wrapper returns error but does not consume error. Decorate with OverridesError() if this is intentional. %s", fm) |
| 67 | + } |
| 68 | + } |
| 69 | + if !errorReturnSeen { |
| 70 | + _, returns := fm.UpFlows() |
| 71 | + if hasError(returns) { |
| 72 | + errorReturnSeen = true |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +func hasError(types []reflect.Type) bool { |
| 80 | + for _, typ := range types { |
| 81 | + if typ == errorType { |
| 82 | + return true |
| 83 | + } |
| 84 | + } |
| 85 | + return false |
| 86 | +} |
0 commit comments