mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 02:31:04 +01:00
add mvc.Err builtin dependency to map any context/mvc response's.Err field on methods like HandleHTTPError
relative to: https://github.com/kataras/iris/issues/1606
This commit is contained in:
parent
f20e7f1ecc
commit
7f7d8df9c0
|
@ -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{
|
||||
|
|
|
@ -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.
|
||||
}
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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`.
|
||||
//
|
||||
|
|
Loading…
Reference in New Issue
Block a user