mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 07:20:35 +01:00
add some api helpers
This commit is contained in:
parent
4a43cd1fbb
commit
10c1542daf
14
aliases.go
14
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.
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
17
x/errors/context_error_handler.go
Normal file
17
x/errors/context_error_handler.go
Normal 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)
|
||||
}
|
10
x/errors/path_type_parameter_error_handler.go
Normal file
10
x/errors/path_type_parameter_error_handler.go
Normal 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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user