Skip to content

Commit a9b21cd

Browse files
committed
Fix nil error value returned from a provider
1 parent bbbcea0 commit a9b21cd

3 files changed

Lines changed: 15 additions & 5 deletions

File tree

‎Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ format: ## Format source files
102102
.PHONY: outdated
103103
outdated: ## Print outdated dependencies
104104
@go mod tidy
105-
@go list -u -m -json all | $(GOMODOUDATED_CMD) -update -direct
105+
@go list -u -m -json all | $(GOMODOUTDATED_CMD) -update -direct
106106

107107
.PHONY: weight
108108
weight: ## Print info about package weight

‎holder.go‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ func createLazyHolder(ctor any) (*holder, *Error) {
3939
if ctype.IsVariadic() {
4040
return nil, newInvalidConstructorError("variadic parameters not supported (use slice instead)")
4141
}
42-
errorInterface := reflect.TypeOf((*error)(nil)).Elem()
43-
if numResults == 2 && !ctype.Out(1).AssignableTo(errorInterface) {
44-
return nil, newInvalidConstructorError("expected second result value to be an error")
42+
if numResults == 2 {
43+
outtype := reflect.New(ctype.Out(1))
44+
if _, ok := outtype.Interface().(*error); !ok {
45+
return nil, newInvalidConstructorError("expected second result value to be an error")
46+
}
4547
}
4648
resultType := ctype.Out(0)
4749
numArgs := ctype.NumIn()
@@ -75,9 +77,12 @@ func createLazyHolder(ctor any) (*holder, *Error) {
7577
result := cval.Call(args)
7678
obj := result[0].Interface()
7779
if len(result) == 2 {
80+
if result[1].IsNil() {
81+
return obj, nil
82+
}
7883
err, ok := result[1].Interface().(error)
7984
if !ok {
80-
return nil, newInvalidConstructorError("expected second result value to be an error")
85+
return nil, newInvalidConstructorError("expected second result value to be an error xxxx")
8186
}
8287
return obj, err
8388
}

‎test/lazy_dependency_test.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ func (suite *LazyDependencySuite) TestGetByType() {
2525
provide: func(ctxb *di.ContextBuilder) { ctxb.Provide(func() *Foo { return &foo }) },
2626
get: func(ctx *di.Context) (any, error) { return di.GetOrErr[*Foo](ctx) },
2727
},
28+
{
29+
value: &foo,
30+
provide: func(ctxb *di.ContextBuilder) { ctxb.Provide(func() (*Foo, error) { return &foo, nil }) },
31+
get: func(ctx *di.Context) (any, error) { return di.GetOrErr[*Foo](ctx) },
32+
},
2833
{
2934
value: foo,
3035
provide: func(ctxb *di.ContextBuilder) { ctxb.Provide(func() Foo { return foo }) },

0 commit comments

Comments
 (0)