1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-17 09:57:01 +00:00

fix https://github.com/kataras/iris/issues/1450 and continue on implementing 1449

Former-commit-id: 617f64d061e88f050a443ea1751fa244164656c5
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-02-14 23:34:56 +02:00
parent 09a410c6cb
commit c13fd84354
24 changed files with 581 additions and 144 deletions

View File

@@ -91,7 +91,7 @@ type ControllerActivator struct {
dependencies di.Values
sorter di.Sorter
errorHandler hero.ErrorHandler
errorHandler di.ErrorHandler
// initialized on the first `Handle` or immediately when "servesWebsocket" is true.
injector *di.StructInjector
@@ -112,7 +112,7 @@ func NameOf(v interface{}) string {
return fullname
}
func newControllerActivator(router router.Party, controller interface{}, dependencies []reflect.Value, sorter di.Sorter, errorHandler hero.ErrorHandler) *ControllerActivator {
func newControllerActivator(router router.Party, controller interface{}, dependencies []reflect.Value, sorter di.Sorter, errorHandler di.ErrorHandler) *ControllerActivator {
typ := reflect.TypeOf(controller)
c := &ControllerActivator{
@@ -425,8 +425,9 @@ func (c *ControllerActivator) handlerOf(m reflect.Method, funcDependencies []ref
c.attachInjector()
// fmt.Printf("for %s | values: %s\n", funcName, funcDependencies)
funcInjector := di.Func(m.Func, funcDependencies...)
funcInjector.ErrorHandler(c.errorHandler)
// fmt.Printf("actual injector's inputs length: %d\n", funcInjector.Length)
if funcInjector.Has {
golog.Debugf("MVC dependencies of method '%s.%s':\n%s", c.fullName, m.Name, funcInjector.String())
@@ -452,15 +453,13 @@ func (c *ControllerActivator) handlerOf(m reflect.Method, funcDependencies []ref
return func(ctx context.Context) {
var (
ctrl = c.injector.Acquire()
ctxValue reflect.Value
errorHandler = c.errorHandler
)
// inject struct fields first before the BeginRequest and EndRequest, if any,
// in order to be able to have access there.
if hasBindableFields {
ctxValue = reflect.ValueOf(ctx)
c.injector.InjectElem(ctrl.Elem(), ctxValue)
c.injector.InjectElem(ctx, ctrl.Elem())
}
// check if has BeginRequest & EndRequest, before try to bind the method's inputs.
@@ -479,18 +478,15 @@ func (c *ControllerActivator) handlerOf(m reflect.Method, funcDependencies []ref
}
if funcHasErrorOut && implementsErrorHandler {
errorHandler = ctrl.Interface().(hero.ErrorHandler)
errorHandler = ctrl.Interface().(di.ErrorHandler)
}
if hasBindableFuncInputs {
// means that ctxValue is not initialized before by the controller's struct injector.
if !hasBindableFields {
ctxValue = reflect.ValueOf(ctx)
}
in := make([]reflect.Value, n)
in[0] = ctrl
funcInjector.Inject(&in, ctxValue)
funcInjector.Inject(ctx, &in)
if ctx.IsStopped() {
return // stop as soon as possible, although it would stop later on if `ctx.StopExecution` called.

View File

@@ -48,13 +48,14 @@ type Application struct {
Router router.Party
Controllers []*ControllerActivator
websocketControllers []websocket.ConnHandler
ErrorHandler hero.ErrorHandler
ErrorHandler di.ErrorHandler
}
func newApp(subRouter router.Party, values di.Values) *Application {
return &Application{
Router: subRouter,
Dependencies: values,
ErrorHandler: di.DefaultErrorHandler,
}
}
@@ -211,7 +212,7 @@ func makeInjector(injector *di.StructInjector) websocket.StructInjector {
return func(_ reflect.Type, nsConn *websocket.NSConn) reflect.Value {
v := injector.Acquire()
if injector.CanInject {
injector.InjectElem(v.Elem(), reflect.ValueOf(websocket.GetContext(nsConn.Conn)))
injector.InjectElem(websocket.GetContext(nsConn.Conn), v.Elem())
}
return v
}
@@ -258,8 +259,8 @@ func (app *Application) handle(controller interface{}) *ControllerActivator {
// HandleError registers a `hero.ErrorHandlerFunc` which will be fired when
// application's controllers' functions returns an non-nil error.
// Each controller can override it by implementing the `hero.ErrorHandler`.
func (app *Application) HandleError(errHandler hero.ErrorHandlerFunc) *Application {
app.ErrorHandler = errHandler
func (app *Application) HandleError(errorHandler func(ctx context.Context, err error)) *Application {
app.ErrorHandler = di.ErrorHandlerFunc(errorHandler)
return app
}

View File

@@ -3,12 +3,12 @@ package mvc
import (
"reflect"
"github.com/kataras/iris/v12/hero"
"github.com/kataras/iris/v12/hero/di"
)
var (
baseControllerTyp = reflect.TypeOf((*BaseController)(nil)).Elem()
errorHandlerTyp = reflect.TypeOf((*hero.ErrorHandler)(nil)).Elem()
errorHandlerTyp = reflect.TypeOf((*di.ErrorHandler)(nil)).Elem()
errorTyp = reflect.TypeOf((*error)(nil)).Elem()
)