mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 10:41:03 +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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/kataras/iris/v12"
|
"github.com/kataras/iris/v12"
|
||||||
"github.com/kataras/iris/v12/mvc"
|
"github.com/kataras/iris/v12/mvc"
|
||||||
)
|
)
|
||||||
|
@ -27,6 +29,14 @@ func (c *controller) Get() string {
|
||||||
return "Hello!"
|
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.
|
// 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().
|
// 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
|
// a controller's field (or method's input argument) is required
|
||||||
// to select which, between those two controllers, is responsible
|
// to select which, between those two controllers, is responsible
|
||||||
// to handle http errors.
|
// 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.
|
code := int(statusCode) // cast it to int.
|
||||||
|
|
||||||
view := mvc.View{
|
view := mvc.View{
|
||||||
|
|
|
@ -168,6 +168,9 @@ var BuiltinDependencies = []*Dependency{
|
||||||
NewDependency(func(ctx *context.Context) Code {
|
NewDependency(func(ctx *context.Context) Code {
|
||||||
return Code(ctx.GetStatusCode())
|
return Code(ctx.GetStatusCode())
|
||||||
}).Explicitly(),
|
}).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.
|
// 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)
|
ErrorHandlerFunc func(*context.Context, error)
|
||||||
|
|
||||||
// Code is a special type for status code.
|
// 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
|
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.
|
// 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.
|
// http error handling in controllers.
|
||||||
// This can be one of the input parameters of the `Controller.HandleHTTPError`.
|
// This can be one of the input parameters of the `Controller.HandleHTTPError`.
|
||||||
Code = hero.Code
|
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.
|
// DeprecationOptions describes the deprecation headers key-values.
|
||||||
// Is a type alias for the `versioning#DeprecationOptions`.
|
// Is a type alias for the `versioning#DeprecationOptions`.
|
||||||
//
|
//
|
||||||
|
|
Loading…
Reference in New Issue
Block a user