mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 02:17:05 +00:00
implement mvc HandleError as requested at #1244
Former-commit-id: 58a69f9cffe67c3aa1bab5d9425c5df65e2367ed
This commit is contained in:
44
_examples/mvc/error-handler/main.go
Normal file
44
_examples/mvc/error-handler/main.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
|
||||
"github.com/kataras/iris/mvc"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.Logger().SetLevel("debug")
|
||||
|
||||
mvcApp := mvc.New(app)
|
||||
// To all controllers, it can optionally be overridden per-controller
|
||||
// if the controller contains the `HandleError(ctx iris.Context, err error)` function.
|
||||
//
|
||||
mvcApp.HandleError(func(ctx iris.Context, err error) {
|
||||
ctx.HTML(fmt.Sprintf("<b>%s</b>", err.Error()))
|
||||
})
|
||||
//
|
||||
mvcApp.Handle(new(myController))
|
||||
|
||||
// http://localhost:8080
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
func basicMVC(app *mvc.Application) {
|
||||
// GET: http://localhost:8080
|
||||
app.Handle(new(myController))
|
||||
}
|
||||
|
||||
type myController struct {
|
||||
}
|
||||
|
||||
// overriddes the mvcApp.HandleError function.
|
||||
// func (c *myController) HandleError(ctx iris.Context, err error) {
|
||||
// ctx.HTML(fmt.Sprintf("<i>%s</i>", err.Error()))
|
||||
// }
|
||||
|
||||
func (c *myController) Get() error {
|
||||
return fmt.Errorf("error here")
|
||||
}
|
||||
Reference in New Issue
Block a user