diff --git a/_examples/mvc/error-handler-http/main.go b/_examples/mvc/error-handler-http/main.go index 42f7ad43..f37a19fc 100644 --- a/_examples/mvc/error-handler-http/main.go +++ b/_examples/mvc/error-handler-http/main.go @@ -1,6 +1,8 @@ package main import ( + "fmt" + "github.com/kataras/iris/v12" "github.com/kataras/iris/v12/mvc" ) @@ -27,6 +29,14 @@ func (c *controller) Get() string { return "Hello!" } +func (c *controller) GetError() mvc.Result { + return mvc.View{ + // Map to mvc.Code and mvc.Err respectfully on HandleHTTPError method. + Code: iris.StatusBadRequest, + Err: fmt.Errorf("custom error"), + } +} + // The input parameter of mvc.Code is optional but a good practise to follow. // You could register a Context and get its error code through ctx.GetStatusCode(). // @@ -39,7 +49,12 @@ func (c *controller) Get() string { // a controller's field (or method's input argument) is required // to select which, between those two controllers, is responsible // to handle http errors. -func (c *controller) HandleHTTPError(statusCode mvc.Code) mvc.View { +func (c *controller) HandleHTTPError(statusCode mvc.Code, err mvc.Err) mvc.View { + if err != nil { + // Do something with that error, + // e.g. view.Data = MyPageData{Message: err.Error()} + } + code := int(statusCode) // cast it to int. view := mvc.View{ diff --git a/hero/container.go b/hero/container.go index 655461ad..7596ef30 100644 --- a/hero/container.go +++ b/hero/container.go @@ -168,6 +168,9 @@ var BuiltinDependencies = []*Dependency{ NewDependency(func(ctx *context.Context) Code { return Code(ctx.GetStatusCode()) }).Explicitly(), + NewDependency(func(ctx *context.Context) Err { + return Err(ctx.GetErr()) + }).Explicitly(), // payload and param bindings are dynamically allocated and declared at the end of the `binding` source file. } diff --git a/hero/handler.go b/hero/handler.go index 85911b8b..dc527d5b 100644 --- a/hero/handler.go +++ b/hero/handler.go @@ -20,7 +20,20 @@ type ( ErrorHandlerFunc func(*context.Context, error) // Code is a special type for status code. + // It's used for a builtin dependency to map the status code given by a previous + // method or middleware. + // Use a type like that in order to not conflict with any developer-registered + // dependencies. + // Alternatively: ctx.GetStatusCode(). Code int + + // Err is a special type for error stored in mvc responses or context. + // It's used for a builtin dependency to map the error given by a previous + // method or middleware. + // Use a type like that in order to not conflict with any developer-registered + // dependencies. + // Alternatively: ctx.GetErr(). + Err error ) // HandleError fires when a non-nil error returns from a request-scoped dependency at serve-time or the handler itself. diff --git a/mvc/aliases.go b/mvc/aliases.go index f52b09d6..e16292f2 100644 --- a/mvc/aliases.go +++ b/mvc/aliases.go @@ -16,6 +16,11 @@ type ( // http error handling in controllers. // This can be one of the input parameters of the `Controller.HandleHTTPError`. Code = hero.Code + // Err is a type alias for the `hero#Err`. + // It is a special type for error stored in mvc responses or context. + // It's used for a builtin dependency to map the error given by a previous + // method or middleware. + Err = hero.Err // DeprecationOptions describes the deprecation headers key-values. // Is a type alias for the `versioning#DeprecationOptions`. //