diff --git a/aliases.go b/aliases.go index a686410c..6c89b756 100644 --- a/aliases.go +++ b/aliases.go @@ -318,6 +318,20 @@ func (p *prefixedDir) Open(name string) (http.File, error) { return p.fs.Open(name) } +type partyConfiguratorMiddleware struct { + handlers []Handler +} + +func (p *partyConfiguratorMiddleware) Configure(r Party) { + r.Use(p.handlers...) +} + +// ConfigureMiddleware is a PartyConfigurator which can be used +// as a shortcut to add middlewares on Party.PartyConfigure("/path", WithMiddleware(handler), new(example.API)). +func ConfigureMiddleware(handlers ...Handler) router.PartyConfigurator { + return &partyConfiguratorMiddleware{handlers: handlers} +} + var ( // Compression is a middleware which enables // writing and reading using the best offered compression. diff --git a/macro/handler/handler.go b/macro/handler/handler.go index b947ca93..1c5fe144 100644 --- a/macro/handler/handler.go +++ b/macro/handler/handler.go @@ -45,7 +45,7 @@ func CanMakeHandler(tmpl macro.Template) (needsMacroHandler bool) { if p.HandleError != nil { // Check for its type. if _, ok := p.HandleError.(ParamErrorHandler); !ok { - panic(fmt.Sprintf("HandleError must be a type of func(iris.Context, int, error) but got: %T", p.HandleError)) + panic(fmt.Sprintf("HandleError input argument must be a type of func(iris.Context, int, error) but got: %T", p.HandleError)) } } break diff --git a/macro/macros.go b/macro/macros.go index 4f3f563b..86eb28b8 100644 --- a/macro/macros.go +++ b/macro/macros.go @@ -595,3 +595,17 @@ func (ms *Macros) GetTrailings() (macros []*Macro) { return } + +// SetErrorHandler registers a common type path parameter error handler. +// The "fnHandler" MUST be a type of handler.ParamErrorHandler: +// func(ctx iris.Context, paramIndex int, err error). It calls +// the Macro.HandleError method for each of the "ms" entries. +func (ms *Macros) SetErrorHandler(fnHandler interface{}) { + for _, m := range *ms { + if m == nil { + continue + } + + m.HandleError(fnHandler) + } +} diff --git a/x/errors/context_error_handler.go b/x/errors/context_error_handler.go new file mode 100644 index 00000000..40f741c6 --- /dev/null +++ b/x/errors/context_error_handler.go @@ -0,0 +1,17 @@ +package errors + +import "github.com/kataras/iris/v12/context" + +// DefaultContextErrorHandler returns a context error handler +// which fires a JSON bad request (400) error message when +// a rich rest response failed to be written to the client. +// Register it on Application.SetContextErrorHandler method. +var DefaultContextErrorHandler context.ErrorHandler = new(jsonErrorHandler) + +type jsonErrorHandler struct{} + +// HandleContextError completes the context.ErrorHandler interface. It's fired on +// rich rest response failures. +func (e *jsonErrorHandler) HandleContextError(ctx *context.Context, err error) { + InvalidArgument.Err(ctx, err) +} diff --git a/x/errors/path_type_parameter_error_handler.go b/x/errors/path_type_parameter_error_handler.go new file mode 100644 index 00000000..801a47d0 --- /dev/null +++ b/x/errors/path_type_parameter_error_handler.go @@ -0,0 +1,10 @@ +package errors + +import "github.com/kataras/iris/v12/context" + +// DefaultPathTypeParameterErrorHandler registers an error handler for macro path type parameter. +// Register it with Application.Macros().SetErrorHandler(DefaultPathTypeParameterErrorHandler). +func DefaultPathTypeParameterErrorHandler(ctx *context.Context, paramIndex int, err error) { + param := ctx.Params().GetEntryAt(paramIndex) // key, value fields. + InvalidArgument.DataWithDetails(ctx, "invalid path parameter", err.Error(), param) +}