add some api helpers

This commit is contained in:
kataras 2022-05-05 02:46:15 +03:00
parent 4a43cd1fbb
commit 10c1542daf
No known key found for this signature in database
GPG Key ID: 403EEB7885C79503
5 changed files with 56 additions and 1 deletions

View File

@ -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.

View File

@ -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

View File

@ -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)
}
}

View File

@ -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)
}

View File

@ -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)
}