1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-17 09:57:01 +00:00

new Application.SetContextErrorHandler method

This commit is contained in:
Gerasimos (Makis) Maropoulos
2022-04-13 01:00:53 +03:00
parent 73dfabf412
commit ae828d8db9
7 changed files with 314 additions and 243 deletions

24
iris.go
View File

@@ -59,6 +59,8 @@ type Application struct {
*router.Router
router.HTTPErrorHandler // if Router is Downgraded this is nil.
ContextPool *context.Pool
// See SetContextErrorHandler, defaults to nil.
contextErrorHandler context.ErrorHandler
// config contains the configuration fields
// all fields defaults to something that is working, developers don't have to set it.
@@ -429,6 +431,28 @@ func (app *Application) GetContextPool() *context.Pool {
return app.ContextPool
}
// SetContextErrorHandler can optionally register a handler to handle
// and fire a customized error body to the client on JSON write failures.
//
// ExampleCode:
//
// type contextErrorHandler struct{}
// func (e *contextErrorHandler) HandleContextError(ctx iris.Context, err error) {
// errors.InvalidArgument.Err(ctx, err)
// }
// ...
// app.SetContextErrorHandler(new(contextErrorHandler))
func (app *Application) SetContextErrorHandler(errHandler context.ErrorHandler) *Application {
app.contextErrorHandler = errHandler
return app
}
// GetContextErrorHandler returns the handler which handles errors
// on JSON write failures.
func (app *Application) GetContextErrorHandler() context.ErrorHandler {
return app.contextErrorHandler
}
// ConfigureHost accepts one or more `host#Configuration`, these configurators functions
// can access the host created by `app.Run` or `app.Listen`,
// they're being executed when application is ready to being served to the public.