Skip to content

Commit

Permalink
Throw error if constructor panicked
Browse files Browse the repository at this point in the history
  • Loading branch information
snovichkov committed Mar 31, 2023
1 parent 26db557 commit bc1a29b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
7 changes: 6 additions & 1 deletion internal/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

package compiler

import "reflect"
import (
"errors"
"reflect"
)

type (
Closer = func() error
Expand All @@ -22,3 +25,5 @@ type (
Value reflect.Value
}
)

var ErrPanicked = errors.New("panicked")
18 changes: 16 additions & 2 deletions internal/compiler/constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func NewConstructor(fn any) (*Constructor, error) {
return c, nil
}

func (c *Constructor) Create(dependencies ...*Dependency) (reflect.Value, Closer, error) {
func (c *Constructor) Create(dependencies ...*Dependency) (_ reflect.Value, _ Closer, err error) {
var args = make([]reflect.Value, 0, len(dependencies))
for i, dep := range dependencies {
if c.vct && i == c.lin {
Expand All @@ -75,7 +75,11 @@ func (c *Constructor) Create(dependencies ...*Dependency) (reflect.Value, Closer
args = append(args, dep.Value)
}

var out = c.val.Call(args)
var out []reflect.Value
if out, err = c.call(args); err != nil {
return reflect.Value{}, nil, err
}

switch c.beh {
case behaviourValue:
return out[0], nil, nil
Expand Down Expand Up @@ -108,6 +112,16 @@ func (c *Constructor) Type() reflect.Type {
return c.typ.Out(0)
}

func (c *Constructor) call(args []reflect.Value) (_ []reflect.Value, err error) {
defer func() {
if recovered := recover(); recovered != nil {
err = fmt.Errorf("unable create because the constructor %w : %+v", ErrPanicked, recovered)
}
}()

return c.val.Call(args), nil
}

func (c *Constructor) guessBehaviour() {
switch {
case c.typ == nil:
Expand Down
32 changes: 23 additions & 9 deletions internal/compiler/constructor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ func TestConstructor(t *testing.T) {
}
)

var closer = func() error {
return nil
}
var (
closer = func() error {
return nil
}
oopsErr = errors.New("oops")
)

var testCases = []TestCase{{
Constructor: []string{},
Expand All @@ -47,6 +50,14 @@ func TestConstructor(t *testing.T) {
return 0, 0, 0, 0
},
Error: compiler.ErrInvalidConstructor,
}, {
Constructor: func() int {
panic("oops")
},
Result: &Result{
Error: compiler.ErrPanicked,
},
Type: reflect.TypeOf(0),
}, {
Constructor: func(a, b int) int {
return a + b
Expand All @@ -68,11 +79,11 @@ func TestConstructor(t *testing.T) {
Type: reflect.TypeOf(1),
}, {
Constructor: func() (int, error) {
return 0, errors.New("oops")
return 0, oopsErr
},
Dependencies: []*compiler.Dependency{},
Result: &Result{
Error: errors.New("oops"),
Error: oopsErr,
Value: reflect.ValueOf(0),
},
Type: reflect.TypeOf(0),
Expand All @@ -98,12 +109,12 @@ func TestConstructor(t *testing.T) {
Type: reflect.TypeOf(0),
}, {
Constructor: func() (int, func() error, error) {
return 0, closer, errors.New("oops")
return 0, closer, oopsErr
},
Dependencies: []*compiler.Dependency{},
Result: &Result{
Closer: closer,
Error: errors.New("oops"),
Error: oopsErr,
Value: reflect.ValueOf(0),
},
Type: reflect.TypeOf(0),
Expand Down Expand Up @@ -164,9 +175,12 @@ func TestConstructor(t *testing.T) {
}, "Dependencies not equal")

var v, c, e = cmp.Create(testCase.Dependencies...)
require.Equal(t, testCase.Result.Error, e)
require.Equal(t, testCase.Result.Value.Interface(), v.Interface())
if v.IsValid() {
require.Equal(t, testCase.Result.Value.Interface(), v.Interface())
}

require.Equal(t, reflect.ValueOf(testCase.Result.Closer).Pointer(), reflect.ValueOf(c).Pointer())
require.ErrorIs(t, e, testCase.Result.Error)
})
}
}

0 comments on commit bc1a29b

Please sign in to comment.