Skip to content

Commit

Permalink
Update error messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
googollee committed Apr 9, 2024
1 parent 58008dd commit ffefa6b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
4 changes: 3 additions & 1 deletion module/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
)

var ErrNoPrivoder = fmt.Errorf("can't find module")

type createPanic struct {
key moduleKey
err error
Expand All @@ -28,7 +30,7 @@ func (c *moduleContext) Value(key any) any {

provider, ok := c.providers[moduleKey]
if !ok {
panic(createPanic{key: moduleKey, err: fmt.Errorf("can't find module provideing %q", moduleKey)})
panic(createPanic{key: moduleKey, err: ErrNoPrivoder})
}

instance, err := provider.value(c)
Expand Down
4 changes: 2 additions & 2 deletions module/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func ExampleModule_createWithError() {
}

// Output:
// inject error: module *module_test.Cache creates an instance error: new cache error
// inject error: creating with module *module_test.Cache: new cache error
}

func ExampleModule_createWithPanic() {
Expand Down Expand Up @@ -187,7 +187,7 @@ func ExampleModule_notExistingProvider() {
}

// Output:
// inject error: module module_test.DB creates an instance error: can't find module provideing "module_test.DB"
// inject error: creating with module module_test.DB: can't find module
}

type FileSystem interface {
Expand Down
7 changes: 1 addition & 6 deletions module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,7 @@ func (m Module[T]) Value(ctx context.Context) T {
return null
}

ret, ok := v.(T)
if !ok {
return null
}

return ret
return v.(T)
}

func (m Module[T]) key() moduleKey {
Expand Down
25 changes: 14 additions & 11 deletions module/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,7 @@ func (r *Repo) AddModule(provider Provider) {
// It returns a new context with all injections. If any module creates an instance with an error, `InjectTo` returns that error with the module name.
func (r *Repo) InjectTo(ctx context.Context) (ret context.Context, err error) {
defer func() {
rErr := recover()
if rErr == nil {
return
}

createErr, ok := rErr.(createPanic)
if !ok {
panic(rErr)
}

err = fmt.Errorf("module %s creates an instance error: %w", createErr.key, createErr.err)
err = r.catchError(recover())
}()

providers := make(map[moduleKey]Provider)
Expand All @@ -74,3 +64,16 @@ func (r *Repo) InjectTo(ctx context.Context) (ret context.Context, err error) {

return
}

func (r *Repo) catchError(err any) error {
if err == nil {
return nil
}

createErr, ok := err.(createPanic)
if !ok {
panic(err)
}

return fmt.Errorf("creating with module %s: %w", createErr.key, createErr.err)
}

0 comments on commit ffefa6b

Please sign in to comment.