mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 10:41:03 +01:00
Path type parameter error handler: add the parameter index in the input arguments
see previous commit
This commit is contained in:
parent
5990e7f432
commit
e901575ca8
|
@ -31,11 +31,15 @@ The codebase for Dependency Injection, Internationalization and localization and
|
||||||
- **New feature:** add the ability to set custom error handlers on path type parameters errors (existing or custom ones). Example Code:
|
- **New feature:** add the ability to set custom error handlers on path type parameters errors (existing or custom ones). Example Code:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
app.Macros().Get("uuid").HandleError(func(ctx iris.Context, err error) {
|
app.Macros().Get("uuid").HandleError(func(ctx iris.Context, paramIndex int, err error) {
|
||||||
ctx.StatusCode(iris.StatusBadRequest)
|
ctx.StatusCode(iris.StatusBadRequest)
|
||||||
|
|
||||||
|
param := ctx.Params().GetEntryAt(paramIndex)
|
||||||
ctx.JSON(iris.Map{
|
ctx.JSON(iris.Map{
|
||||||
"error": err.Error(),
|
"error": err.Error(),
|
||||||
"message": "invalid path parameter",
|
"message": "invalid path parameter",
|
||||||
|
"parameter": param.Key,
|
||||||
|
"value": param.ValueRaw,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -153,11 +153,15 @@ func main() {
|
||||||
// UUIDv4 (and v1) path parameter validation.
|
// UUIDv4 (and v1) path parameter validation.
|
||||||
|
|
||||||
// Optionally, set custom handler on path parameter type error:
|
// Optionally, set custom handler on path parameter type error:
|
||||||
app.Macros().Get("uuid").HandleError(func(ctx iris.Context, err error) {
|
app.Macros().Get("uuid").HandleError(func(ctx iris.Context, paramIndex int, err error) {
|
||||||
ctx.StatusCode(iris.StatusBadRequest)
|
ctx.StatusCode(iris.StatusBadRequest)
|
||||||
|
|
||||||
|
param := ctx.Params().GetEntryAt(paramIndex)
|
||||||
ctx.JSON(iris.Map{
|
ctx.JSON(iris.Map{
|
||||||
"error": err.Error(),
|
"error": err.Error(),
|
||||||
"message": "invalid path parameter",
|
"message": "invalid path parameter",
|
||||||
|
"parameter": param.Key,
|
||||||
|
"value": param.ValueRaw,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -18,11 +18,11 @@ import (
|
||||||
// Note that the builtin macros return error too, but they're handled
|
// Note that the builtin macros return error too, but they're handled
|
||||||
// by the `else` literal (error code). To change this behavior
|
// by the `else` literal (error code). To change this behavior
|
||||||
// and send a custom error response you have to register it:
|
// and send a custom error response you have to register it:
|
||||||
// app.Macros().Get("uuid").HandleError(func(iris.Context, err error)).
|
// app.Macros().Get("uuid").HandleError(func(ctx iris.Context, paramIndex int, err error)).
|
||||||
// You can also set custom macros by `app.Macros().Register`.
|
// You can also set custom macros by `app.Macros().Register`.
|
||||||
//
|
//
|
||||||
// See macro.HandleError to set it.
|
// See macro.HandleError to set it.
|
||||||
type ParamErrorHandler = func(*context.Context, error) // alias.
|
type ParamErrorHandler = func(*context.Context, int, error) // alias.
|
||||||
|
|
||||||
// CanMakeHandler reports whether a macro template needs a special macro's evaluator handler to be validated
|
// CanMakeHandler reports whether a macro template needs a special macro's evaluator handler to be validated
|
||||||
// before procceed to the next handler(s).
|
// before procceed to the next handler(s).
|
||||||
|
@ -112,9 +112,8 @@ func MakeFilter(tmpl macro.Template) context.Filter {
|
||||||
if value != nil && p.HandleError != nil {
|
if value != nil && p.HandleError != nil {
|
||||||
// The "value" is an error here, always (see template.Eval).
|
// The "value" is an error here, always (see template.Eval).
|
||||||
// This is always a type of ParamErrorHandler at this state (see CanMakeHandler).
|
// This is always a type of ParamErrorHandler at this state (see CanMakeHandler).
|
||||||
p.HandleError.(ParamErrorHandler)(ctx, value.(error))
|
p.HandleError.(ParamErrorHandler)(ctx, p.Index, value.(error))
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -315,7 +315,7 @@ func (m *Macro) Trailing() bool {
|
||||||
|
|
||||||
// HandleError registers a handler which will be executed
|
// HandleError registers a handler which will be executed
|
||||||
// when a parameter evaluator returns false and a non nil value which is a type of `error`.
|
// when a parameter evaluator returns false and a non nil value which is a type of `error`.
|
||||||
// The "fnHandler" value MUST BE a type of `func(iris.Context, err error)`,
|
// The "fnHandler" value MUST BE a type of `func(iris.Context, paramIndex int, err error)`,
|
||||||
// otherwise the program will receive a panic before server startup.
|
// otherwise the program will receive a panic before server startup.
|
||||||
// The status code of the ErrCode (`else` literal) is set
|
// The status code of the ErrCode (`else` literal) is set
|
||||||
// before the error handler but it can be modified inside the handler itself.
|
// before the error handler but it can be modified inside the handler itself.
|
||||||
|
|
|
@ -34,7 +34,7 @@ type TemplateParam struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Index int `json:"index"`
|
Index int `json:"index"`
|
||||||
ErrCode int `json:"errCode"`
|
ErrCode int `json:"errCode"`
|
||||||
// Note that, the value MUST BE a type of `func(iris.Context, err error)`.
|
// Note that, the value MUST BE a type of `handler.ParamErrorHandler`.
|
||||||
HandleError interface{} `json:"-"` /* It's not an typed value because of import-cycle,
|
HandleError interface{} `json:"-"` /* It's not an typed value because of import-cycle,
|
||||||
// neither a special struct required, see `handler.MakeFilter`. */
|
// neither a special struct required, see `handler.MakeFilter`. */
|
||||||
TypeEvaluator ParamEvaluator `json:"-"`
|
TypeEvaluator ParamEvaluator `json:"-"`
|
||||||
|
|
Loading…
Reference in New Issue
Block a user